creating windows

Hello, I've been looking at making windows, but I don't understand it fully. Is there some tutorial on youtube / this forum ( I have searched) or yourself that can explain every line to me?
closed account (G309216C)
Hi,

There are plenty of resources available on internet related to GUI programming a prime and great example would be :[i]"forgers WinAPI"[/o]

Link: http://www.winprog.org/tutorial/

Sorry, but I am not going to give source code because it is all in the Link given.

Good Luck!
I don't understand this:

1
2
3
WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

How can this set the windows stuff when it's not passed through the function? Wouldn't it value to 0? and only make the window 0 (if you have more than one window it would only make the first one)?


EDIT: HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam
I also don't understand the LPAram etc
Last edited on
Did this (Tried to re write the code to understand it)
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
#include <Windows.h>

//global class name
const char WindowC[] = "WindowsClass";


//this handles everything you do to the window (clicking etc)
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg) {

		//skype is a fegget
		case WM_CLOSE: MessageBox(NULL, "NO!", NULL, NULL);

	}

	return 0;
}

//the main function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX window;//object
	HWND hwnd;//the handle
	MSG msg;//the message

	window.cbSize        = sizeof(WNDCLASSEX);
    window.style         = 0;
    window.lpfnWndProc   = WndProc;
    window.cbClsExtra    = 0;
    window.cbWndExtra    = 0;
    window.hInstance     = hInstance;
    window.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    window.hCursor       = LoadCursor(NULL, IDC_ARROW);
    window.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    window.lpszMenuName  = NULL;
    window.lpszClassName = WindowC;
    window.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&window))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, WindowC, "Hello", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


	while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

	return msg.wParam;
}


But it always says window creation failed.
SpaceWorm gave you good advice. If you followed it, you would understand why your window creation is failing.
closed account (G309216C)
Thanks! Freddie. There is a small error in the code and it is very obvious to spot. Just read through the Book again.

Good Luck!
Note that there are two omissions in your code cf. the tutorial. Both in WndProc. One will fix CreateWindowEx but the other fix is required to get the program to actually exit.

In addition to the tutorial SpaceWorm pointed you at, you might find reading the MSDN entries for the main functions, structs, and messages helpful. A lot of them are cross-reference, so it should be easy to navigate about. And while you don't need to read everything (esp for long entries, like CreateWindowEx) but the intro, the return, and the comments are worth reading.

Regarding your specific questions:

WNDCLASSEX window;//object

Here WNDCLASSEX is not an object. It's (a struct defining) the window class. Not class in the C++ sense; here class just mean the type: i.e. and window of class EDIT is an Edit control window. The class has a set of properties (the values set when you register it, including an associated WndProc) which are common for all windows of the class.

It is better to stick with either wc or use wndClass for the variable name as it's not actually a window. The window handle (here hwnd) is what identifies each instance of a given class of window.

When you define a new window class, you have to register it before use so the system know how to create it, in particular, the address of the function which handle its message (the window proceedure, often WndProc). You do this by filling in the structure and then passing it to RegisterClassEx. When this returns (successfully), you can go on the create windows of this class.

HWND hwnd;

This is the handle which identifies the window instance. You can have more than one instance of a window, assuming you have coded your WndProc correctly e.g. avoided static data. (CreateWindowsEx won't -- itself -- prevent you from creating more than one window instance, but a badly written WndProc will prevent it from working correctly.)

Handle (computing)
http://en.wikipedia.org/wiki/Handle_%28computing%29

You create a window using CreateWindowEx.

When you call CreateWindowEx, the system allocates the resources for the window (associating it with a handle) and then calls your WndProc with WM_CREATE to allow you to initialize data for the instance. This is where you should create child windows, if you need them, rather than in the WndMain.

By handling WM_CREATE, you can control whether the window creatinon succeeds: if you return 0, the window creation is allowed to complete; if you return -1, the creation is aborted and a value of NULL returned by CreateWindowEx. (This mechanism can be used to prevent more than one instance from being created.)

MSG Msg;

MSG is struct (actually, a typedef of a struct) which stores information about a window message, including the message code (e.g. WM_CREATE, WM_PAINT), the window handle, the W and L params.

The application is responsible for spinning a message loop. It reads a message from the message queue (GetMessage), translates virtual-key messages into character message (TranslateMessage) and then forwards the message onto the appropriate window (DispatchMessage). The message loop can be extended to do more processing, if and when required.

How can this set the windows stuff when it's not passed through the function?

Not sure which "stuff" or function you refer to:
- window (or wc) is passed to RegisterClassEx using the address-of operator (&)
- hwnd accepts the return from CreateWindowEx
- msg is filled in by GetMessage and then passed to TranslateMessage and then DispatchMessage, also using the address-of operator (&). DispatchMessage relays the message to the appropriate WndProc

I also don't understand the LPAram etc

WPARAM and LPARAM are typedefs of integral types. Which type depend on whether you're talking about Win32 or Win64. The names come from the olden, Win16 days when W = word (16 bit in Win16) and L = long (32 bit). But nowaways they are just known as 'W' and 'L'.

What do the letters W and L stand for in WPARAM and LPARAM?
http://blogs.msdn.com/b/oldnewthing/archive/2003/11/25/55850.aspx

What are the definitions for LPARAM and WPARAM?
http://stackoverflow.com/questions/2515261/what-are-the-definitions-for-lparam-and-wparam

Andy
Last edited on
u forgot this line in initializing WNDCLASSEX strucutere:
window.cbSize = sizeof (WNDCLASSEX)

creation fails because sizeof registered class is unkown
also, use UnregisterClass () funciton deallocate memory properly (at the end of app)
There is no need to use UnregisterClass under routine circumstances.

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

All window classes that an application registers are unregistered when it terminates.

Andy

PS And I've just grepped the Microsoft Windows SDK samples, as a sanity check, for UnregisterClass: only 6 samples use this function (haven't investigated why...) cf. loads and loads using RegisterClassEx

Topic archived. No new replies allowed.