Need this code explained

Pages: 12
Hi, i opened a new code blocks project and wanted to explore win32 gui stuff but i dont know what most of this stuff means, i know the switch statement and all that but as for windows 32 code i have no clue so could you explain to me what this stuff is or give me a link to a site that explains it in lamens terms. i would like to start getting into windows 32 gui programming and stuff cause doing stuff in cmd sickens me now.:


Main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}



Resource.rc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "resource.h"

DLG_MAIN DIALOGEX 6, 5, 194, 106

CAPTION "Code::Blocks Template Dialog App"

FONT 8, "Tahoma"

STYLE 0x10CE0804

BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
  CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 29, 46, 15
END



Resource.h:

1
2
3
4
5
6
7
8
#include <windows.h>

// ID of Main Dialog
#define DLG_MAIN 101

// ID of Button Controls
#define IDC_BTN_TEST 1001
#define IDC_BTN_QUIT 1002 
Last edited on
You've gotten yourself into some fun stuff here. While you may know the basic structure stuff of c++, you've just opened a can of worms which will require constant referencing to msdn.

Edit: I don't claim to be an expert here. I'm new to it too. Some of this info may be wrong, but the referenced links are correct. Check those out.

I use visual studio and in this IDE I have a nice feature that lets me right click on a variable and go to its definition. I assume that codeblocks has something similar.

Lets go through some of this stuff one by one.
resource.rc defines your dialogue box. It has the size, fonts, buttons and placements. The buttons will trigger specific "words" or "events" (example: IDC_BTN_TEST will be sent whenever the "Test" button is pressed). The words or events are defined in resource.h.


If I look at your C file, the first thing that I see is:
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
This replaces int main() in windows applications. The HINSTANCE variables are pointers to a particular instance, which I believe is the process but I may be wrong. If I check out what LPSTR is, I see that it is defined in WinNT.h as char* so really it is just a string.

Next you are using the DialogBox function
DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);

This is a standard Win32 function and its arguments/return value/usage can be found on the msdn site:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645452(v=vs.85).aspx

The last argument in DialogBox() is a pointer to the DialogProc. This is another default function that defines what will happen in our box.
Referene information can be found here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645469(v=vs.85).aspx

This is a particularly interesting function because this is what captures "events". This means it captures any button presses, resizing, mouse-overs, closing, etc. The "events" are inserted in the second parameter (UINT uMsg). Standard events that you may see are all defined in WinUser.h. These include WM_INITDIALOG, WM_CLOSE or WM_COMMAND. As an example, WM_CLOSE tells us that the user closed the window by using the "X" in the top right corner.

Now, WM_COMMAND will be accompanied by additional arguments found in wParam and lParam. I believe wParam is always present and lParam is an additional argument available when required. The words (or enums) found in wParam are defined in your resource.h and represent commands or buttons found in resource.rc. When you receive one of these messages, you will now have the chance to tell the program what to do.

Example, if the "Quit" button is selected, WM_COMMAND is sent in uMsg and IDC_BTN_QUIT is sent in wParam. If we go through the switch statement we see that we then call EndDialog(). This can be found here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645472(v=VS.85).aspx

The first argument of EndDialog is the pointer to the window's handle so we know which window to close. In this case, it is the current dialogue box handle hwndDlg.


I learned this stuff by going through an interesting Win32 API tutorial:
http://www.winprog.org/tutorial/

I recommend seeing that for further information.

Good luck
Last edited on
wow thanks :D i have some questions though:

In this line CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 29, 46, 15

What is: 0x10010000 ? what is that? also what does "Button" do? I tried changing it and it gave me an error so what is its purpose?
Last edited on
Good questions.

To answer #1 I can see that the author's intended output in the window was
You clicked "Test" button!
. To see how to make this message we need to look at what makes a string. A string is a set of characters that begins and ends with " . Now let's say that we want to have the " character appear in the console. If we simply added the quote, then we'd end the string instead of displaying the character. Instead the backslash (\) tells the compiler to expect a special character next, not a command. So if we write "Hello \" World" we will get
Hello " World
as an output. Other common special characters include:
Next line = \n
Tab = \t
End of string = \0.

To answer #2, we have to look at the reference for the MessageBox function.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx

We can see that the second argument contains the text for the message box and the third argument contains the caption. The caption is what you see on the top of the window and in the task-bar. Try changing it and you'll see that the title on the top of the window changes.
Oh, and happy new year! (12 minutes for me. I'm heading out).
Happy new year :D i still got 6 hours to go :) i'll look over those documents and try to understand as best i can. I'm going through the code and commenting everything i can so i can understand it better.
Ok so here is my updated code:

Main.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"



HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            //Blank
            return TRUE;

        case WM_CLOSE: // If the user clicks the X to close the dialog box
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "Cats Suck (JK)", "Information", MB_ICONINFORMATION);
                    return TRUE;

                case IDC_BTN_TESTBTN2:
                    MessageBox(hwndDlg, "You are seeing this message because my cat is a dickhead", "Information", MB_ICONINFORMATION);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}



Resource.rc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "resource.h"

