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