Now to do the windows stuff we must include the windows header file
windows.h
uhmm so
as any normal snippet, we have a main() function, in WINAPI its like the following:
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )Alright lets explain. HINSTANCE hInstance handles the current instance of application running.
HINSTANCE hPrevInstance handles the previous instance running or ran or w/e.
LPSTR lpCmdLine The command line arguments as a single string. NOT including the program name.
int nCmdShow is the current status of the window, either hidden, system tray maximised, whatever, we'll learn about them in another tut xD
now that we know what all that stuff was about, lets see what "msg" is
a message, well you can consider it as a loop that keeps checking for activity in window, any buttons were pressed, windows created, windows quit, or stuff.. we declare it like so
MSG msg;So untill now we have
#include<windows.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;
now to register our window ^_^what forms a window my fellow coders ?
The class name, its handle, its background color, its cursor and the procedure used by this window. All of that is saved in a structure, then the structure is registered .unless you want to register it one by one >.>
so lets say our structure is wc.
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "My Window" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
explanation ? there you go"
lpsz class name is the class name of our window.
hInstance is the instance for our window.
background color is the backgounr color for our window
hCursor is the cursor used in this window, in our case its the normal IDC_ARROW.
now we must register our struct xD We do like so
RegisterClass(&wc); , the wc is the structures name, kkay ? kay, move on..
now to create the window, ths thing we can see =)
we use Createwindow() to create a window, textbox or any object in our win32 application.
our former code is
#include<windows.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "My Window" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
Now creating our window
CreateWindow( wc.lpszClassName, TEXT("Our first window xD!"),
WS_VISIBLE|WS_OVERLAPPEDWINDOW,
100, 100, 500, 500, 0, 0, hInstance, 0);
Just add them together i'm not going to add them now,now we should get the message, get the message ?
as i have said earlier, we use a loop to see any action happening in our window, by using the msg;
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
This will read the msg, translate it then pass it on, now that we're done with that WinMain function we should return a value aight xD ?
return (int) msg.wParam;
}
here we are casting msg.wParam as an integer..
so our WinMain source is now
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Static Control" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("Our first window xD"),
WS_VISIBLE|WS_OVERLAPPEDWINDOW,
100, 100, 500, 500, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
oh my bad btw, WS_VISIBLE is a property, it tells the window to be visible, WS_OVERLAPPEDWINDOW puts the minimize,maximize and close button. kay ? kay.now to our new function to handle the sent 'msg'
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )This is the Window procedure function, that handles any procedure done by the window,
HWND = handle,
UINT msg , is the msg we used earlier xD
so to read the message we're going to use a switch statement,
switch(msg)
{
case WM_CREATE:
{
break;
}
case WM_COMMAND:
{}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Alright i rammed them all at once but i'm sleepy, so case WM_CREATE is if the window was successfully created, here you can place if you want to create any more textboxs or anythihng.case WM_COMMAND handles if any commands were given..
case WM_DESTROY is if the window was " DESTROYED "
I will write another part on handling WM_COMMAND xD
basically after reading this, you should know how a windowed program works, but not how to create one, after my second tutorial maybe you'll be able to create your own :)
If you enjoyed it, thank me with a +rep ^_^
Edited by Moudi, 26 March 2010 - 01:01 AM.


Sign In
Create Account


Back to top










