2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 2 - CD-ROM (WM_ERASEBKGND Demo)
6 // Created by Damon Chandler <dmc27@ee.cornell.edu>
7 // Updates can be downloaded at: <www.coriolis.com>
9 // Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
10 // if you have any questions about this code.
11 // ------------------------------------------------------------------
13 //*********************************************************//
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. //
22 //*********************************************************//
25 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
27 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 const char* WndClassName = "GMainWnd";
32 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
36 int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE HPrevInstance,
37 LPTSTR lpCmdLine, int nCmdShow)
42 memset(&wc, 0, sizeof(WNDCLASS));
44 wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
45 wc.lpfnWndProc = MainWndProc;
46 wc.hInstance = HInstance;
47 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
49 reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
50 wc.lpszClassName = WndClassName;
52 if (RegisterClass(&wc))
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);
64 ShowWindow(HWnd, nCmdShow);
68 while (GetMessage(&msg, NULL, 0, 0))
70 TranslateMessage(&msg);
71 DispatchMessage(&msg);
77 //------------------------------------------------------------------
80 // static text and bitmap-related variables
83 HBITMAP HBmp, HOldBmp;
84 const char* filename = "BACKBITMAP.BMP";
86 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
94 CreateWindow(TEXT("STATIC"), TEXT("Static Text"),
95 WS_CHILD | WS_VISIBLE | SS_CENTER,
97 HWnd, NULL, HInst, NULL);
99 // create a memory DC compatible with the screen
100 HMemDC = CreateCompatibleDC(NULL);
103 // load a DDB from file
104 HBmp = static_cast<HBITMAP>(
105 LoadImage(HInst, filename, IMAGE_BITMAP,
106 0, 0, LR_LOADFROMFILE)
110 // associate the DDB with the memory DC
111 HOldBmp = static_cast<HBITMAP>(
112 SelectObject(HMemDC, HBmp)
117 case WM_CTLCOLORSTATIC:
119 if (reinterpret_cast<HWND>(LParam) == HStatic)
121 HDC HStaticDC = reinterpret_cast<HDC>(WParam);
122 SetBkMode(HStaticDC, TRANSPARENT);
124 return reinterpret_cast<LRESULT>(
125 GetStockObject(NULL_BRUSH)
133 if (GetObject(HBmp, sizeof(BITMAP), &bmp))
136 GetClientRect(HWnd, &RClient);
138 HDC Hdc = reinterpret_cast<HDC>(WParam);
139 SetStretchBltMode(Hdc, COLORONCOLOR);
142 // TODO: add palette handling code for
143 // palettized displays (see Chapter 9)...
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,
161 DeleteObject(SelectObject(HMemDC, HOldBmp));
163 // free the memory DC
170 return DefWindowProc(HWnd, Msg, WParam, LParam);
172 //------------------------------------------------------------------