ReactOS  0.4.13-dev-544-gede3fdd
patblt.cpp
Go to the documentation of this file.
1 
2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 2 - Listing 2.1 (PatBlt Tracking Rect 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 
20 const char* WndClassName = "GMainWnd";
22  LPARAM LParam);
23 
24 
26  int nCmdShow)
27 {
28  HInst = HInstance;
29 
30  WNDCLASS wc;
31  memset(&wc, 0, sizeof(WNDCLASS));
32 
36  wc.hInstance = HInstance;
38  wc.hbrBackground = static_cast<HBRUSH>(
40  );
41 
42  if (RegisterClass(&wc))
43  {
44  HWND HWnd =
46  TEXT("PatBlt Tracking Rect Demo"),
49  CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
50  NULL, NULL, HInst, NULL);
51 
52  if (HWnd)
53  {
54  ShowWindow(HWnd, nCmdShow);
55  UpdateWindow(HWnd);
56 
57  MSG msg;
58  while (GetMessage(&msg, NULL, 0, 0))
59  {
62  }
63  }
64  }
65  return 0;
66 }
67 //------------------------------------------------------------------
68 
69 
70 // image related
73 const char* filename = "PENGUIN.BMP";
74 RECT RImage = {225, 110, 225, 110};
75 
76 // tracking related
77 bool is_tracking = false;
79 POINT PMouse = {0, 0};
80 RECT RTrack = {0, 0, 0, 0};
81 const int line_width = 5;
82 
83 
84 // utility function to map to/from window coordinates
85 void MapRect(IN HWND HWndFrom, IN HWND HWndTo, IN OUT RECT& RMap)
86 {
88  HWndFrom, HWndTo,
89  reinterpret_cast<LPPOINT>(&RMap), 2
90  );
91 }
92 //------------------------------------------------------------------
93 
94 
95 // utility function that uses the PatBlt function to
96 // render a tracking rectangle
97 void RenderTrackingRect(IN HDC HDestDC, IN const RECT& RRender)
98 {
99  const int width = RRender.right - RRender.left;
100  const int height = RRender.bottom - RRender.top;
101  const DWORD dwROP3 = DSTINVERT; // experiment with others
102 
103  // render top bar
104  PatBlt(HDestDC,
105  RRender.left, RRender.top,
106  width, line_width,
107  dwROP3);
108  // render bottom bar
109  PatBlt(HDestDC,
110  RRender.left, RRender.bottom - line_width,
111  width, line_width,
112  dwROP3);
113  // render left bar
114  PatBlt(HDestDC,
115  RRender.left, RRender.top + line_width,
116  line_width, height - (2 * line_width),
117  dwROP3);
118  // render right bar
119  PatBlt(HDestDC,
120  RRender.right - line_width, RRender.top + line_width,
121  line_width, height - (2 * line_width),
122  dwROP3);
123 
124 }
125 //------------------------------------------------------------------
126 
127 
129  LPARAM LParam)
130 {
131  switch (Msg)
132  {
133  case WM_CREATE:
134  {
135  // create a memory DC
137  if (HMemDC)
138  {
139  // load the penguin bitmap
140  HBITMAP HBmp = static_cast<HBITMAP>(
143  );
144  if (HBmp)
145  {
146  // get the bitmap's dimensions
147  BITMAP bmp;
148  if (GetObject(HBmp, sizeof(BITMAP), &bmp))
149  {
150  RImage.right += bmp.bmWidth;
151  RImage.bottom += bmp.bmHeight;
152 
153  // realize the bitmap
154  HOldBmp = static_cast<HBITMAP>(
156  );
157  }
158  else DeleteObject(HBmp);
159  }
160  }
161  break;
162  }
163  case WM_LBUTTONDOWN:
164  {
165  PMouse.x = LOWORD(LParam);
166  PMouse.y = HIWORD(LParam);
167 
168  RECT RClient;
169  if (PtInRect(&RImage, PMouse) &&
170  GetClientRect(HWnd, &RClient))
171  {
172  MapRect(HWnd, HWND_DESKTOP, RClient);
173  ClipCursor(&RClient);
174 
175  // grab a handle to the screen DC and clip
176  // all output to the client area of our window
177  HScreenDC = GetDC(NULL);
178  HRGN HClipRgn = CreateRectRgnIndirect(&RClient);
179  SelectClipRgn(HScreenDC, HClipRgn);
180  DeleteObject(HClipRgn);
181 
182  CopyRect(&RTrack, &RImage);
183  MapRect(HWnd, HWND_DESKTOP, RTrack);
184 
185  // render the first tracking rect
187  is_tracking = true;
188  }
189  break;
190  }
191  case WM_MOUSEMOVE:
192  {
193  if (HScreenDC && is_tracking)
194  {
195  POINT PCurrent = {LOWORD(LParam), HIWORD(LParam)};
196  const int dX = PCurrent.x - PMouse.x;
197  const int dY = PCurrent.y - PMouse.y;
198 
199  // erase the previous rectangle
201  // update the postion
202  OffsetRect(&RTrack, dX, dY);
203  // render the new tracking rectangle
205 
206  // update the mouse position
207  memcpy(&PMouse, &PCurrent, sizeof(POINT));
208  }
209  break;
210  }
211  case WM_LBUTTONUP:
212  {
213  // clean up
214  if (is_tracking)
215  {
216  is_tracking = false;
219 
220  InvalidateRect(HWnd, &RImage, true);
221  CopyRect(&RImage, &RTrack);
222  MapRect(HWND_DESKTOP, HWnd, RImage);
223  InvalidateRect(HWnd, &RImage, true);
224 
225  ClipCursor(NULL);
226  }
227  break;
228  }
229  case WM_PAINT:
230  {
231  PAINTSTRUCT ps;
232  HDC Hdc = BeginPaint(HWnd, &ps);
233  try
234  {
235  //
236  // TODO: Add palette support...
237  //
238 
239  // render the penguin
240  BitBlt(Hdc, RImage.left, RImage.top,
243  HMemDC, 0, 0,
244  SRCCOPY);
245  }
246  catch (...)
247  {
248  EndPaint(HWnd, &ps);
249  }
250  EndPaint(HWnd, &ps);
251  break;
252  }
253  case WM_DESTROY:
254  {
255  // clean up
256  if (HOldBmp)
257  {
259  }
260  if (HMemDC)
261  {
262  DeleteDC(HMemDC);
263  }
264  PostQuitMessage(0);
265  return 0;
266  }
267  }
268  return DefWindowProc(HWnd, Msg, WParam, LParam);
269 }
270 //------------------------------------------------------------------
HGDIOBJ WINAPI GetStockObject(_In_ int)
#define IN
Definition: typedefs.h:38
GLint GLint GLsizei width
Definition: gl.h:1546
#define DispatchMessage
Definition: winuser.h:5631
BOOL WINAPI TranslateMessage(_In_ const MSG *)
int WINAPI SelectClipRgn(_In_ HDC, _In_opt_ HRGN)
long y
Definition: polytest.cpp:48
int WINAPI MapWindowPoints(_In_opt_ HWND hWndFrom, _In_opt_ HWND hWndTo, _Inout_updates_(cPoints) LPPOINT lpPoints, _In_ UINT cPoints)
#define WM_LBUTTONDOWN
Definition: winuser.h:1752
#define CreateWindow
Definition: winuser.h:5620
BOOL WINAPI OffsetRect(_Inout_ LPRECT, _In_ int, _In_ int)
BOOL WINAPI ClipCursor(_In_opt_ LPCRECT)
long x
Definition: polytest.cpp:48
HDC WINAPI GetDC(_In_opt_ HWND)
RECT RTrack
Definition: patblt.cpp:80
#define LR_LOADFROMFILE
Definition: winuser.h:1082
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
HRGN WINAPI CreateRectRgnIndirect(_In_ LPCRECT)
static HDC
Definition: imagelist.c:92
#define CALLBACK
Definition: compat.h:27
BOOL WINAPI UpdateWindow(_In_ HWND)
HBITMAP HOldBmp
Definition: patblt.cpp:72
LONG top
Definition: windef.h:292
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define CS_HREDRAW
Definition: winuser.h:648
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
UINT_PTR WPARAM
Definition: windef.h:207
HBRUSH hbrBackground
Definition: winuser.h:3123
const int line_width
Definition: patblt.cpp:81
LONG left
Definition: windef.h:291
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
BOOL WINAPI EndPaint(_In_ HWND, _In_ const PAINTSTRUCT *)
#define WS_CLIPCHILDREN
Definition: pedump.c:619
const char * filename
Definition: ioapi.h:135
LONG right
Definition: windef.h:293
static VOID NTAPI BitBlt(IN ULONG Left, IN ULONG Top, IN ULONG Width, IN ULONG Height, IN PUCHAR Buffer, IN ULONG BitsPerPixel, IN ULONG Delta)
Definition: vga.c:410
RECT RImage
Definition: patblt.cpp:74
CHAR * LPTSTR
Definition: xmlstorage.h:192
BOOL WINAPI PatBlt(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ DWORD)
int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE, LPTSTR, int nCmdShow)
Definition: patblt.cpp:25
void RenderTrackingRect(IN HDC HDestDC, IN const RECT &RRender)
Definition: patblt.cpp:97
HDC WINAPI BeginPaint(_In_ HWND, _Out_ LPPAINTSTRUCT)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
HCURSOR hCursor
Definition: winuser.h:3122
#define CW_USEDEFAULT
Definition: winuser.h:225
void MapRect(IN HWND HWndFrom, IN HWND HWndTo, IN OUT RECT &RMap)
Definition: patblt.cpp:85
HINSTANCE HInst
Definition: patblt.cpp:19
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
#define DefWindowProc
Definition: ros2win.h:31
#define WM_DESTROY
Definition: winuser.h:1591
#define DSTINVERT
Definition: wingdi.h:326
#define BLACK_BRUSH
Definition: wingdi.h:895
#define WS_CAPTION
Definition: pedump.c:624
#define CS_VREDRAW
Definition: winuser.h:653
#define LR_DEFAULTSIZE
Definition: winuser.h:1084
BOOL WINAPI InvalidateRect(_In_opt_ HWND, _In_opt_ LPCRECT, _In_ BOOL)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GetObject
Definition: wingdi.h:4446
BITMAP bmp
Definition: alphablend.c:62
#define WM_PAINT
Definition: winuser.h:1602
POINT PMouse
Definition: patblt.cpp:79
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
LPCSTR lpszClassName
Definition: winuser.h:3125
struct @1574 Msg[]
#define LoadCursor
Definition: winuser.h:5678
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
const char * WndClassName
Definition: patblt.cpp:20
UINT style
Definition: winuser.h:3116
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
#define TEXT(s)
Definition: k32.h:26
BOOL WINAPI DeleteDC(_In_ HDC)
HINSTANCE hInstance
Definition: winuser.h:3120
#define WM_LBUTTONUP
Definition: winuser.h:1753
Definition: bl.h:1331
#define HWND_DESKTOP
Definition: winuser.h:1195
unsigned int UINT
Definition: ndis.h:50
#define WM_MOUSEMOVE
Definition: winuser.h:1751
#define IMAGE_BITMAP
Definition: winuser.h:211
#define GetMessage
Definition: winuser.h:5656
bool is_tracking
Definition: patblt.cpp:77
HDC HMemDC
Definition: patblt.cpp:71
WNDPROC lpfnWndProc
Definition: winuser.h:3117
BOOL WINAPI PtInRect(_In_ LPCRECT, _In_ POINT)
#define msg(x)
Definition: auth_time.c:54
#define IDC_ARROW
Definition: winuser.h:682
#define OUT
Definition: typedefs.h:39
#define WM_CREATE
Definition: winuser.h:1590
BOOL WINAPI CopyRect(_Out_ LPRECT, _In_ LPCRECT)
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
#define HIWORD(l)
Definition: typedefs.h:246
LONG bottom
Definition: windef.h:294
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
Definition: patblt.cpp:128
#define LoadImage
Definition: winuser.h:5681
static HBITMAP
Definition: button.c:44
#define RegisterClass
Definition: winuser.h:5702
LONG_PTR LRESULT
Definition: windef.h:209
#define WS_VISIBLE
Definition: pedump.c:620
#define memset(x, y, z)
Definition: compat.h:39
#define LOWORD(l)
Definition: pedump.c:82
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define SRCCOPY
Definition: wingdi.h:332
#define APIENTRY
Definition: api.h:79
HBITMAP HBmp
HDC HScreenDC
Definition: patblt.cpp:78