//6 is the X position of the dialog box on the screen
//5 is the Y position of the dialog box on the screen
//304 is the width of the dialog box
//206 is the height of the dialog box

DLG_MAIN DIALOGEX 6, 5, 304, 206

CAPTION "Project Manager" //Title of the window

FONT 8, "Tahoma" //Font size (8) and type (Tahoma)

STYLE 0x10CE0804

//138,  5, 46, 15
//138 is the X position of the button, Left and right
//5 is the Y position of the button, Up and down
//46 is the buttons length
//15 is the buttons height
//&Test is the name of the button
//IDC_BTN_TEST is the name of the defined keyword in resource.h

/*
BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
END
*/

BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
  CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 48, 46, 15
  CONTROL "&Button", IDC_BTN_TESTBTN2, "Button", 0x10010000, 138, 29, 46, 15
END


So is there any way to make a textbox in this window? I tried changine "Button" to "TextBox" but i dont think its that easy.
Most IDEs come with a graphical utility for editing resource (.rc) files. These will let you place buttons and text as desired. I suggest using the one that comes with your compiler. That is the easiest way to do it.

If you want to manually edit your resource.rc, you'll do this between the BEGIN and END statements.

Each line in your files starts with CONTROL. There are also other options. Here is an example below from the Win32 API tutorial that I linked above:

1
2
3
4
5
6
7
8
9
10
11
IDD_ABOUT DIALOG DISCARDABLE 0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
FONT 8, "MS Sans Serif"
BEGIN
	DEFPUSHBUTTON "&OK",IDOK,174,18,50,14
	PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14
	GROUPBOX "About this program...",IDC_STATIC,7,7,225,52
	CTEXT "An example program showing how to use Dialog	Boxes\r\n\r\nby theForger",
	IDC_STATIC,16,18,144,33
END


In this example we are making a dialogue box, everything above BEGIN is setting up defaults for the window which will be displayed when we make a dialogue box with
DialogBox(... , MAKEINTRESOURCE(IDD_ABOUT), ... , ... )

We also see some STYLE options, FONT and a CAPTION set up here. Play around with those to see what they do.

Now for the meat, the BEGIN and END stuff...
You have CONTROL at the start of each line. This defines the type of object placed. There are several other options:

DEFPUSHBUTTON : This is the default pushbutton that is active when the window pops up. Pressing "ENTER" will select this pushbutton. Pressing TAB will switch the active pushbutton.

PUSHBUTTON : This is a normal pushbutton not selected by default. I think it is very similar to your CONTROL command.

GROUPBOX : This sets up a border which may contain text, or buttons or whatever. You can also place a caption for the border which is displayed on the top-left

CTEXT : This is what you are really looking for. It contains text, x/y placement, and width/height dimensions.

So try changing your resource.rc to this:
1
2
3
4
5
6
BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
  CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 48, 46, 15
  CONTROL "&Button", IDC_BTN_TESTBTN2, "Button", 0x10010000, 138, 29, 46, 15
  CTEXT "Your text here", IDC_STATIC, 25, 5, 100, 20
END


Where
25 is the X position (in pixels I think)
5 is the Y position
100 is the width
20 is the height.

Note IDC_STATIC simply means that this object is not associated with any wParam messages in the DialogProc function.
Last edited on
Wow I've been wanting to get into windows programming but it's always seemed so daunting. But you actually made it seem much more manageable. I'll have to keep this thread bookmarked. I think it should be articled/stickies too :)
Wow, awesome thanks!! I put in CTEXT "Your text here", IDC_STATIC, 25, 5, 100, 20
and my compiler (Code Blocks) just says syntax error??
Nevermind i got it, i forgot to define IDC_STATIC :P but how do i make a text box that can be typed in?

also I made some check boxed and a radial button but when i click them in the Interface they dont get checked, how do i make them so when you click them their checked. I didnt see anything about check boxes in there. Here is the code again:

Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"



HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            //Blank
            return TRUE;

        case WM_CLOSE: // If the user clicks the X to close the dialog box
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "Cats Suck (JK)", "Information", MB_ICONINFORMATION);
                    return TRUE;

                case IDC_BTN_TESTBTN2:
                    MessageBox(hwndDlg, "You are seeing this message because my cat is a dickhead", "Information", MB_ICONINFORMATION);
                    return TRUE;

                case Button:
                    MessageBox(hwndDlg, "Test Button 2", "Information", MB_ICONINFORMATION);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}



Resource.rc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "resource.h"

//6 is the X position of the dialog box on the screen
//5 is the Y position of the dialog box on the screen
//304 is the width of the dialog box
//206 is the height of the dialog box

DLG_MAIN DIALOGEX 6, 5, 304, 206

CAPTION "Project Manager" //Title of the window

FONT 8, "Ariel" //Font size (8) and type (Tahoma)

STYLE 0x10CE0804

//138,  5, 46, 15
//138 is the X position of the button, Left and right
//5 is the Y position of the button, Up and down
//46 is the buttons length
//15 is the buttons height
//&Test is the name of the button
//IDC_BTN_TEST is the name of the defined keyword in resource.h

/*
BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
END
*/

