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