Need someone to check my windows-based application code

I think I just finished writing my first windows-based application and i was wondering if anyone can find any errors on my code. I want to make an application window and i want to make sure that my code can properly do that. If you think I am missing something crucial to create an application window, please tell me!

(p.s.. Should i copy the code to a notepad file and save it as a .exe to create the application window?)


Anyway, here is my code, so far I have created a parent window and a child window with their procedure.



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
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <windows.h>
#define FirstChild 100      //child window

HINSTANCE handle;                   //handle associated with a window
HWND mainWIN;                       //handle to the main window

mainWIN = CreateWindowEX (
        0,                          //No extended styles
        "Main Class",               //The name of the window class
        "My Awesome Window",        //the name of the window itself
        WS_OVERLAPPEDWINDOW |       //Style of the window
        WS_HSCROLL | WS_VSCROLL,
        CW_USEDEFAULT,              //default horizontal position
        CW_USEDEFAULT,              //default vertical position
        CW_USEDEFAULT,              //default width
        CW_USEDEFAULT,              //default height
        (HWND) NULL,                //no parent of owner window
        (HMENU) NULL,               //use window menu
        hinstance,                  //instance handle
        null);                      //no window creation data



if (!mainWIN)                       //checking to see if theres any problems with handle to main window
    return false;


 ShowWindow (mainWIN, SW_SHOWDEFAULT);  //displays the window
 UpdateWindow (mainWIN);







//creating child window for client space in main windows

long APIENTRY MainWindowsProcedure (HWND handle, UINT message, WPARAM w, LPARAM l)
{
    RECT coordinates_of_client;                //object that contains the corner coordinates


    switch (message)
    {
        case WM_CREATE:         //creating main window

            CreateWindowEX                  //creating child window
            (
             0,                             //no extended styles
             "Child Class",                 //name of child window class
             (LPCTSTR) NULL,                //child window will not have a name
             WS_CHILD | WS_BORDER,          //specifying the style of the window
             0,0,0,0,                       //no scrolls or height or width on child window
             handle,                        //specifying parent window for this child window
             (HMENU) (int) (FirstChild)     //menu for child window (consists of child menu identifier)
             NULL                           //no windows creation data
            );
            return 0;

        case WM_SIZE:                       //main window size

            GetClientRect (handle, &coordinates_of_client); 
            EnumChildWindows (handle, EnumChildProc, (LPARAM)& coordinates_of_client);  
            return 0;
    }

    return DefWindowProc (handle, message, w,l)   //defining windows procedure
}
Last edited on
I don't see where you've called RegisterClassEx() to register your ""Main Class".

Also, I don't understand your comment about saving the code to an exe file.
OK i re-did most of my code and i filled out all the members of the wndclassex structure so that i can define my class. and of course i called the RegisterClassEx() function