//CTEXT - Places text in the window, but not a text box.
//PUSHBUTTON - Similar to CONTROL
//RADIOBUTTON - Creates a Radio Button
//CHECKBOX - Creates a checkbox


BEGIN
  CONTROL "&Test", IDC_BTN_TEST, "Button", 0x10010000, 138,  5, 46, 15
  CONTROL "&Quit", IDC_BTN_QUIT, "Button", 0x10010000, 138, 48, 46, 15
  CONTROL "&Button", IDC_BTN_TESTBTN2, "Button", 0x10010000, 138, 29, 46, 15
  CTEXT "Your text here", IDC_STATIC, 138, 68, 46, 15
  GROUPBOX "GroupBox", IDC_STAT2, 25, 5, 100, 50
  PUSHBUTTON "HI", Button, 50, 25, 46, 15
  RADIOBUTTON "Radio Test", RadioB, 138, 88, 50, 50
  CHECKBOX "Checkbox", CheckB, 138, 120, 50, 50
END



Resource.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>

// ID of Main Dialog
#define DLG_MAIN 101

// ID of Button Controls
#define IDC_BTN_TEST 1001
#define IDC_BTN_QUIT 1002
#define IDC_BTN_TESTBTN2 1003
#define IDC_STATIC 1004
#define IDC_STAT2 1005
#define Button 1006
#define RadioB 1007
#define CheckB 1008 
Last edited on
Please help
That woud be an edit control:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Edit_box_IN_MAIN
	HWND hwndEditIn = CreateWindowEx(
            0, L"EDIT",   // predefined class 
            NULL,         // no window title 
            WS_CHILD | WS_VISIBLE | 
            ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, 
            10,				// x position
			45,				// y position
			( App::wWidth - 25 ),			// width
			150,		// height
            hWnd,         // parent window 
            (HMENU) ID_EDITBOX_IN_MAIN,   // edit control ID 
            (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE), 
            NULL);        // pointer not needed 


http://msdn.microsoft.com/en-us/library/windows/desktop/bb775464%28v=vs.85%29.aspx

Have a look at the Caesar Cipher I made:
http://sites.google.com/site/davevisone/home/win32api

I've added the source code in the download. I too have been teaching myself Win32 API.

Hope it helps!
Sorry, but at this point you've passed me in Win32.
So where would i put this in my program? This is all new and confusing to me, maybe if i understood what the keywords meant and did it would be a ton easier. These are the words i dont understand.

HWND
HINSTANCE
WinMain
hInstance
hPrevInstance
UINT
uMsg
WPARAM
wParam
LPARAM
lParam
CALLBACK
DialogProc
APIENTRY

I need these explained in lamens terms, I dont understand all that technical dialog that explains some of these in the links that were provided. I dont need a paragraph for each just a simple sentance explaining what there for and what they do.

Also the CMD box always pops up with my program and its annoying, how do i majke it go away? I was thinking it has something to do with this:

lpCmdLine int nShowCmd


Also How do i check the checkbox and Radio button when they are clicked?
Last edited on
Post Bump
closed account (zb0S216C)
This MSDN page will explain all of the terms you're having difficulty with: http://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx

Ch1156 wrote:
Also the CMD box always pops up with my program and its annoying, how do i majke it go away? (sic)

If you're using Code::Blocks, a console window will appear in the background when the project is ran in debug mode. In release mode, the console shouldn't show unless you tell it otherwise.

Wazzak
Thanks, i will read all of that, but first id like to figure out this checkbox dilemma. I have the checkbox in my window, and i click it but it doesnt check, and thats what i need. Ihave this in my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
switch(uMsg)
    {
        case WM_INITDIALOG:
            //Blank
            return TRUE;

        case WM_CLOSE: // If the user clicks the X to close the dialog box
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //MB_ICONINFORMATION is the type of icon displayed in the window that pops up. There is also//
                //MB_ICONHAND, MB_ICONSTOP, or MB_ICONERROR - Error Icon                                           //
                //MB_ICONQUESTION - Question mark Icon                                                                          //
                //MB_ICONEXCLAMATION or MB_ICONWARNING - Exclamation Icon                                       //
                //MB_ICONASTERISK or MB_ICONINFORMATION - Asterisk Icon                                            //
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "Your computer will now crash because you suck.", "Information", MB_ICONERROR);
                    return TRUE;

                case IDC_BTN_TESTBTN2:
                    MessageBox(hwndDlg, "Rated Argh for pirates fuck you.", "Information", MB_ICONWARNING);
                    return TRUE;

                case Button:
                    MessageBox(hwndDlg, "Test Button 2", "Information", MB_ICONINFORMATION);
                    return TRUE;

                case CheckB:
                        if (CheckB) {
                        BST_CHECKED;
                        }
            }
    }


BST_CHECKED Doesnt Work, The code compiles and everything without errors but the checkbox doesnt work. CheckB is CheckBox just to let you know.
Bump
Not being funny fella, but Google knows everything!!
http://www.apitalk.com/windows-Programming/Create-A-Checkbox-And-Track-Check-Uncheck-Events.html

Try using it! (:
Pages: 12