update for HEAD-2003091401
[reactos.git] / apps / tests / wm_paint / wm_paint.c
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    MSG msg;
28    WNDCLASS wc;
29    memset(&wc, 0, sizeof(WNDCLASS));
30     
31    wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
32    wc.lpfnWndProc = MainWndProc;
33    wc.hInstance = HInstance;
34    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
35    wc.hbrBackground = (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          while (GetMessage(&msg, NULL, 0, 0))
52          {
53              TranslateMessage(&msg);
54              DispatchMessage(&msg);
55          }
56       }
57     }
58     return 0;
59 }
60 //------------------------------------------------------------------
61
62
63 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, 
64    LPARAM LParam)
65 {
66   const char* text = "Persistent Text"; 
67
68    switch (Msg)
69    {
70       case WM_PAINT:
71       {
72          // determine the invalidated area of the window            
73          RECT RUpdate;
74          HDC Hdc;
75          GetUpdateRect(HWnd, &RUpdate, NULL);
76
77          // grab a handle to our window's
78          // common display device context
79          Hdc = GetDC(HWnd);
80 #if 0
81          try
82 #endif
83          {
84             RECT RClient;
85             GetClientRect(HWnd, &RClient);
86
87             // set the clipping region
88             IntersectClipRect(Hdc, RUpdate.left, RUpdate.top, 
89                               RUpdate.right, RUpdate.bottom);
90
91             // fill the client area with the background brush
92             //HBRUSH HBrush = 
93                //reinterpret_cast<HBRUSH>
94                   (HBRUSH)(GetClassLong(HWnd, GCL_HBRBACKGROUND)
95                   );
96             FillRect(Hdc, &RClient, NULL);
97             
98             // render the persistent text
99             SetTextColor(Hdc, PALETTERGB(0, 0, 255));
100             DrawText(Hdc, text, strlen(text), &RClient,
101                      DT_CENTER | DT_VCENTER | DT_SINGLELINE);
102          }
103 #if 0
104          catch (...)
105 #endif
106          {
107             // release the device context
108             ReleaseDC(HWnd, Hdc);
109
110             // validate the update area            
111             ValidateRect(HWnd, &RUpdate);
112          }
113          // release the device context
114          ReleaseDC(HWnd, Hdc);
115
116          // validate the update area            
117          ValidateRect(HWnd, &RUpdate);
118
119          break;
120       }
121       case WM_DESTROY:
122       {
123          PostQuitMessage(0);
124          return 0;
125       }
126    }
127    return DefWindowProc(HWnd, Msg, WParam, LParam);
128 }
129 //------------------------------------------------------------------
130