Saturday, February 28, 2009

How to log in to a website from a form (.NET, VC++2008)

Introduction
Some times you want to login to a website. Instead of using a Web browser control and having the user sign in, it would be much neater if we can just have two text boxes and a login button. Based on user request, in this tutorial we will be logging in to Facebook.

It is recommended you have some experience understanding of C++ before we continue. Also, I'm sorry about the indenting problem (Blogger seems to remove all indenting).

Step 1
Open up Visual C++ 2008 (other VC++ versions may vary slightly in code). Click File -> New Project. Type in a name for your project. Make sure you are under the "Visual C++" node and have Windows Forms Application selected.

Go ahead and click OK to create your project.

Step 2
Double click on the form (the window) to create a Load event handler for Form1. The load event handler should look something like this:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}


Let's create a web browser that is hidden from view, but we can still use it. Let's also make it load to the homepage of Facebook.
System::Windows::Forms::WebBrowser^ m_Web;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
m_Web = gcnew WebBrowser();
}


So, after we navigate, we want to create two text boxes. Switch back to designer view and create two text boxes.


Now we have TextBox1 and TextBox2. Using the same process, add a Button to the form. Then double-click on it.

We now have everything in place, set up and ready to go. Time for some coding.

Step 3: Code
Switch back to Form1.h using Solution Explorer or the tab at the top. We will now have to create a event handler for our web browser. After we create the event handler, we will do login error checking, then log in.
System::Windows::Forms::WebBrowser^ m_Web;
int count;

public: void myDocComplete(Object^ sender, WebBrowserDocumentCompletedEventArgs^ e)
{
if (e->Url->ToString() == "http://www.facebook.com/index.php")
{
m_Web->Document->GetElementById("email")->SetAttribute("value", textBox1->Text);
m_Web->Document->GetElementById("pass")->SetAttribute("value", textBox2->Text);

m_Web->Document->GetElementsByTagName("input")[6]->InvokeMember("click");

} else
if (e->Url->ToString() == "http://www.facebook.com/home.php?")
{
MessageBox::Show("Logged in.");
}
else
if (e->Url->ToString() == "https://login.facebook.com/login.php?login_attempt=1")
{
MessageBox::Show("Incorrect login info");
}
}

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
count = 0;
m_Web = gcnew WebBrowser();
m_Web->DocumentCompleted += gcnew WebBrowserDocumentCompletedEventHandler(this, &winform::Form1::myDocComplete);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
m_Web->Navigate("http://www.facebook.com/index.php");
}


Next, change the line where it says "WebBrowserDocumentCompletedEventHandler(this, &winform::Form1::myDocComplete)" to the name of your namespace. Then we're all done!

Here's a demo project made in VC++ 2008 for those of you that need it. Open winform.sln, compile and run!

Click here to download the demo project. If that doesn't work, go here.

Thursday, February 26, 2009

Blinking colored text illusion

Introduction
Everyone likes to add some color and features to their program. This is the easiest way to achieve it, but it uses OS functions and clears the whole screen. There are much more efficient ways to achieve this, but this is a very easy way nevertheless.

Update: CppBuilder2006 found some errors in the code in Borland compilers, and the code was updated to work with VC++ and Borland.

Code
Just compile and run. I am sorry about the indenting problem. This codebox is having issues at the moment.
#include <iostream>
#include <windows.h>

using namespace std;

enum Colors { blue = 1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

int main(int argc, char* argv[])
{
// print, clear, then print again 10 times
for (int i = 0; i < 10; i++)
{
system("cls");
SetConsoleTextAttribute(handle, hwhite);
cout << "HI!\n";
cout << "This is blinking text =)";
Sleep(500);
system("cls");
cout << "HI!\n";
system("cls");
}
return 0;
}


Sunday, February 15, 2009

The ultimate winsock tutorial (C++)

Ever wanted to learn to make MMORPG's? DirectInput, now deprecated, will still work, but before you begin those tutorials, I highly recommend you start with lonewolf's Winsock tutorials.
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_1.shtm

Saturday, February 14, 2009

Console: Search and replace with std::string (C++)

Introduction
Let's face it. std::string is a thousand times better than a char*. It is widely used to store input and output. Not to mention convenient. However, it lacks some important features. Searching and replacing portions of the text would be a very neat feature indeed. A feature like that isn't already made for us, so we have to make our own. It's a good thing std::string has find() and insert().

Code
Here's the function and an example of how to use it. It replaces "Joe" with "Mike".
#include <iostream>

std::string Replace(std::string Str, std::string Old, std::string New)
{
// the location of the string to replace string
int x;

// while we can find the str to replace
while( (x=Str.find(Old)) >= 0 )
{
// for every character in the string to replace
for (std::string::size_type i = 0; i < Old.length(); i++)
// replace it with ""
Str.replace(x, 1, "");

// then insert our new string into the main one
Str.insert(x, New);
}

// after we're done, return the new, replaced string
return Str;
}

int main()
{
// make an example string, then replace
std::string exampleStr = "Hi! Are you Joe?";
exampleStr = Replace(exampleStr, "Joe", "Mike");

// print string
std::cout << exampleStr.c_str();

return 0;
}


Console: Setting the text color (C++)

Introduction
When making console apps, be it just learning C++ or for a school project, you want your apps to appeal to the feature-hungry crowd.

One way you can increase the loveliness of your application is by easily setting the color your console font.

In all fairness, it's very easy. In order to change the color, we will need to use SetConsoleTextAttribute(), which is located in windows.h.

Code
enum Colors { blue = 1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

void Print(int color, std::string output)
{
SetConsoleTextAttribute( handle, color);
cout << output;
}


 

C++ Universe - The universe compromised of C++ and C#. Powered By Blogger © 2009.