update for HEAD-2003091401
[reactos.git] / apps / tests / wm_erasebkgnd / wm_erasebkgnd.cpp
1
2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 2 - CD-ROM (WM_ERASEBKGND 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 // SYNOPSIS:                                               //
16 //   This sample project demonstrates how to render        //
17 //   a background image in response to the WM_ERASEBKGND   //
18 //   message.  It also shows how to create a transparent   //
19 //   static (text) control by handling the                 //
20 //   WM_CTLCOLORSTATIC message.                            //
21 //                                                         // 
22 //*********************************************************//
23
24
25 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
26 #include <windows.h>
27 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
28
29
30 HINSTANCE HInst;
31 const char* WndClassName = "GMainWnd";
32 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, 
33    LPARAM LParam);
34
35
36 int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE HPrevInstance,
37     LPTSTR lpCmdLine, int nCmdShow)
38 {
39    HInst = HInstance;
40
41    WNDCLASS wc;
42    memset(&wc, 0, sizeof(WNDCLASS));
43     
44    wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
45    wc.lpfnWndProc = MainWndProc;
46    wc.hInstance = HInstance;
47    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
48    wc.hbrBackground = 
49       reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
50    wc.lpszClassName = WndClassName;
51
52    if (RegisterClass(&wc))
53    {
54       HWND HWnd = 
55          CreateWindow(WndClassName, 
56                       TEXT("WM_ERASEBKGND Demo"),
57                       WS_OVERLAPPEDWINDOW | WS_CAPTION | 
58                       WS_VISIBLE | WS_CLIPSIBLINGS,
59                       CW_USEDEFAULT, CW_USEDEFAULT, 205, 85,
60                       NULL, NULL, HInstance, NULL);
61                                  
62       if (HWnd)
63       {
64          ShowWindow(HWnd, nCmdShow);
65          UpdateWindow(HWnd);
66
67          MSG msg;
68          while (GetMessage(&msg, NULL, 0, 0))
69          {
70              TranslateMessage(&msg);
71              DispatchMessage(&msg);
72          }      
73       }
74     }
75     return 0;
76 }
77 //------------------------------------------------------------------
78
79
80 // static text and bitmap-related variables
81 HWND HStatic;
82 HDC HMemDC;
83 HBITMAP HBmp, HOldBmp;
84 const char* filename = "BACKBITMAP.BMP";
85
86 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, 
87    LPARAM LParam)
88 {
89    switch (Msg)
90    {
91       case WM_CREATE:
92       {
93          HStatic = 
94             CreateWindow(TEXT("STATIC"), TEXT("Static Text"), 
95                          WS_CHILD | WS_VISIBLE | SS_CENTER, 
96                          10, 20, 175, 30, 
97                          HWnd, NULL, HInst, NULL);
98
99          // create a memory DC compatible with the screen
100          HMemDC = CreateCompatibleDC(NULL);
101          if (HMemDC)
102          {
103             // load a DDB from file
104             HBmp = static_cast<HBITMAP>(
105                LoadImage(HInst, filename, IMAGE_BITMAP, 
106                          0, 0, LR_LOADFROMFILE)
107                );
108             if (HBmp)
109             {
110                // associate the DDB with the memory DC
111                HOldBmp = static_cast<HBITMAP>(
112                   SelectObject(HMemDC, HBmp)
113                   );
114             }
115          }
116       }
117       case WM_CTLCOLORSTATIC:
118       {
119          if (reinterpret_cast<HWND>(LParam) == HStatic)
120          {
121             HDC HStaticDC = reinterpret_cast<HDC>(WParam);
122             SetBkMode(HStaticDC, TRANSPARENT);
123
124             return reinterpret_cast<LRESULT>(
125                GetStockObject(NULL_BRUSH)
126                );
127          }
128          break;
129       }
130       case WM_ERASEBKGND:
131       {
132          BITMAP bmp;
133          if (GetObject(HBmp, sizeof(BITMAP), &bmp))
134          {
135             RECT RClient;
136             GetClientRect(HWnd, &RClient);
137
138             HDC Hdc = reinterpret_cast<HDC>(WParam); 
139             SetStretchBltMode(Hdc, COLORONCOLOR);
140             
141             //
142             // TODO: add palette handling code for 
143             // palettized displays (see Chapter 9)...
144             //
145
146             // render the background image            
147             StretchBlt(Hdc, 0, 0, 
148                        RClient.right - RClient.left,
149                        RClient.bottom - RClient.top,
150                        HMemDC, 0, 0, bmp.bmWidth, bmp.bmHeight, 
151                        SRCCOPY);
152             return TRUE;
153          }
154          break;
155       }
156       case WM_DESTROY:
157       {
158          if (HBmp)
159          {
160             // free the bitmap
161             DeleteObject(SelectObject(HMemDC, HOldBmp));
162          }
163          // free the memory DC
164          DeleteDC(HMemDC);
165
166          PostQuitMessage(0);
167          return 0;
168       }
169    }
170    return DefWindowProc(HWnd, Msg, WParam, LParam);
171 }
172 //------------------------------------------------------------------
173
174
175