BTW, Do you think that my code is enough to make a simple windows application?? or am i missing something else? (i'm just trying to make my first windows application)

Here is my updated 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <windows.h>
#define FirstChild 100              //child window

HINSTANCE handle;                   //handle associated with a window
HWND mainWIN;                       //handle to the main window

//prototype
long APIENTRY Windows_Procedure (HWND , UINT , WPARAM , LPARAM );



bool registering_class (HINSTANCE handle_to_win_procedure)
{
    WNDCLASSEX mynewclass;                              //declaring structure object
    
    mynewclass.cbSize = sizeof (mynewclass);            //giving the size of class
    mynewclass.style = CS_HREDRAW | CS_VREDRAW;         //specifying class styles
    mynewclass.Windows_Procedure;                       //passing pointer of windows procedure
    mynewclass.cbClsExtra = 0;                          //no extra bytes for allocating the window class structure
    mynewclass.cbWndExtra = 0;                          //no extra bytes for the windows instance
    mynewclass.handle_to_win_procedure;                 //specifying the handle to the windows procedure
    mynewclass.hIcon = NULL;                            //if this is null, then application will load a default icon
    mynewclass.hCursor = LoadCursor (NULL, IDC_CROSS)   //specifying the image of the mouse cursor
    mynewclass.hbrBackground = GetStockObject (WHITE_BRUSH) //setting the background of client to a white shade
    mynewclass.lpszMenuName = "Main Menu";                  //specifying the name of window menu
    mynewclass.lpszClassName = "Main Class";                //specifying the name of the window class
    mynewclass.hIconSm = NULL;                             
    
    RegisterClassEx (&mynewclass);
}






//creating main windows

mainWIN = CreateWindowEX (
        0,                          //No extended styles
        "Main Class",               //The name of the window class
        "My Awesome Window",        //the name of the window itself
        WS_OVERLAPPEDWINDOW |       //Style of the window
        WS_HSCROLL | WS_VSCROLL,
        CW_USEDEFAULT,              //default horizontal position
        CW_USEDEFAULT,              //default vertical position
        CW_USEDEFAULT,              //default width
        CW_USEDEFAULT,              //default height
        (HWND) NULL,                //no parent of owner window
        (HMENU) NULL,               //use window menu
        hinstance,                  //instance handle
        null);                      //no window creation data



if (!mainWIN)                       //checking to see if theres any problems with handle to main window
    return false;


 ShowWindow (mainWIN, SW_SHOWMAXIMIZED);  //displays the window
 UpdateWindow (mainWIN);                  //updates window and sends a WS_PAINT message to paint the application









long APIENTRY Windows_Procedure (HWND handle1, UINT message, WPARAM w, LPARAM l)  
{
    RECT coordinates_of_client;                


    switch (message)                        //message will contain an integer value
    {
        case WM_CREATE:                    

            CreateWindowEX                  //creating child window
            (
             0,                             //no extended styles
             "Main Class",                 //name of child window class
             (LPCTSTR) NULL,                //child window will not have a name
             WS_CHILD | WS_BORDER,          //specifying the style of the window
             0,0,0,0,                       //no scrolls or height or width on child window
             handle1,                        //specifying parent window for this child window
             (HMENU) (int) (FirstChild)     //menu for child window
             NULL                           //no windows creation data
            );
            return 0;

        case WM_SIZE:                                         
            GetClientRect (handle1, &coordinates_of_client); 
            EnumChildWindows (handle1, EnumChildProc, (LPARAM)& coordinates_of_client);
            return 0;
    }

    ShowWindow (handle1, SW_SHOW);
    return DefWindowProc (handle1, message, w,l)  
}


Last edited on
Don't you need a WinMain?
and mynewclass.Windows_Procedure; doesn't exist, nor does it set your Windows_Procedure function as the window procedure of your class.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633577%28v=vs.85%29.aspx
use:
mynewclass.lpfnWndProc=Windows_Procedure;
I think you should take a look at this tutorial:
http://www.winprog.org/tutorial/start.html
And judging by your save as .exe comment:
http://www.cplusplus.com/doc/tutorial/introduction/
Yep, some major conceptual problems, but hey! Keep trying!!!

As Homberto said, that Forgers Tutorial is pretty good. Its not that hard to get a Windows Api GUI program up and running. Here's as simple and small of a compilable example as I can come up with. Makes a main program window wit title bar, etc, and compiles to only 7K with my GCC 4.4 series compiler...

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
//Main.cpp
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>

LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 if(msg==WM_DESTROY)
 {
    PostQuitMessage(0);
    return 0;
 }

 return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 wchar_t szClassName[]=L"Form1";
 WNDCLASSEX wc;
 MSG messages;
 HWND hWnd;

 memset(&wc,0,sizeof(WNDCLASSEX));
 wc.lpszClassName = szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize        = sizeof (WNDCLASSEX);        wc.hInstance=hIns;
 wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,75,75,320,305,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}


Saving it as an exe file won't buy you anything more than an error message when you click on it though. A C++ compiler is needed to read in the textural source and spit out binary op codes in the form of an executable file which Windows can recognize and execute.
Topic archived. No new replies allowed.