Creating a window with a border and caption

Hi everyone!

I'm learning how to write my first Windows application. I've written the following code to create a window class, register it, create a window of that class and show it. I know this program isn't complete and that it will crash whenever an event will reach it, but I'm just concerned about getting the window to shup up correctly at the moment, nothing more, nothing less. It works, but I just can't get a border and caption displayed. I'd really like to do this as I would like to write traditional applications in the future. This is my current program:

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
/* Standard C(++) include files. */
#include <iostream>
#include <cstdlib>

/* Include file for the Win32 API. */
#include <windows.h>

/* Window procedure coming in the future. For now the application will crash when an event reaches it. */
LRESULT CALLBACK my_window_procedure ( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) 
{
}

int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCmdShow )
{

    /* Our window class for the main window and a handle to our main window. */
    WNDCLASSEX my_window_class;
    HWND main_window;

    /* Initialize all members of the window class to zero, so we don't have 
       to worry about those members which we don't care about now. */
    memset ( & my_window_class, 0, sizeof ( my_window_class ) );
 
    /* Set some window class members which are of importance to us and 
       register it. */
    my_window_class.cbSize = sizeof ( my_window_class );
    my_window_class.style = CS_HREDRAW | CS_VREDRAW;
    my_window_class.lpfnWndProc = my_window_procedure;
    my_window_class.hInstance = hInstance;
    my_window_class.lpszClassName = "my_window_class";
    
    RegisterClassEx ( & my_window_class );    

    /* Create a window with a border and caption and set our handle to point to 
       it.*/
    main_window = CreateWindow ( "my_window_class", "My First Windows Application", WS_CAPTION, 0, 0, 250, 250, NULL, NULL, hInstance, NULL );
    
    /* Show that window */ 
    ShowWindow ( main_window, nCmdShow );

    /* Stop the program from exiting directly. */
    std::cin.get ( );
        
    return ( 0 );

}


I'm using Dev-C++ 4.9.9.2 to build it. It compiles and runs, but I just can't get the border and caption of the window to show up. MSDN says WS_CAPTION is the correct window class for such windows, so I really don't know where the problem lies.

Thanks in advance for your help,
Joshua
Line 42 is of no consequence in a Win32 project like this one. That approach is only good for console applications.

You show the window but you don't update it. Usually ShowWindow() is followed by UpdateWindow().

And your real problem: Your window procedure is just blank. That doesn't work. As a minimum you need to call DefWindowProc(). Without this call, nothing will ever be drawn, not even the border or the caption.
Thank you. Calling DefWindowProc() was the answer to my problem.

Oh, and it's possible to call the standard C++ I/O functions in a Win32 program without problmes, you'll just see a console window then, that's all.

Topic archived. No new replies allowed.