1*c2c66affSColin Finck
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck * Windows 2000 Graphics API Black Book
4*c2c66affSColin Finck * (BitBlt Bitmap Rendering Demo)
5*c2c66affSColin Finck *
6*c2c66affSColin Finck * Created by Damon Chandler <dmc27@ee.cornell.edu>
7*c2c66affSColin Finck * Updates can be downloaded at: <www.coriolis.com>
8*c2c66affSColin Finck *
9*c2c66affSColin Finck * Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
10*c2c66affSColin Finck * if you have any questions about this code.
11*c2c66affSColin Finck */
12*c2c66affSColin Finck
13*c2c66affSColin Finck
14*c2c66affSColin Finck #include <windows.h>
15*c2c66affSColin Finck #include <string.h>
16*c2c66affSColin Finck
17*c2c66affSColin Finck HINSTANCE HInst;
18*c2c66affSColin Finck const char* WndClassName = "GMainWnd";
19*c2c66affSColin Finck LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
20*c2c66affSColin Finck LPARAM LParam);
21*c2c66affSColin Finck
22*c2c66affSColin Finck
WinMain(HINSTANCE HInstance,HINSTANCE HPrevInstance,LPTSTR lpCmdLine,int nCmdShow)23*c2c66affSColin Finck int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE HPrevInstance,
24*c2c66affSColin Finck LPTSTR lpCmdLine, int nCmdShow)
25*c2c66affSColin Finck {
26*c2c66affSColin Finck WNDCLASS wc;
27*c2c66affSColin Finck MSG msg;
28*c2c66affSColin Finck
29*c2c66affSColin Finck HInst = HInstance;
30*c2c66affSColin Finck
31*c2c66affSColin Finck memset(&wc, 0, sizeof(WNDCLASS));
32*c2c66affSColin Finck
33*c2c66affSColin Finck wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
34*c2c66affSColin Finck wc.lpfnWndProc = MainWndProc;
35*c2c66affSColin Finck wc.hInstance = HInstance;
36*c2c66affSColin Finck wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
37*c2c66affSColin Finck /* wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1); */
38*c2c66affSColin Finck wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
39*c2c66affSColin Finck wc.lpszClassName = WndClassName;
40*c2c66affSColin Finck
41*c2c66affSColin Finck if (RegisterClass(&wc))
42*c2c66affSColin Finck {
43*c2c66affSColin Finck HWND HWnd =
44*c2c66affSColin Finck CreateWindow(
45*c2c66affSColin Finck WndClassName, TEXT("BitBlt Bitmap Rendering Demo"),
46*c2c66affSColin Finck WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION |
47*c2c66affSColin Finck WS_VISIBLE | WS_CLIPSIBLINGS,
48*c2c66affSColin Finck 0, 0, 220, 230,
49*c2c66affSColin Finck NULL, NULL, HInst, NULL
50*c2c66affSColin Finck );
51*c2c66affSColin Finck
52*c2c66affSColin Finck if (HWnd)
53*c2c66affSColin Finck {
54*c2c66affSColin Finck ShowWindow(HWnd, nCmdShow);
55*c2c66affSColin Finck UpdateWindow(HWnd);
56*c2c66affSColin Finck
57*c2c66affSColin Finck while (GetMessage(&msg, NULL, 0, 0))
58*c2c66affSColin Finck {
59*c2c66affSColin Finck TranslateMessage(&msg);
60*c2c66affSColin Finck DispatchMessage(&msg);
61*c2c66affSColin Finck }
62*c2c66affSColin Finck }
63*c2c66affSColin Finck }
64*c2c66affSColin Finck return 0;
65*c2c66affSColin Finck }
66*c2c66affSColin Finck
67*c2c66affSColin Finck /* image related */
68*c2c66affSColin Finck BITMAP bmp;
69*c2c66affSColin Finck LPCSTR filename = TEXT("lena.bmp");
70*c2c66affSColin Finck HDC HMemDC = NULL;
71*c2c66affSColin Finck HBITMAP HOldBmp = NULL;
72*c2c66affSColin Finck
MainWndProc(HWND HWnd,UINT Msg,WPARAM WParam,LPARAM LParam)73*c2c66affSColin Finck LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
74*c2c66affSColin Finck LPARAM LParam)
75*c2c66affSColin Finck {
76*c2c66affSColin Finck switch (Msg)
77*c2c66affSColin Finck {
78*c2c66affSColin Finck case WM_CREATE:
79*c2c66affSColin Finck {
80*c2c66affSColin Finck /* create a memory DC */
81*c2c66affSColin Finck HMemDC = CreateCompatibleDC(NULL);
82*c2c66affSColin Finck if (HMemDC)
83*c2c66affSColin Finck {
84*c2c66affSColin Finck /* load a bitmap from file */
85*c2c66affSColin Finck HBITMAP HBmp =
86*c2c66affSColin Finck /* static_cast<HBITMAP> */(
87*c2c66affSColin Finck LoadImage(HInst, MAKEINTRESOURCE(1000), IMAGE_BITMAP,
88*c2c66affSColin Finck 0, 0, 0)
89*c2c66affSColin Finck );
90*c2c66affSColin Finck if (HBmp)
91*c2c66affSColin Finck {
92*c2c66affSColin Finck /* extract dimensions of the bitmap */
93*c2c66affSColin Finck GetObject(HBmp, sizeof(BITMAP), &bmp);
94*c2c66affSColin Finck
95*c2c66affSColin Finck /* associate the bitmap with the memory DC */
96*c2c66affSColin Finck /* HOldBmp = static_cast<HBITMAP> */
97*c2c66affSColin Finck (SelectObject(HMemDC, HBmp)
98*c2c66affSColin Finck );
99*c2c66affSColin Finck }
100*c2c66affSColin Finck }
101*c2c66affSColin Finck }
102*c2c66affSColin Finck case WM_PAINT:
103*c2c66affSColin Finck {
104*c2c66affSColin Finck PAINTSTRUCT ps;
105*c2c66affSColin Finck const HDC Hdc = BeginPaint(HWnd, &ps);
106*c2c66affSColin Finck #if 0
107*c2c66affSColin Finck try
108*c2c66affSColin Finck #endif
109*c2c66affSColin Finck {
110*c2c66affSColin Finck
111*c2c66affSColin Finck /* TODO: add palette support (see Chapter 9)... */
112*c2c66affSColin Finck
113*c2c66affSColin Finck
114*c2c66affSColin Finck BitBlt(Hdc, 20, 15,
115*c2c66affSColin Finck bmp.bmWidth, bmp.bmHeight,
116*c2c66affSColin Finck HMemDC, 0, 0,
117*c2c66affSColin Finck SRCCOPY);
118*c2c66affSColin Finck }
119*c2c66affSColin Finck #if 0
120*c2c66affSColin Finck catch (...)
121*c2c66affSColin Finck #endif
122*c2c66affSColin Finck {
123*c2c66affSColin Finck EndPaint(HWnd, &ps);
124*c2c66affSColin Finck }
125*c2c66affSColin Finck EndPaint(HWnd, &ps);
126*c2c66affSColin Finck break;
127*c2c66affSColin Finck }
128*c2c66affSColin Finck case WM_DESTROY:
129*c2c66affSColin Finck {
130*c2c66affSColin Finck /* clean up */
131*c2c66affSColin Finck DeleteObject(SelectObject(HMemDC, HOldBmp));
132*c2c66affSColin Finck DeleteDC(HMemDC);
133*c2c66affSColin Finck
134*c2c66affSColin Finck PostQuitMessage(0);
135*c2c66affSColin Finck return 0;
136*c2c66affSColin Finck }
137*c2c66affSColin Finck }
138*c2c66affSColin Finck return DefWindowProc(HWnd, Msg, WParam, LParam);
139*c2c66affSColin Finck }
140