Problem with creating window!

Hi.
I am new to windows programming.
I wrote a code to show a simple window but there is a problem in it.
Please help me. BTW I use native C++.
Here's the 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
#include <windows.h>

int PASCAL WinMain(HINSTANCE hCurInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASS wc;
	HINSTANCE hinst;
	char myclass[] = "MyClass";

	wc.style = CS_CLASSDC;
	wc.lpfnWndProc = WNDPROC(hCurInstance, CS_CLASSDC, 0, 0); // <- Here's the Problem
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hCurInstance;
	wc.hIcon = LoadIcon(hCurInstance, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = myclass;

	ATOM regclassres = RegisterClass(&wc);
	if(regclassres != 0)
	{                     
		HWND hwnd = CreateWindow(myclass,
	"MyWindow",
	WS_OVERLAPPEDWINDOW,
	10,
	10,
	500,
	400,
	NULL,
	NULL,
	hCurInstance,
	NULL);
		ShowWindow(hwnd, SW_SHOW);
		UpdateWindow(hwnd);
	}
}

Thanks :-)
Errors that I see:

1) No window procedure function
2) No message loop, the program will instantly close
3) Not a full window class (UNLESS THE WNDCLASS STRUCTURE USES LESS/DIFFERENT MEMBERS, I'VE ALWAYS USED WNDCLASSEX SO I'M NOT SURE), you need:

cbClsExtra
cbWndExtra
cbSize
style
lpszClassName
lpszMenuName
lpfnWndProc
hInstance
hCursor
hIcon
hIconSm
hbrBackground

4) The way that you are programming it is "different" than the way most people program a window, here is how most people will do it (if you want you can learn how this works and you will be good to go)

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
#include <Windows.h>
LRESULT CALLBACK WindowProcedure (HWND, unsigned int, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow) {
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = "1"; // Or what ever you want, I always use numbers
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure; // Or what ever you named your wndproc function
	WindowClass.hInstance = hInstance;
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (255, 255, 255)); // The three numbers are the red, green, and blue values for your background window, 255 x3 = white
	if (!RegisterClassEx (&WindowClass)) {
		MessageBox (NULL, "Window class registration failed", NULL, MB_ICONWARNING);
		return 0;
	}
	HWND hWnd = CreateWindow ("1", // Or what you named your class
							  "Title here",
							  WS_OVERLAPPEDWINDOW,
							  315, 115, // Start points of your window
							  700, 480, // Width and height of your window
							  NULL, NULL,
							  hInstance, NULL);
	if (hWnd == NULL) {
		MessageBox (hWnd, "Window creation failed", NULL, MB_ICONWARNING);
		return 0;
	}
	ShowWindow (hWnd, SW_SHOW);
	MSG uMsg;
	while (GetMessage (&uMsg, NULL, 0, 0) > 0) {
		TranslateMessage (&uMsg);
		DispatchMessage (&uMsg);
	}
	return 0;
}
LRESULT CALLBACK WindowProcedure (HWND hWnd, unsigned int uiMsg, WPARAM wParam, LPARAM lParam) {
	switch (uiMsg) {
		case WM_CLOSE:
			DestroyWindow (hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage (0);
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}


Let me know if you have any questions on anything I posted here.
Last edited on
Thanks.
But I have a few questions.
1. What is window class name?
2. In your code, what is difference between hWnd and hInstance? In other word, what is difference between HWND and HINSTANCE?

Thanks again :-)
Thanks.
But I have a few questions.
1. What is window class name?
2. In your code, what is difference between hWnd and hInstance? In other word, what is difference between HWND and HINSTANCE?

Thanks again :-)
Thanks.
But I have a few questions.
1. What is window class name?
2. In your code, what is difference between hWnd and hInstance? In other word, what is difference between HWND and HINSTANCE?

Thanks again :-)
1. It appears you have named your Windows class MyClass but made a pointer to it as myclass; this may be a problem but I'm not sure I'm just a beginner too.

2. HWND is a handle to a window.
HINSTANCE is a handle to an instance (the program).

You seem to be using the Windows class older style which is not a problem for beginners like us I think. The Windows extended class style gives additional options though; even if you don't use them maybe best getting used to the idea and using it as a template. Here's an example of some of the code I would use at the beginning of a Windows 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
47
48
49
50
51
52
//	Registering Window Class///////////////////////
void RegisterMyWindow(HINSTANCE hInstance)
{
    WNDCLASSEX  wcex;	

    wcex.cbSize        = sizeof (wcex);				
    wcex.style         = CS_HREDRAW | CS_VREDRAW;		
    wcex.lpfnWndProc   = WndProc;						
    wcex.cbClsExtra    = 0;								
    wcex.cbWndExtra    = 0;								
    wcex.hInstance     = hInstance;						
    wcex.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON1));
    wcex.hCursor       = LoadCursor (NULL, IDC_HAND);	
	
	wcex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); 
    wcex.lpszMenuName  = (LPCSTR) IDR_MENU1;							
    wcex.lpszClassName = "FirstWindowClass";				
    wcex.hIconSm       = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON1)); 

	RegisterClassEx (&wcex);							
}


// Initialise Window Class////////////////////////
BOOL InitialiseMyWindow(HINSTANCE hInstance, int nCmdShow)
{
 
   //	Creating the Window
	HWND hwnd;
    hwnd = CreateWindowEx(
        WS_EX_OVERLAPPEDWINDOW,						//extended Window style
		"FirstWindowClass",							//pointer to registered class name
		"FirstWindow",									//pointer to Window name
        WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,	//Window styles
        100,										//horizontal position of Window
        100,										//vertical position of Window
        640,					//Window width
        480,					//Window height
		NULL,					//handle to parent or owner Window
		NULL,					//handle to menu, or child window identifier
		hInstance,				//handle to application instance
		NULL);					//pointer to Window-creation data

    if (!hwnd)
	{
		return FALSE;
	}
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
	ghwnd = hwnd;
	return TRUE;
}


Hope that helps.
It helps to read a tutorial http://www.winprog.org/tutorial/
Thank you guys :-)
closed account (yAqiE3v7)
The most basic way to get a simple window would be to use this code, though it can vary from compiler to compiler but give it a shot:

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
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Last edited on
Topic archived. No new replies allowed.