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

DefWindowProc() Overview

DefWindowProc: DefWindowProc() does the default processing of messages defined in win32 sub-system. Application calls this function to those messages which does not have any processing inside the application. This ensures that application is processing every single message. Application calls the DefWindowProc() with the same parameters received by the window procedure. Win32 subsystem performs default action for the message when this is called.

DefWindowProc() prototype

LRESULT DefWindowProc(
  HWND hWnd,
  UINT Msg,
  WPARAM wParam,
  LPARAM lParam


  • hWnd - Handle to the window procedure that received the message.
  • Msg - Specifies the message.
  • wParam - Specifies additional message information. The content of this parameter depends on the value of the Msg parameter.
  • lParam - Specifies additional message information. The content of this parameter depends on the value of the Msg parameter.

Return Values

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

Windows message processing

Windows Event processing and DefWindowProc

Windows GUI subsystem is a server for all application runnding on the frontend desktop. Every application running acts as a client and process an infinite loop of message processing from windows GUI subsystem. It this Window proc is a single point callback. Application process events like keyboard / mouse events can handle individial functions. However each message should be processed and Window proc should return a value. So the remaining messages those are not handled should go to DefWindowProc(). DefWindowProc() is a call back to Win32 GUI server to handle default action for this message.

Source Code

#include <windows.h>
                         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));
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";
26    RegisterClass(&wc);
28     hWnd = CreateWindow("MyWin32Class", "DefWindowProc demo", WS_OVERLAPPEDWINDOW,
29        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
31    ShowWindow(hWnd, nCmdShow);
32    UpdateWindow(hWnd);
33    while (GetMessage(&msg, NULL, 0, 0)) 
34    {
35      DispatchMessage(&msg);
36    }
37    return 0;
38  }
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      /* Call DefWindowProc() as default */
49        return DefWindowProc(hWnd, message, wParam, lParam);
50     }
51     return 0;
52  }

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 Sun 27 Apr/2014 11:50:13(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.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  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.