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 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 const char* WndClassName
= "GMainWnd";
34 LRESULT CALLBACK
MainWndProc(HWND HWnd
, UINT Msg
, WPARAM WParam
,
38 int APIENTRY
WinMain(HINSTANCE HInstance
, HINSTANCE HPrevInstance
,
39 LPTSTR lpCmdLine
, int nCmdShow
)
42 HPrevInst
= HPrevInstance
;
46 memset(&wc
, 0, sizeof(WNDCLASS
));
48 wc
.style
= CS_VREDRAW
| CS_HREDRAW
| CS_DBLCLKS
;
49 wc
.lpfnWndProc
= MainWndProc
;
50 wc
.hInstance
= HInstance
;
51 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
53 reinterpret_cast<HBRUSH
>(COLOR_BTNFACE
+ 1);
54 wc
.lpszClassName
= WndClassName
;
56 if (RegisterClass(&wc
))
59 CreateWindow(WndClassName
,
60 TEXT("WM_ERASEBKGND Demo"),
61 WS_OVERLAPPEDWINDOW
| WS_CAPTION
|
62 WS_VISIBLE
| WS_CLIPSIBLINGS
,
63 CW_USEDEFAULT
, CW_USEDEFAULT
, 205, 85,
64 NULL
, NULL
, HInstance
, NULL
);
68 ShowWindow(HWnd
, nCmdShow
);
72 while (GetMessage(&msg
, NULL
, 0, 0))
74 TranslateMessage(&msg
);
75 DispatchMessage(&msg
);
81 //------------------------------------------------------------------
84 // static text and bitmap-related variables
87 HBITMAP HBmp
, HOldBmp
;
88 const char* filename
= "BACKBITMAP.BMP";
90 LRESULT CALLBACK
MainWndProc(HWND HWnd
, UINT Msg
, WPARAM WParam
,
98 CreateWindow(TEXT("STATIC"), TEXT("Static Text"),
99 WS_CHILD
| WS_VISIBLE
| SS_CENTER
,
101 HWnd
, NULL
, HInst
, NULL
);
103 // create a memory DC compatible with the screen
104 HMemDC
= CreateCompatibleDC(NULL
);
107 // load a DDB from file
108 HBmp
= static_cast<HBITMAP
>(
109 LoadImage(HInst
, filename
, IMAGE_BITMAP
,
110 0, 0, LR_LOADFROMFILE
)
114 // associate the DDB with the memory DC
115 HOldBmp
= static_cast<HBITMAP
>(
116 SelectObject(HMemDC
, HBmp
)
121 case WM_CTLCOLORSTATIC
:
123 if (reinterpret_cast<HWND
>(LParam
) == HStatic
)
125 HDC HStaticDC
= reinterpret_cast<HDC
>(WParam
);
126 SetBkMode(HStaticDC
, TRANSPARENT
);
128 return reinterpret_cast<LRESULT
>(
129 GetStockObject(NULL_BRUSH
)
137 if (GetObject(HBmp
, sizeof(BITMAP
), &bmp
))
140 GetClientRect(HWnd
, &RClient
);
142 HDC Hdc
= reinterpret_cast<HDC
>(WParam
);
143 SetStretchBltMode(Hdc
, COLORONCOLOR
);
146 // TODO: add palette handling code for
147 // palettized displays (see Chapter 9)...
150 // render the background image
151 StretchBlt(Hdc
, 0, 0,
152 RClient
.right
- RClient
.left
,
153 RClient
.bottom
- RClient
.top
,
154 HMemDC
, 0, 0, bmp
.bmWidth
, bmp
.bmHeight
,
165 DeleteObject(SelectObject(HMemDC
, HOldBmp
));
167 // free the memory DC
174 return DefWindowProc(HWnd
, Msg
, WParam
, LParam
);
176 //------------------------------------------------------------------