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

A standard windows program contains the following minimal steps:

  1. Define a Custom Window Class Define your own window class,
  2. Define a WndProc() callback for this the window This call back should handle all necessary messages and must pass those messages to DefWindowProc() which it is not handling. This message handler should also have a exit path like when user exits the window by clicking cross button or Alt+F4 from the keyboard,
  3. Register the Window Class Register your class so that you can use the name with CreateWindow() call,
  4. Create the registered window Create the main overlapped window,
  5. Show and Update Window Show and update the window as per user's show value,
  6. Wait for the user messages and dispatch - An infinite loop fetches messages from message queue and dispatch it to window procedure.

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 Sun 27 Apr/2014 05:46:32(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.