ReactOS 0.4.15-dev-8061-g57b775e
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
20const 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"),
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
73const char* filename = "PENGUIN.BMP";
74RECT RImage = {225, 110, 225, 110};
75
76// tracking related
77bool is_tracking = false;
79POINT PMouse = {0, 0};
80RECT RTrack = {0, 0, 0, 0};
81const int line_width = 5;
82
83
84// utility function to map to/from window coordinates
85void 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
97void 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,
107 dwROP3);
108 // render bottom bar
109 PatBlt(HDestDC,
110 RRender.left, RRender.bottom - line_width,
112 dwROP3);
113 // render left bar
114 PatBlt(HDestDC,
115 RRender.left, RRender.top + line_width,
117 dwROP3);
118 // render right bar
119 PatBlt(HDestDC,
120 RRender.right - line_width, RRender.top + 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
178 HRGN HClipRgn = CreateRectRgnIndirect(&RClient);
179 SelectClipRgn(HScreenDC, HClipRgn);
180 DeleteObject(HClipRgn);
181
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);
223 InvalidateRect(HWnd, &RImage, true);
224
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
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 {
263 }
265 return 0;
266 }
267 }
268 return DefWindowProc(HWnd, Msg, WParam, LParam);
269}
270//------------------------------------------------------------------
#define msg(x)
Definition: auth_time.c:54
struct @1636 Msg[]
#define NULL
Definition: types.h:112
#define APIENTRY
Definition: api.h:79
#define CALLBACK
Definition: compat.h:35
static VOID BitBlt(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Width, _In_ ULONG Height, _In_reads_bytes_(Delta *Height) PUCHAR Buffer, _In_ ULONG BitsPerPixel, _In_ ULONG Delta)
Definition: common.c:57
unsigned long DWORD
Definition: ntddk_ex.h:95
pKey DeleteObject()
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
GLint GLint GLsizei width
Definition: gl.h:1546
const char * filename
Definition: ioapi.h:137
#define TEXT(s)
Definition: k32.h:26
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
BITMAP bmp
Definition: alphablend.c:62
static HBITMAP
Definition: button.c:44
static HDC
Definition: imagelist.c:92
unsigned int UINT
Definition: ndis.h:50
bool is_tracking
Definition: patblt.cpp:77
const char * WndClassName
Definition: patblt.cpp:20
HDC HMemDC
Definition: patblt.cpp:71
int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE, LPTSTR, int nCmdShow)
Definition: patblt.cpp:25
const int line_width
Definition: patblt.cpp:81
void RenderTrackingRect(IN HDC HDestDC, IN const RECT &RRender)
Definition: patblt.cpp:97
RECT RImage
Definition: patblt.cpp:74
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
Definition: patblt.cpp:128
HDC HScreenDC
Definition: patblt.cpp:78
HINSTANCE HInst
Definition: patblt.cpp:19
RECT RTrack
Definition: patblt.cpp:80
POINT PMouse
Definition: patblt.cpp:79
void MapRect(IN HWND HWndFrom, IN HWND HWndTo, IN OUT RECT &RMap)
Definition: patblt.cpp:85
HBITMAP HOldBmp
Definition: patblt.cpp:72
#define LOWORD(l)
Definition: pedump.c:82
#define WS_CAPTION
Definition: pedump.c:624
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
#define WS_VISIBLE
Definition: pedump.c:620
#define WS_CLIPCHILDREN
Definition: pedump.c:619
#define DefWindowProc
Definition: ros2win.h:31
#define memset(x, y, z)
Definition: compat.h:39
Definition: bl.h:1331
HBRUSH hbrBackground
Definition: winuser.h:3170
HINSTANCE hInstance
Definition: winuser.h:3167
HCURSOR hCursor
Definition: winuser.h:3169
UINT style
Definition: winuser.h:3163
LPCSTR lpszClassName
Definition: winuser.h:3172
WNDPROC lpfnWndProc
Definition: winuser.h:3164
long y
Definition: polytest.cpp:48
long x
Definition: polytest.cpp:48
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1829
#define IN
Definition: typedefs.h:39
#define HIWORD(l)
Definition: typedefs.h:247
#define OUT
Definition: typedefs.h:40
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
HGDIOBJ WINAPI GetStockObject(_In_ int)
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
#define SRCCOPY
Definition: wingdi.h:333
#define DSTINVERT
Definition: wingdi.h:327
BOOL WINAPI PatBlt(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ DWORD)
#define BLACK_BRUSH
Definition: wingdi.h:896
#define GetObject
Definition: wingdi.h:4468
HRGN WINAPI CreateRectRgnIndirect(_In_ LPCRECT)
BOOL WINAPI DeleteDC(_In_ HDC)
int WINAPI SelectClipRgn(_In_ HDC, _In_opt_ HRGN)
#define WM_PAINT
Definition: winuser.h:1620
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
#define CS_VREDRAW
Definition: winuser.h:658
BOOL WINAPI CopyRect(_Out_ LPRECT, _In_ LPCRECT)
#define IMAGE_BITMAP
Definition: winuser.h:211
BOOL WINAPI TranslateMessage(_In_ const MSG *)
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define LR_LOADFROMFILE
Definition: winuser.h:1092
#define WM_CREATE
Definition: winuser.h:1608
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
BOOL WINAPI ClipCursor(_In_opt_ LPCRECT)
#define CS_HREDRAW
Definition: winuser.h:653
#define IDC_ARROW
Definition: winuser.h:687
#define WM_MOUSEMOVE
Definition: winuser.h:1775
#define WM_LBUTTONDOWN
Definition: winuser.h:1776
int WINAPI MapWindowPoints(_In_opt_ HWND hWndFrom, _In_opt_ HWND hWndTo, _Inout_updates_(cPoints) LPPOINT lpPoints, _In_ UINT cPoints)
#define CreateWindow
Definition: winuser.h:5754
#define HWND_DESKTOP
Definition: winuser.h:1209
BOOL WINAPI PtInRect(_In_ LPCRECT, _In_ POINT)
#define GetMessage
Definition: winuser.h:5790
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
BOOL WINAPI EndPaint(_In_ HWND, _In_ const PAINTSTRUCT *)
BOOL WINAPI UpdateWindow(_In_ HWND)
#define LoadCursor
Definition: winuser.h:5812
HDC WINAPI GetDC(_In_opt_ HWND)
#define WM_LBUTTONUP
Definition: winuser.h:1777
#define CW_USEDEFAULT
Definition: winuser.h:225
#define LoadImage
Definition: winuser.h:5815
BOOL WINAPI OffsetRect(_Inout_ LPRECT, _In_ int, _In_ int)
#define RegisterClass
Definition: winuser.h:5836
#define WM_DESTROY
Definition: winuser.h:1609
#define LR_DEFAULTSIZE
Definition: winuser.h:1094
#define DispatchMessage
Definition: winuser.h:5765
BOOL WINAPI InvalidateRect(_In_opt_ HWND, _In_opt_ LPCRECT, _In_ BOOL)
HDC WINAPI BeginPaint(_In_ HWND, _Out_ LPPAINTSTRUCT)
HBITMAP HBmp
CHAR * LPTSTR
Definition: xmlstorage.h:192