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