How to write a program in order to create a window?
STEPS:
• We have a structure called WNDCLASS, which will store information about the windows, first we have to assign values to the variables of this structure.
• Check whether the values assigned to variables of WNDCLASS structure are valid or not by using a function called RegisterClass().
• Create a Window by the calling a function called CreateWindow();
• Even though we create a window it is not visible until and unless we use a function called ShowWindow()
First step:
Every window must come from a WNDCLASS structure.
We will have to fill up the WNDCLASS structure by assigning values to variables in WNDCLASS structure.
There are 10 variables in WNDCLASS
1) style --It specifies the style about how is the WNDCLASS object created.
2) lpfnWndProc—It specifies the name of the function that will receive and process the messages received by windows created from this class.
3) cbClsExtra—It stores the count extra bytes that may be required by the WNDCLASS structure
4) cbWndExtra—It stores the count of extra bytes that may be needed by windows created from this WNDCLASS.
5) hInstance—It is a handle to the application which has creates this WNDCLASS.
6) hIcon—It specifies the icon that will be displayed when windows created from this class are minimised.
7) hCursor—It specifies the mouse cursor that will be displayed in windows created from this class.
8) hbrBackground—It specifies the brush that will be used for painting the client area.
9) lpszClassName—It specifies the Class name.
10) lpszMenuName—It specifies the menu name that is to be displayed on the window.
Second step:
The second step is to verify whether the values, which are assigned to variables of WNDCLASS structure, are valid or not.
We use RegisterClass function for this purpose.
This function expects a parameter that will be the address of object of WND CLASS structure.
Third step:
After the class is registered successfully we have to create the window from that class.
We use CreateWindow for this purpose that should be provided with 11 parameters.
The declaration of the CreateFunction is as follows:
HWND CreateWindow (LPSTR lpclassname, LPSTR lpwindowname, DWORD dwStyle, int X , int y ,int nWidth,int nHeight, HWND Parent, HMENU menuname, HINSTANCE hInstance, PARAM param)
Thursday, February 21, 2008
MSG STRUCTURE
This is a structure, which is created by the windows operating system for every window as soon as a window is created.
It consists of 6 variables the declaration of the structure is as follows.
Struct tagMSG
{
HWND hWnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG;
PARAMETERS:
HWND hWnd = It specifies a handle to the window for which the message is generated.
UINT message = It specifies the actual message about the event that got generated.
WPARAM wParam and LPARAM lParam = These parameters hold some additional information about the message generated.
DWORD time = It specifies the time the message was sent or the time when the event is raised.
POINT x, y = It specifies the x, y co-ordinate of the mouse cursor.
MSG structure is the structure, which will store the information of the messages generated.
Messages are small packets of data that windows send to a running program to let it know that an event has occurred.
Windows generates message for every hardware event.
It passes these messages to the appropriate message queue.
The running program or application pulls messages from message queue using the message loop.
The message loop uses the GetMessage function to read messages.
It consists of 6 variables the declaration of the structure is as follows.
Struct tagMSG
{
HWND hWnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG;
PARAMETERS:
HWND hWnd = It specifies a handle to the window for which the message is generated.
UINT message = It specifies the actual message about the event that got generated.
WPARAM wParam and LPARAM lParam = These parameters hold some additional information about the message generated.
DWORD time = It specifies the time the message was sent or the time when the event is raised.
POINT x, y = It specifies the x, y co-ordinate of the mouse cursor.
MSG structure is the structure, which will store the information of the messages generated.
Messages are small packets of data that windows send to a running program to let it know that an event has occurred.
Windows generates message for every hardware event.
It passes these messages to the appropriate message queue.
The running program or application pulls messages from message queue using the message loop.
The message loop uses the GetMessage function to read messages.
Wednesday, February 20, 2008
GETMESSAGE, TRANSLATEMESSAGE and DispatchMessage Function
GETMESSAGE FUNCTION :
GetMessage () function gets messages from MSG structure.
The syntax of the function goes this way.
BOOL GetMessage (LPMSG lpMsg, HWND hWnd, WORD w1, WORD w2)
This function returns Boolean value.
PARAMETERS:
LPMSG lpMsg = It will specify the address of the MSG structure where this function should go to fetch the messages.
LPMSG stands for long pointer to MSG structure.
HWND hWnd = It is the handle to the window receiving messages.
WORD w1 and WORD w2 = They specify the lowest and highest value message to receive.
They are always set to 0.
After receiving messages, these messages should be translated into code which applications can understand.
So, we have a function called translate function to do the task of translating.
TRANSLATEMESSAGE FUNCTION:
The syntax of this function is as follows
TranslateMessage (MSG &msg.)
This function will take only one parameter that is address of the msg. structure.
This function is required only to interact with the keyboard.
After translating the messages, the messages should be processed.
Inorder to process the messages, they should be redirected or sent to the function which will do the job of processing.
We have a function called dispatch message function to do this task.
DISPATCHMESSAGE FUNCTION:
This function is used to send messages to appropriate processing function.
The syntax of this function is
DispatchMessage (MSG &msg.)
This function takes one parameter that is address to the MSG structure.
This function gets the name of the processing function from the second member variable of the WNDCLASS structure.
There are few functions, which do the job of processing the messages.
One of them is ProcessMessagefn.
GetMessage () function gets messages from MSG structure.
The syntax of the function goes this way.
BOOL GetMessage (LPMSG lpMsg, HWND hWnd, WORD w1, WORD w2)
This function returns Boolean value.
PARAMETERS:
LPMSG lpMsg = It will specify the address of the MSG structure where this function should go to fetch the messages.
LPMSG stands for long pointer to MSG structure.
HWND hWnd = It is the handle to the window receiving messages.
WORD w1 and WORD w2 = They specify the lowest and highest value message to receive.
They are always set to 0.
After receiving messages, these messages should be translated into code which applications can understand.
So, we have a function called translate function to do the task of translating.
TRANSLATEMESSAGE FUNCTION:
The syntax of this function is as follows
TranslateMessage (MSG &msg.)
This function will take only one parameter that is address of the msg. structure.
This function is required only to interact with the keyboard.
After translating the messages, the messages should be processed.
Inorder to process the messages, they should be redirected or sent to the function which will do the job of processing.
We have a function called dispatch message function to do this task.
DISPATCHMESSAGE FUNCTION:
This function is used to send messages to appropriate processing function.
The syntax of this function is
DispatchMessage (MSG &msg.)
This function takes one parameter that is address to the MSG structure.
This function gets the name of the processing function from the second member variable of the WNDCLASS structure.
There are few functions, which do the job of processing the messages.
One of them is ProcessMessagefn.
PROCESSMESSAGEFN, Explanation Function
PROCESSMESSAGEFN FUNCTION:
This function will process messages which it receives from DispatchMessage () function.
The syntax of this function is as follows
long PASCAL ProcessMessagefn (HWND hWnd, UINT message, WPARAM wParam, and LPARAM lParam)
PARAMETERS:
HWND hWnd: It is handle to the window for which the message has been generated.
UINT message: It is the message that has been generated.
WPARAM wParam and LPARAM lParam: It is the additional information about the message.
This will contain set of conditions that will do the job of processing the messages.
Consider the following example:
long PASCAL ProcessMessagefn(HWND hWnd, UINT message,WPARAM wParam LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
default :
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
NOTE: This is a part of the program but not the complete program, and will give us the output if at all this is executed as it is.
Explanation of the program:
As already been explained ProcessMessagefn function is used to do the job of processing the messages.
So it contains a set of switch to handles different types of messages.
Whenever an event is generated windows operating system sent a message to the application message queue.
They are a set of window messages, which have their own definition, and they are raised in response to any event.
For example in the above example we have WM_DESTROY
This message is generated by windows operating system, where WM stands for window message. It is generated whenever we click on the close button or quit from the window.
This is the message sent by windows to the application to destroy the window crated by it.
Our application should be able to handle all the messages sent by the windows.
So, to terminate or destroy the application window and to return control back to the operating system we use a function that is PostQuitMessage (0);
This function release the entire memory allocated to all windows of that particular application and returns the value 0 or the control to the operating system.
This function will process messages which it receives from DispatchMessage () function.
The syntax of this function is as follows
long PASCAL ProcessMessagefn (HWND hWnd, UINT message, WPARAM wParam, and LPARAM lParam)
PARAMETERS:
HWND hWnd: It is handle to the window for which the message has been generated.
UINT message: It is the message that has been generated.
WPARAM wParam and LPARAM lParam: It is the additional information about the message.
This will contain set of conditions that will do the job of processing the messages.
Consider the following example:
long PASCAL ProcessMessagefn(HWND hWnd, UINT message,WPARAM wParam LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
default :
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
NOTE: This is a part of the program but not the complete program, and will give us the output if at all this is executed as it is.
Explanation of the program:
As already been explained ProcessMessagefn function is used to do the job of processing the messages.
So it contains a set of switch to handles different types of messages.
Whenever an event is generated windows operating system sent a message to the application message queue.
They are a set of window messages, which have their own definition, and they are raised in response to any event.
For example in the above example we have WM_DESTROY
This message is generated by windows operating system, where WM stands for window message. It is generated whenever we click on the close button or quit from the window.
This is the message sent by windows to the application to destroy the window crated by it.
Our application should be able to handle all the messages sent by the windows.
So, to terminate or destroy the application window and to return control back to the operating system we use a function that is PostQuitMessage (0);
This function release the entire memory allocated to all windows of that particular application and returns the value 0 or the control to the operating system.
DEFWINDOWPROC FUNCTION
DEFWINDOWPROC FUNCTION:
Our application created by us may not be able to handle or process all the messages it receives from MSG structure through GetMessage function, So such unprocessed messages should be sent for default processing.
This default processing is done by a function DefWindowProc ().
The syntax of the function is:
DefWindowProc (HWND hWnd, UINT message, WPARAM wParam , LPARAM lParam)
This function will take the same four parameters as ProcessMessagefn because those are required to process any message.
Our application created by us may not be able to handle or process all the messages it receives from MSG structure through GetMessage function, So such unprocessed messages should be sent for default processing.
This default processing is done by a function DefWindowProc ().
The syntax of the function is:
DefWindowProc (HWND hWnd, UINT message, WPARAM wParam , LPARAM lParam)
This function will take the same four parameters as ProcessMessagefn because those are required to process any message.
Sample Windows Application
A Sample Windows Application :
#include
long PASCAL ProcessMessagefn(HWND,UINT,WPARAM,LPARAM);
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASS wc;
MSG msg;
MessageBox(0," Welcome to our Application","Application",MB_OK);
if(!hPrevInstance)
{
wc.style=NULL;
wc.lpfnWndProc=ProcessMessagefn;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance =hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName="MyClass";
if(!RegisterClass(&wc))
{
MessageBox (0,"Could not register the WNDCLASS wc","Application", MB_OK);
return FALSE;
}
}
hWnd=CreateWindow("MyClass","Hello world", WS_OVERLAPPEDWINDOW, 10,10,200,300,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox (0,"could not create window","Application", MB_OK);
return FALSE;
}
ShowWindow (hWnd, nCmdShow);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage (&msg.);
DispatchMessage (&msg.);
}
MessageBox(0,"Application terminating","Application",MB_OK);
return 0;
}
long PASCAL ProcessMessagefn(HWND hWnd ,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
#include
long PASCAL ProcessMessagefn(HWND,UINT,WPARAM,LPARAM);
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASS wc;
MSG msg;
MessageBox(0," Welcome to our Application","Application",MB_OK);
if(!hPrevInstance)
{
wc.style=NULL;
wc.lpfnWndProc=ProcessMessagefn;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance =hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName="MyClass";
if(!RegisterClass(&wc))
{
MessageBox (0,"Could not register the WNDCLASS wc","Application", MB_OK);
return FALSE;
}
}
hWnd=CreateWindow("MyClass","Hello world", WS_OVERLAPPEDWINDOW, 10,10,200,300,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox (0,"could not create window","Application", MB_OK);
return FALSE;
}
ShowWindow (hWnd, nCmdShow);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage (&msg.);
DispatchMessage (&msg.);
}
MessageBox(0,"Application terminating","Application",MB_OK);
return 0;
}
long PASCAL ProcessMessagefn(HWND hWnd ,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
STEPS TO CREATE AND EXECUTE A NEW PROGRAM
STEPS TO CREATE AND EXECUTE A NEW PROGRAM :
The steps consists of four steps
* Creating a source file
* Compiling the source file or creating an .obj file
* Building or linking or creating an exe file.
* Executing the exe file.
CREATING A SOURCE FILE:
* Click on the Start and select Programs and then select the option VC++.
* As soon as new window opens Click on the FILE option and click on theNEW option.
* Select the PROJECTS tab and select Win32 Application and enter a project name.
* Click on ok.
* Click on the FILE option and click on the NEW option.
* Select the FILE tab and Click the option TEXT FILE, then enter the file name and click on ok.
* Save the .txt file with an extension of .c
* Right click on the file and select on the option insert file into project and click the project name.
* Write down the code and save it.
The steps consists of four steps
* Creating a source file
* Compiling the source file or creating an .obj file
* Building or linking or creating an exe file.
* Executing the exe file.
CREATING A SOURCE FILE:
* Click on the Start and select Programs and then select the option VC++.
* As soon as new window opens Click on the FILE option and click on theNEW option.
* Select the PROJECTS tab and select Win32 Application and enter a project name.
* Click on ok.
* Click on the FILE option and click on the NEW option.
* Select the FILE tab and Click the option TEXT FILE, then enter the file name and click on ok.
* Save the .txt file with an extension of .c
* Right click on the file and select on the option insert file into project and click the project name.
* Write down the code and save it.
Subscribe to:
Posts (Atom)