EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Message Processing

Windows applications are based on message queues. Windows operating system has event and graphical management server running as background system process. This process acts as message queue server and all windows applications acts as message queue clients. Application calls GetMessage to receive message from event server. This is a blocking call and it waits for a message to arrive. Operating system receives keyboard inputs and mouse movements or clicks or other system events. All these are events are passed to event and graphical management server. Server constructs a message object of type MSG and sends this to message queue client which is current application. There can be many applications running but server decides to send the message to the application whose top level window is focused for inputs. GetMessage blocking call in WinMain returns and this event MSG is further passed to DispatchMessage(). DispatchMessage() is a API which indirectly triggers a call back to window procedure which is registered by application with windows class. Application does not call this function directly, Win32 subsystem calls this function when application dispatches the particular message. GetMessage and DispatchMessage are generally placed in an infinite loop. It keeps receiving new messagea and further dispatches to wndproc till application exits.

Windows Event processing and Window procedure wndproc

WNDPROC

The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a place holder for the application-defined function name. Application processes only messages those are needed and for the rest, it passes to DefWindowProc() for default handling in Win32 subsystem.
LRESULT CALLBACK WindowProc(
  HWND hwnd,      /* handle to window */
  UINT uMsg,      /* message identifier */
  WPARAM wParam,  /* first message parameter */
  LPARAM lParam   /* second message parameter */
);

Parameters

  • hwnd - Handle to the window.
  • uMsg - Specifies the message.
  • wParam - Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter.
  • lParam -Specifies additional message information. The contents of this parameter depend on the value of the uMsg parameter.

Return Values

The return value is the result of the message processing and depends on the message sent.

Source Code

#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message, 
                         WPARAM wParam,
                         LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
10                       LPSTR     lpCmdLine,
11                       int       nCmdShow)
12  {
13    WNDCLASS wc;
14    MSG msg;
15    HWND hWnd;
16    ZeroMemory(&amp;wc, sizeof(WNDCLASS));
17 
18    wc.style           = CS_HREDRAW | CS_VREDRAW;
19    wc.lpfnWndProc     = (WNDPROC)WndProc;
20    wc.hInstance       = hInstance;
21    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
22    wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
23    wc.lpszClassName   = (LPCTSTR)"MyWin32Class";
24 
25 
26    RegisterClass(&wc);
27 
28     hWnd = CreateWindow("MyWin32Class", "Hello World!", WS_OVERLAPPEDWINDOW,
29        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
30 
31    ShowWindow(hWnd, nCmdShow);
32    UpdateWindow(hWnd);
33    while (GetMessage(&msg, NULL, 0, 0)) 
34    {
35      DispatchMessage(&msg);
36    }
37    return 0;
38  }
39 
40  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
41  {
42    switch (message) 
43    {
44      case WM_DESTROY:
45        PostQuitMessage(0);
46        break;
47      default:
48        return DefWindowProc(hWnd, message, wParam, lParam);
49     }
50     return 0;
51  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 10:45:49(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.