This commit was manufactured by cvs2svn to create branch 'captive'.
[reactos.git] / apps / tests / wm_paint / Listing1_1.cpp
1
2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 1 - Listing 1.1 (WM_PAINT Demo)
5 //
6 // Created by Damon Chandler <dmc27@ee.cornell.edu>
7 // Updates can be downloaded at: <www.coriolis.com>
8 //
9 // Please do not hesistate to e-mail me at dmc27@ee.cornell.edu 
10 // if you have any questions about this code.
11 // ------------------------------------------------------------------
12
13
14 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
15 #include <windows.h>
16 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
17
18
19 const char* WndClassName = "GMainWnd";
20 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, 
21    LPARAM LParam);
22
23
24 int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE HPrevInstance,
25     LPTSTR lpCmdLine, int nCmdShow)
26 {
27    WNDCLASS wc;
28    memset(&wc, 0, sizeof(WNDCLASS));
29     
30    wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
31    wc.lpfnWndProc = MainWndProc;
32    wc.hInstance = HInstance;
33    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
34    wc.hbrBackground = 
35       reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
36    wc.lpszClassName = WndClassName;
37
38    if (RegisterClass(&wc))
39    {
40       HWND HWnd = 
41          CreateWindow(WndClassName, TEXT("WM_PAINT Demo"),
42                       WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_VISIBLE,
43                       CW_USEDEFAULT, CW_USEDEFAULT, 200, 150,
44                       NULL, NULL, HInstance, NULL);
45                                  
46       if (HWnd)
47       {
48          ShowWindow(HWnd, nCmdShow);
49          UpdateWindow(HWnd);
50
51          MSG msg;
52          while (GetMessage(&msg, NULL, 0, 0))
53          {
54              TranslateMessage(&msg);
55              DispatchMessage(&msg);
56          }
57       }
58     }
59     return 0;
60 }
61 //------------------------------------------------------------------
62
63
64 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, 
65    LPARAM LParam)
66 {
67    switch (Msg)
68    {
69       case WM_PAINT:
70       {
71          // determine the invalidated area of the window            
72          RECT RUpdate;            
73          GetUpdateRect(HWnd, &RUpdate, false);
74
75          // grab a handle to our window's
76          // common display device context
77          HDC Hdc = GetDC(HWnd);
78 #if 0
79          try
80 #endif
81          {
82             RECT RClient;
83             GetClientRect(HWnd, &RClient);
84
85             // set the clipping region
86             IntersectClipRect(Hdc, RUpdate.left, RUpdate.top, 
87                               RUpdate.right, RUpdate.bottom);
88
89             // fill the client area with the background brush
90             HBRUSH HBrush = 
91                reinterpret_cast<HBRUSH>(
92                   GetClassLong(HWnd, GCL_HBRBACKGROUND)
93                   );
94             FillRect(Hdc, &RClient, HBrush);
95             
96             // render the persistent text
97             const char* text = "Persistent Text"; 
98             SetTextColor(Hdc, PALETTERGB(0, 0, 255));
99             DrawText(Hdc, text, strlen(text), &RClient,
100                      DT_CENTER | DT_VCENTER | DT_SINGLELINE);
101          }
102 #if 0
103          catch (...)
104 #endif
105          {
106             // release the device context
107             ReleaseDC(HWnd, Hdc);
108
109             // validate the update area            
110             ValidateRect(HWnd, &RUpdate);
111          }
112          // release the device context
113          ReleaseDC(HWnd, Hdc);
114
115          // validate the update area            
116          ValidateRect(HWnd, &RUpdate);
117
118          break;
119       }
120       case WM_DESTROY:
121       {
122          PostQuitMessage(0);
123          return 0;
124       }
125    }
126    return DefWindowProc(HWnd, Msg, WParam, LParam);
127 }
128 //------------------------------------------------------------------
129