Post: [C++] Win32 WINAPI Make a GUI! #2 - Button
02-24-2014, 06:00 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hey guys!
This is the 2nd part of my Win32 Programming tutorial...

Today I will be teaching you on how to add a button to your program and code what it does!

Well my button will just be checking if the 'window' Computer (aka My Computer) is opened or not...

So first off what is a button?

Normally defined as 'In computing, a button (sometimes known as a command button or push button) is a user interface element that provides the user a simple way to trigger an event' Source: wikipedia...

A button is actually a Window! but you change the window class to the class BUTTON...
If you notice per example on avast .. it has a different GUI...

Example : You must login or register to view this content.



That´s because in C++ you can design your own stuff! You don´t need to be stuck with default windows stuff... YOU HAVE DIRECT3D!!!! ANYWAYS in these tutorials we will be learning the Win32 'lib'!

One day later I will make tutorial on how to create your own personal designed UI´s and then Game stuff..

Now let´s stick up to the tutorial...

So yeah creating a button is creating a window with a different class type...

The parameters of a button are :

DWORD dwExStyle -> usually in a button we set this as NULL so we wont go through this here...

LPCWSTR lpClassName -> this is the class name.. for a button is BUTTON... per example for textBox is EDIT... get it??

LPCWSTR lpWindowName -> this is the window name.. in this case the text displayed on the button

DWORD dwStyle -> in a button you will always need WS_VISIBLE | BS_DEFPUSHBUTTON... these are the style properties.. there are ALOT you can use..

int x -> this is the spot in the x cartesian where the button will stay

int y -> this is the spot in the y cartesian where the button will stay

int nWidth -> the width of the button (adjust as for how many text you have...)

int nHeight -> the height of the button (usually 30)

HWND hWndParent -> usually setted as our main window hwnd cuz the button will be a child of the window!

HMENU hMenu -> this is what will use the buttonID as..

the rest is null....

So let´s start!!!

First off you want to go to the top of your code and add a Define..

this will define your button... each control in your program (button , textBoxes...) needs to have their own ID!

so per example

    #define BUTTON_01    1


ok... if you were to make another button(or textbox) you could do #define BUTTON_02 2 or #define TEXTBOX_01 2...
Ok?

now that you have your button ID´ied you want to go into our handle class (LRESULT CALLBACK WndProc) and add another case called WM_CREATE:...

like this

    case WM_CREATE:
{
/*Button01*/
/*Button01-End*/
}
break;


as you can see we already created a spot for our button! Smile

that is where you will put any element that is added onto the program...

Ok...

So now in our Button01 area inside our new case we want to define some stuff...

our button class
our button text
and our button itself!

    LPCWSTR button01_ID = L"BUTTON";
LPCWSTR button01_text = L"Verify";
HWND button01 = CreateWindowEx(NULL, button01_ID, button01_text, BS_DEFPUSHBUTTON | WS_VISIBLE | WS_BORDER | WS_CHILD, 100, 50, 70, 30, hwnd, (HMENU)BUTTON_01, NULL, NULL);


In Here we used button01_ID as button01_class...


We have already filled up our code to create the window.. just match it up with the params I gave above....

So now you have a button!

Now let´s code what the button does...

You want to add a new case called WM_COMMANSad Awesome

like this

    case WM_COMMANSad Awesome
{
switch (wParam)
{

}
}
break;


and as you already understood, we created a switch for our wParam...

ok!

inside the switch you want to add a case for the buttonID! (defined on top of the code)

    case BUTTON_01:

break;


anything inside that case willl be what the BUTTON_01 does!

go onto the top of you code and create a void.. my button will just be checking if the dialog My Computer is opened.. so..

    void checkIfComputerIsOpened()
{
LPCWSTR Computer = L"Computador";
HWND computer = FindWindow(NULL, Computer);

if (computer == NULL)
{
LPCWSTR ComputerError = L"Computer is not opened!";
LPCWSTR ComputerError_Caption = L"Error";
MessageBox(NULL, ComputerError, ComputerError_Caption, MB_OK | MB_ICONERROR);
}
else
{
LPCWSTR ComputerError = L"Computer is opened!";
LPCWSTR ComputerError_Caption = L"Congrats";
MessageBox(NULL, ComputerError, ComputerError_Caption, MB_OK | MB_ICONINFORMATION);
}
}


