Author : Vivek Mohan
Page : << Previous 2 Next >>
CW_USEDEFAULT , CW_USEDEFAULT , 240 , 120 ,
Argument 5 , 6 , 7 , 8 : Specify the window's co-ordinates and dimensions. The first two , the X and Y co-ords and the second two the width and the height of the window. The CW_USEDEFAULT specifies that the window is free to choose its position. You can also specify your own integer values
NULL , NULL ,
Argument 9 , 10 : are the handles to the Parent Window and Menu. Since our window has no parent or a menu the values are set to NULL.
hInstance , NULL
Argument 11 , 12 : are the handle to the application instance and the creation data which is the data that can be sent to the window being created , respectively currently NULL.
);
End of function
If handle to the newly created window is null , show an error and then exit
if(hwnd == NULL)
{
MessageBox(NULL , "Failed to create window"
, "Error"
, MB_ICONEXCLAMATION | MB_OK );
return 0;
}
DRAWING THE WINDOW
==================
ShowWindow (hwnd,nCmdShow);
UpdateWindow(hwnd);
Now that the window has been created , it needs to be drawn and then updated to make sure that it is properly drawn. The ShowWindow(...) command draws the window The parameters are the handle to the window we created and the parameter nCmdShow which is one of the arguments of WinMain(..). The nCmdShow specifies the state of the window. Through the command line arguments it can be specified wether the window should be minimized , maximized or visible...
You can use other values sch as SW_SHOWNORMAL or SW_SHOWMAXIMIZED or SW_SHOWMINIMIZED etc.
MESSAGE LOOPING
===============
MSG Msg;
while(GetMessage(&Msg,NULL,0,0))
The message looping is like an engine which drives the windows application.
What makes the window just stay right where it is until you either move it or close it ? After the registering and window creation this is the point where the real stuff takes place.
The Basic Job Of the Message Loop
---------------------------------
1 . get message from the message queue , if none wait for one.
2 . translate the message
3 . dispatch the message to the window or whereever applicable
Messages are generated by the system whenever you move the mouse , click or press a key on the keyboard. These messages are added to a message queue. The GetMessage(...) function gets one such message at a time and returns it.
If no message is available in the message queue then GetMessage(..) blocks or waits till a message is generated.
{
TranslateMessage(&Msg);
The TranslateMessage(..) function does additional processing.
DispatchMessage (&Msg);
This function after processing the messages dispatches the messages , where it is meant to be so. It may be to the window we created or to the system or another program.
}
return Msg.wParam;
}
WINDOW PROCEDURE
================
LRESULT CALLBACK WndProc (HWND hwnd , UINT msg,WPARAM wParam , LPARAM lParam)
{
This is the definition of the function WndProc we protoyped a few pages back If you remeber while registering the window class , we set the lpfnWndProc as the below function. This means that the procedure for the window is the function WndProc(...). The function accepts the message and processes according to what it is (ie what we decide).
This function can be the Window procedure for the many windows and hence the handle to the window dispatching the message is passed.
switch(msg)
Switch between different cases of message
{
case WM_CLOSE:
The message is to close the window This message is sent when the user clicks on the X button on the top-right hand corner of the window or presses Alt+F4.
You can add the MessageBox(...) function here and experiment
DestroyWindow (hwnd);
The destroy window function sends the WM_DESTROY mesage to the window This message is handled as another case below
break;
case WM_DESTROY:
This message destroys the window and all its child windows , before removing the window from the system.
PostQuitMessage(0);
Since we have only one window and no child windows , we just Post a quit message.
This is done by the function PosQuitMessage(..) function , which sends a message WM_QUIT. But this is not processed by this function. The GetMessage(..) function when gets this message , returns false and the message loop ends.
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
This part is not so important right now.
}
return 0;
}
Thats it. I hope you have liked this small effort to help all you programmers out there new to win programming and wanting to create a window. Please send your feed back to mailvivek27@sify.com .
Copyright : You are free to distribute this article as long as you don't use it for profitable purposes (as if you can) ,and all the messages are retained.
win_code.cpp
// Author : Vivek Mohan
// E-Mail : mailvivek27@sify.com
// Home : http://compgurux.tripod.com/
# include <windows.h>
// The Class Name
const char g_szClassName [] = "MyWindowClass";
// The window procedure
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance ,
LPSTR lpCmdLine , int nCmdShow )
{
WNDCLASSEX wc; // Window Class
// Setting the window properties
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc ;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
// Register the class
if(!RegisterClassEx(&wc))
{ MessageBox(NULL , "Failed to register window class" ,
"Error" , MB_ICONEXCLAMATION | MB_OK );
return 0;
}
// Create the window
HWND hwnd;
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE ,
g_szClassName ,
"Title Of Me Window !" ,
WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT , CW_USEDEFAULT , 240 , 120 ,
NULL , NULL ,
hInstance , NULL
);
if(hwnd == NULL)
Page : << Previous 2 Next >>