Win Api
Win Api
Example: simple_window
I always liked to do things first and learn them later...so here is the
code to a simple window which will be explained shortly.
#include <windows.h>
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
For most part this is the simplest windows program you can write that actually creates a
functional window, a mere 70 or so lines. If you got the first example to compile then this
one should work with no problems.
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
cbSize
The size of the structure.
style
Class Styles (CS_*), not to be confused with Window Styles (WS_*)
This can usually be set to 0.
lpfnWndProc
Pointer to the window procedure for this window class.
cbClsExtra
Amount of extra data allocated for this class in memory. Usually
0.
cbWndExtra
Amount of extra data allocated in memory per window of this
type. Usually 0.
hInstance
Handle to application instance (that we got in the first parameter
of WinMain()).
hIcon
Large (usually 32x32) icon shown when the user presses
Alt+Tab.
hCursor
Cursor that will be displayed over our window.
hbrBackground
Background Brush to set the color of our window.
lpszMenuName
Name of a menu resource to use for the windows with this class.
lpszClassName
Name to identify the class with.
hIconSm
Small (usually 16x16) icon to show in the taskbar and in the top
left corner of the window.
Don't worry if that doesn't make much sense to you yet, the various
parts that count will be explained more later. Another thing to
remember is to not try and remember this stuff. I rarely (never)
memorize structs, or function parameters, this is a waste of effort and,
more importantly, time. If you know the functions you need to call
then it is a matter of seconds to look up the exact parameters in your
help files. If you don't have help files, get them. You are lost without.
Eventually you will come to know the parameters to the functions you
use most.
Once the class is registered, we can create a window with it. You
should look up the paramters for CreateWindowEx() (as you should
ALWAYS do when using a new API call), but I'll explain them briefly
here.
HWND hwnd;
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
Next we have the class name (g_szClassName), this tells the system
what kind of window to create. Since we want to create a window from
the class we just registered, we use the name of that class. After that
we specify our window name or title which is the text that will be
displayed in the Caption, or Title Bar on our window.
Next (NULL, NULL, g_hInst, NULL) we have the Parent Window handle,
the menu handle, the application instance handle, and a pointer to
window creation data. In windows, the windows on your screen are
arranged in a heirarchy of parent and child windows. When you see a
button on a window, the button is the Child and it is contained within
the window that is it's Parent. In this example, the parent handle is
NULL because we have no parent, this is our main or Top Level window.
The menu is NULL for now since we don't have one yet. The instance
handle is set to the value that is passed in as the first parameter to
WinMain(). The creation data (which I almost never use) that can be
used to send additional data to the window that is being created is also
NULL.
If you're wondering what this magic NULL is, it's simply defined as 0
(zero). Actually, in C it's defined as ((void*)0), since it's intended for
use with pointers. Therefore you will possibly get warnings if you use
NULL for integer values, depending on your compiler and the warning
level settings. You can choose to ignore the warnings, or just use 0
instead.
Number one cause of people not knowing what the heck is wrong with
their programs is probably that they didn't check the return values of
their calls to see if they failed or not. CreateWindow() will fail at some
point even if you're an experianced coder, simply because there are
lots of mistakes that are easy to make. Untill you learn how to quickly
identify those mistakes, at least give yourself the chance of figuring
out where things go wrong, and Always check return values!
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
After we've created the window and checked to make sure we have a
valid handle we show the window, using the last parameter in
WinMain() and then update it to ensure that it has properly redrawn
itself on the screen.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
This is the heart of the whole program, pretty much everything that
your program does passes through this point of control.
If the message loop is the heart of the program, the window procedure
is the brain. This is where all the messages that are sent to our
window get processed.
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM
lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
The window procedure is called for each message, the HWND parameter
is the handle of your window, the one that the message applies to.
This is important since you might have two or more windows of the
same class and they will use the same window procedure ( WndProc()).
The difference is that the parameter hwnd will be different depending on
which window it is. For example when we get the WM_CLOSE message we
destroy the window. Since we use the window handle that we received
as the first paramter, any other windows will not be affected, only the
one that the message was intended for.
WM_CLOSE is sent when the user presses the Close Button or types Alt-
F4. This will cause the window to be destroyed by default, but I like to
handle it explicitly, since this is the perfect spot to do cleanup checks,
or ask the user to save files etc. before exiting the program.
Phew. Well that's it! If I haven't explained stuff clearly enough yet,
just hang in there and hopefully things will become more clear as we
get into more usefull programs.