thats my simple void...

Just put it into the button case like this..

    case BUTTON_01:
checkIfComputerIsOpened();
break;


AND YOUR DONE!

As you can see its very very simple...

Thank you for reading and I hope you understood...

Full Source : You must login or register to view this content.

Video :

The following 2 users say thank you to MW2TopTenWORLD for this useful post:

mikel20trucios
02-24-2014, 09:06 PM #2
Script
Banned
Originally posted by MW2TopTenWORLD View Post
Hey guys!
This is the 2nd part of my Win32 Programming tutorial...

Today I will be teaching you on how to add a button to your program and code what it does!

Well my button will just be checking if the 'window' Computer (aka My Computer) is opened or not...

So first off what is a button?

Normally defined as 'In computing, a button (sometimes known as a command button or push button) is a user interface element that provides the user a simple way to trigger an event' Source: wikipedia...

A button is actually a Window! but you change the window class to the class BUTTON...
If you notice per example on avast .. it has a different GUI...

Example : You must login or register to view this content.



That´s because in C++ you can design your own stuff! You don´t need to be stuck with default windows stuff... YOU HAVE DIRECT3D!!!! ANYWAYS in these tutorials we will be learning the Win32 'lib'!

One day later I will make tutorial on how to create your own personal designed UI´s and then Game stuff..

Now let´s stick up to the tutorial...

So yeah creating a button is creating a window with a different class type...

The parameters of a button are :

DWORD dwExStyle -> usually in a button we set this as NULL so we wont go through this here...

LPCWSTR lpClassName -> this is the class name.. for a button is BUTTON... per example for textBox is EDIT... get it??

LPCWSTR lpWindowName -> this is the window name.. in this case the text displayed on the button

DWORD dwStyle -> in a button you will always need WS_VISIBLE | BS_DEFPUSHBUTTON... these are the style properties.. there are ALOT you can use..

int x -> this is the spot in the x cartesian where the button will stay

int y -> this is the spot in the y cartesian where the button will stay

int nWidth -> the width of the button (adjust as for how many text you have...)

int nHeight -> the height of the button (usually 30)

HWND hWndParent -> usually setted as our main window hwnd cuz the button will be a child of the window!

HMENU hMenu -> this is what will use the buttonID as..

the rest is null....

So let´s start!!!

First off you want to go to the top of your code and add a Define..

this will define your button... each control in your program (button , textBoxes...) needs to have their own ID!

so per example

    #define BUTTON_01    1


ok... if you were to make another button(or textbox) you could do #define BUTTON_02 2 or #define TEXTBOX_01 2...
Ok?

now that you have your button ID´ied you want to go into our handle class (LRESULT CALLBACK WndProc) and add another case called WM_CREATE:...

like this

    case WM_CREATE:
{
/*Button01*/
/*Button01-End*/
}
break;


as you can see we already created a spot for our button! Smile

that is where you will put any element that is added onto the program...

Ok...

So now in our Button01 area inside our new case we want to define some stuff...

our button class
our button text
and our button itself!

    LPCWSTR button01_ID = L"BUTTON";
LPCWSTR button01_text = L"Verify";
HWND button01 = CreateWindowEx(NULL, button01_ID, button01_text, BS_DEFPUSHBUTTON | WS_VISIBLE | WS_BORDER | WS_CHILD, 100, 50, 70, 30, hwnd, (HMENU)BUTTON_01, NULL, NULL);


In Here we used button01_ID as button01_class...


We have already filled up our code to create the window.. just match it up with the params I gave above....

So now you have a button!

Now let´s code what the button does...

You want to add a new case called WM_COMMANSad Awesome

like this

    case WM_COMMANSad Awesome
{
switch (wParam)
{

}
}
break;


and as you already understood, we created a switch for our wParam...

ok!

inside the switch you want to add a case for the buttonID! (defined on top of the code)

    case BUTTON_01:

break;


anything inside that case willl be what the BUTTON_01 does!

go onto the top of you code and create a void.. my button will just be checking if the dialog My Computer is opened.. so..

    void checkIfComputerIsOpened()
{
LPCWSTR Computer = L"Computador";
HWND computer = FindWindow(NULL, Computer);

if (computer == NULL)
{
LPCWSTR ComputerError = L"Computer is not opened!";
LPCWSTR ComputerError_Caption = L"Error";
MessageBox(NULL, ComputerError, ComputerError_Caption, MB_OK | MB_ICONERROR);
}
else
{
LPCWSTR ComputerError = L"Computer is opened!";
LPCWSTR ComputerError_Caption = L"Congrats";
MessageBox(NULL, ComputerError, ComputerError_Caption, MB_OK | MB_ICONINFORMATION);
}
}


thats my simple void...

Just put it into the button case like this..

    case BUTTON_01:
checkIfComputerIsOpened();
break;


AND YOUR DONE!

As you can see its very very simple...

Thank you for reading and I hope you understood...

Full Source : You must login or register to view this content.

Video :


Instead of doing all of this just use Windows Forms Application. It's the same thing, just easier to do. No one writes GUI like this now days.
02-24-2014, 09:31 PM #3
Originally posted by Script View Post
Instead of doing all of this just use Windows Forms Application. It's the same thing, just easier to do. No one writes GUI like this now days.


There is no Windows Forms Application in C++... there is in 2010 . but its not real c++ its visual c++ (which belongs to the .net framework).... the Windows Forms Application in C++ Visual Studio 2013 is just a pre-compiled file with some of the stuff you will need to make a GUI Winky Winky

*sorry if I said any mistake
02-24-2014, 09:37 PM #4
Script
Banned
Originally posted by MW2TopTenWORLD View Post
There is no Windows Forms Application in C++... there is in 2010 . but its not real c++ its visual c++ (which belongs to the .net framework).... the Windows Forms Application in C++ Visual Studio 2013 is just a pre-compiled file with some of the stuff you will need to make a GUI Winky Winky

*sorry if I said any mistake


Microsoft developed a 'library' for C++ which is what you are using above. Which isn't 'real' C++. To me the 'real' C++ is written and compiled with mingw. However, this is C++; just it's using Microsoft's library.
02-24-2014, 09:40 PM #5
Originally posted by Script View Post
Microsoft developed a 'library' for C++ which is what you are using above. Which isn't 'real' C++. To me the 'real' C++ is written and compiled with mingw. However, this is C++; just it's using Microsoft's library.


MFC Classes... im not talking about that??

In 2010 you actually had a designer like in C# and code was like 'textBox1::Text->"TEXT CHANGED!";'

MFC Classes use the Win32 library.. they just simplify code.. and there is no difference between Visual Studio 2013 C++ than Mingw... just that you have to compile it by yourself in mingw *Sorry if I said any mistake
02-24-2014, 10:04 PM #6
Script
Banned
Originally posted by MW2TopTenWORLD View Post
MFC Classes... im not talking about that??

In 2010 you actually had a designer like in C# and code was like 'textBox1::Text->"TEXT CHANGED!";'

MFC Classes use the Win32 library.. they just simplify code.. and there is no difference between Visual Studio 2013 C++ than Mingw... just that you have to compile it by yourself in mingw *Sorry if I said any mistake


I haven't used VS in a while. I thought in VS2012-2013 they allow WFA for C++?
02-25-2014, 05:05 PM #7
Originally posted by Script View Post
I haven't used VS in a while. I thought in VS2012-2013 they allow WFA for C++?


They dont.. you have to design with code... they only allow designer in c# and vb

The following user thanked MW2TopTenWORLD for this useful post:

Script

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo