ReactOS 0.4.15-dev-7988-g06a3508
primitives.cpp
Go to the documentation of this file.
1
2// ------------------------------------------------------------------
3// Windows 2000 Graphics API Black Book
4// Chapter 5 - Listing 5.1 (Output Primitives 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#include <cassert>
17
18// for the MakeFont() function...
19#include "mk_font.h"
20//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21
22
24const char* WndClassName = "GMainWnd";
26 LPARAM LParam);
27
28
30 int nCmdShow)
31{
33
34 WNDCLASS wc;
35 memset(&wc, 0, sizeof(WNDCLASS));
36
40 wc.hInstance = hInst;
42 wc.hbrBackground = reinterpret_cast<HBRUSH>(
44 );
45
46 if (RegisterClass(&wc))
47 {
48 HWND hWnd =
50 WndClassName, TEXT("Output Primitives Demo"),
55 );
56
57 if (hWnd)
58 {
59 ShowWindow(hWnd, nCmdShow);
61
62 MSG msg;
63 while (GetMessage(&msg, NULL, 0, 0))
64 {
67 }
68 }
69 }
70 return 0;
71}
72//------------------------------------------------------------------
73
74
78 };
79
80void DrawPrimitive(IN HDC hDC, IN const RECT& RPrimitive,
81 IN OutPrimitive PrimitiveID)
82{
83 RECT R = RPrimitive;
84 InflateRect(&R, -10, -10);
85
86 switch (PrimitiveID)
87 {
88 case opLine:
89 {
90 const POINT PLine[] = {{R.left, R.top}, {R.right, R.bottom}};
91 Polyline(hDC, PLine, 2);
92 break;
93 }
94 case opBezier:
95 {
96 const POINT PControlPoints[] = {
97 {R.left, R.top},
98 {(R.right - R.left) / 2, R.top},
99 {(R.right - R.left) / 2, R.bottom},
100 {R.right, R.bottom}
101 };
102 PolyBezier(hDC, PControlPoints, 4);
103 break;
104 }
105 case opRectangle:
106 {
107 Rectangle(hDC, R.left, R.top, R.right, R.bottom);
108 break;
109 }
110 case opRoundRect:
111 {
112 RoundRect(hDC, R.left, R.top, R.right, R.bottom, 20, 20);
113 break;
114 }
115 case opEllipse:
116 {
117 Ellipse(hDC, R.left, R.top, R.right, R.bottom);
118 break;
119 }
120 case opArc:
121 {
122 const POINT PRads[] = {
123 {(R.right - R.left) / 3 + R.left, R.top},
124 {(R.right - R.left) / 3 + R.left, R.bottom}
125 };
126 Arc(hDC, R.left, R.top, R.right, R.bottom,
127 PRads[0].x, PRads[0].y, PRads[1].x, PRads[1].y);
128 break;
129 }
130 case opPie:
131 {
132 const POINT PRads[] = {
133 {(R.right - R.left) / 3 + R.left, R.top},
134 {(R.right - R.left) / 3 + R.left, R.bottom}
135 };
136 Pie(hDC, R.left, R.top, R.right, R.bottom,
137 PRads[0].x, PRads[0].y, PRads[1].x, PRads[1].y);
138 break;
139 }
140 case opChord:
141 {
142 const POINT PRads[] = {
143 {(R.right - R.left) / 3 + R.left, R.top},
144 {(R.right - R.left) / 3 + R.left, R.bottom}
145 };
146 Chord(hDC, R.left, R.top, R.right, R.bottom,
147 PRads[0].x, PRads[0].y, PRads[1].x, PRads[1].y);
148 break;
149 }
150 case opCustom:
151 {
152 const POINT PVertices[] = {
153 {R.left, (R.bottom - R.top) / 2 + R.top},
154 {(R.right - R.left) / 2 + R.left, R.top},
155 {R.right, (R.bottom - R.top) / 2 + R.top},
156 {(R.right - R.left) / 2 + R.left, R.bottom}
157 };
158 Polygon(hDC, PVertices, 4);
159 break;
160 }
161 }
162}
163//------------------------------------------------------------------
164
165
166HWND hListBox = NULL; // handle to the list box
167HBRUSH hListBrush = NULL; // handle to the list box brush
168HPEN hListPen = NULL; // handle to the list box pen
169HFONT hListFont = NULL; // handle to the list box font
170
173{
174 switch (msg)
175 {
176 case WM_CREATE:
177 {
178 hListBox =
180 WS_EX_CLIENTEDGE, TEXT("LISTBOX"), TEXT(""),
184 0, 0, 640, 480,
186 );
187 assert(hListBox != NULL);
188
190 hListBox, GWL_ID, reinterpret_cast<LONG_PTR>(hListBox)
191 );
192
194 reinterpret_cast<LPARAM>(TEXT("Line")));
196 reinterpret_cast<LPARAM>(TEXT("Bezier Curve")));
198 reinterpret_cast<LPARAM>(TEXT("Rectangle")));
200 reinterpret_cast<LPARAM>(TEXT("Rounded Rectangle")));
202 reinterpret_cast<LPARAM>(TEXT("Ellipse")));
204 reinterpret_cast<LPARAM>(TEXT("Arc")));
206 reinterpret_cast<LPARAM>(TEXT("Pie Slice")));
208 reinterpret_cast<LPARAM>(TEXT("Chord")));
210 reinterpret_cast<LPARAM>(TEXT("Custom")));
211
212 hListBrush = CreateSolidBrush(PALETTERGB(64, 192, 64));
213 hListPen = CreatePen(PS_SOLID, 3, PALETTERGB(0, 0, 0));
214 HDC hScreenDC = GetDC(NULL);
215#if 0
216 try
217#endif
218 {
219 // MakeFont() from Chapter 4
221 hScreenDC, "Impact", 20, ANSI_CHARSET,
223 );
224 }
225#if 0
226 catch (...)
227#endif
228 {
229 ReleaseDC(NULL, hScreenDC);
230 }
231 ReleaseDC(NULL, hScreenDC);
232
233 break;
234 }
235 case WM_MEASUREITEM:
236 {
237 LPMEASUREITEMSTRUCT lpmis =
238 reinterpret_cast<LPMEASUREITEMSTRUCT>(lParam);
239 assert(lpmis != NULL);
240
241 if (lpmis->CtlID == reinterpret_cast<UINT_PTR>(hListBox))
242 {
243 lpmis->itemHeight = 150;
244 return TRUE;
245 }
246 break;
247 }
248 case WM_DRAWITEM:
249 {
250 LPDRAWITEMSTRUCT lpdis =
251 reinterpret_cast<LPDRAWITEMSTRUCT>(lParam);
252 assert(lpdis != NULL);
253
254 if (lpdis->CtlID == reinterpret_cast<UINT_PTR>(hListBox))
255 {
256 SaveDC(lpdis->hDC);
257#if 0
258 try
259#endif
260 {
261 SelectObject(lpdis->hDC, hListBrush);
262 SelectObject(lpdis->hDC, hListPen);
263 SelectObject(lpdis->hDC, hListFont);
264
265 bool selected = (lpdis->itemState & ODS_SELECTED);
266 COLORREF clrText = GetSysColor(
268 );
271 );
272
273 // fill the background
274 FillRect(lpdis->hDC, &lpdis->rcItem, hListBrush);
275
276 // render the output primitive
277 RECT RPrimitive = lpdis->rcItem;
278 InflateRect(&RPrimitive, -5, -5);
279 RPrimitive.right = static_cast<int>(
280 0.6 * lpdis->rcItem.right + 0.5
281 );
282 FillRect(
283 lpdis->hDC, &RPrimitive,
284 reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1)
285 );
287 lpdis->hDC, RPrimitive,
288 static_cast<OutPrimitive>(lpdis->itemID)
289 );
290 if (selected) InvertRect(lpdis->hDC, &RPrimitive);
291 DrawEdge(lpdis->hDC, &RPrimitive, EDGE_SUNKEN, BF_RECT);
292
293 // render the text
294 TCHAR text[13];
295 if (SNDMSG(hListBox, LB_GETTEXT, lpdis->itemID,
296 reinterpret_cast<LPARAM>(&text)) != LB_ERR)
297 {
298 RECT RText = RPrimitive;
299 RText.left = RPrimitive.right;
300 RText.right = lpdis->rcItem.right;
301
302 SelectObject(lpdis->hDC, hListFont);
303 SetBkMode(lpdis->hDC, TRANSPARENT);
304 SetTextColor(lpdis->hDC, clrText);
305
306 DrawText(lpdis->hDC, text, -1, &RText,
308 }
309
310 // indicate keyboard focus
311 if (lpdis->itemState & ODS_FOCUS)
312 {
313 RECT RFocus = lpdis->rcItem;
314 InflateRect(&RFocus, -1, -1);
315 DrawFocusRect(lpdis->hDC, &RFocus);
316 }
317 }
318#if 0
319 catch (...)
320#endif
321 {
322 RestoreDC(lpdis->hDC, -1);
323 }
324 RestoreDC(lpdis->hDC, -1);
325 return TRUE;
326 }
327 break;
328 }
329 case WM_SIZE:
330 {
333 );
334 return 0;
335 }
336 case WM_DESTROY:
337 {
341
343 return 0;
344 }
345 }
347}
348//-------------------------------------------------------------------------
349
350
351
static HDC hDC
Definition: 3dtext.c:33
#define msg(x)
Definition: auth_time.c:54
HWND hWnd
Definition: settings.c:17
HINSTANCE hInstance
Definition: charmap.c:19
Definition: arc.h:55
WPARAM wParam
Definition: combotst.c:138
struct @1633 Msg[]
LPARAM lParam
Definition: combotst.c:139
static char selected[MAX_PATH+1]
Definition: dirdlg.c:7
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define APIENTRY
Definition: api.h:79
#define CALLBACK
Definition: compat.h:35
const WCHAR * text
Definition: package.c:1799
#define assert(x)
Definition: debug.h:53
pKey DeleteObject()
#define TEXT(s)
Definition: k32.h:26
static HDC
Definition: imagelist.c:92
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
static const ULONG FS_BOLD
Definition: mk_font.h:25
HFONT MakeFont(IN HDC hDestDC, IN LPCSTR typeface_name, IN int point_size, IN const BYTE charset, IN const DWORD style)
Definition: mk_font.cpp:23
static const ULONG FS_UNDERLINE
Definition: mk_font.h:27
unsigned int UINT
Definition: ndis.h:50
#define LOWORD(l)
Definition: pedump.c:82
#define WS_CHILD
Definition: pedump.c:617
#define WS_CAPTION
Definition: pedump.c:624
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
#define LBS_OWNERDRAWFIXED
Definition: pedump.c:682
#define LBS_HASSTRINGS
Definition: pedump.c:684
#define WS_VSCROLL
Definition: pedump.c:627
#define WS_VISIBLE
Definition: pedump.c:620
#define LBS_NOINTEGRALHEIGHT
Definition: pedump.c:686
#define WS_CLIPCHILDREN
Definition: pedump.c:619
BOOL Polygon(CONST PPOINT UnsafePoints, int Count, int polyFillMode)
Definition: polytest.cpp:730
const char * WndClassName
Definition: primitives.cpp:24
HPEN hListPen
Definition: primitives.cpp:168
HINSTANCE hInst
Definition: primitives.cpp:23
OutPrimitive
Definition: primitives.cpp:75
@ opArc
Definition: primitives.cpp:77
@ opEllipse
Definition: primitives.cpp:77
@ opBezier
Definition: primitives.cpp:76
@ opChord
Definition: primitives.cpp:77
@ opLine
Definition: primitives.cpp:76
@ opRoundRect
Definition: primitives.cpp:76
@ opPie
Definition: primitives.cpp:77
@ opCustom
Definition: primitives.cpp:77
@ opRectangle
Definition: primitives.cpp:76
HFONT hListFont
Definition: primitives.cpp:169
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
Definition: primitives.cpp:171
HWND hListBox
Definition: primitives.cpp:166
HBRUSH hListBrush
Definition: primitives.cpp:167
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow)
Definition: primitives.cpp:29
void DrawPrimitive(IN HDC hDC, IN const RECT &RPrimitive, IN OutPrimitive PrimitiveID)
Definition: primitives.cpp:80
#define DefWindowProc
Definition: ros2win.h:31
#define memset(x, y, z)
Definition: compat.h:39
#define R(b, x)
Definition: sha2.c:134
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 left
Definition: windef.h:306
#define SetWindowLongPtr
Definition: treelist.c:70
#define SNDMSG
Definition: treelist.h:31
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1829
#define IN
Definition: typedefs.h:39
#define HIWORD(l)
Definition: typedefs.h:247
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
DWORD COLORREF
Definition: windef.h:300
BOOL WINAPI Chord(_In_ HDC hdc, _In_ INT xLeft, _In_ INT yTop, _In_ INT xRight, _In_ INT yBottom, _In_ INT xRadial1, _In_ INT yRadial1, _In_ INT xRadial2, _In_ INT yRadial2)
Definition: arc.c:119
BOOL WINAPI PolyBezier(_In_ HDC hdc, _In_reads_(cpt) const POINT *apt, _In_ DWORD cpt)
Definition: painting.c:263
BOOL WINAPI Polyline(_In_ HDC hdc, _In_reads_(cpt) const POINT *apt, _In_ int cpt)
BOOL WINAPI Ellipse(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
#define PALETTERGB(r, g, b)
Definition: wingdi.h:2942
#define TRANSPARENT
Definition: wingdi.h:950
BOOL WINAPI RestoreDC(_In_ HDC, _In_ int)
#define ANSI_CHARSET
Definition: wingdi.h:383
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
BOOL WINAPI Rectangle(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
int WINAPI SetBkMode(_In_ HDC, _In_ int)
Definition: dc.c:1056
COLORREF WINAPI SetTextColor(_In_ HDC, _In_ COLORREF)
Definition: text.c:918
BOOL WINAPI RoundRect(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int)
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
HPEN WINAPI CreatePen(_In_ int, _In_ int, _In_ COLORREF)
#define PS_SOLID
Definition: wingdi.h:586
int WINAPI SaveDC(_In_ HDC)
BOOL WINAPI Pie(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int)
#define LB_ERR
Definition: winuser.h:2432
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
#define CS_VREDRAW
Definition: winuser.h:658
#define CreateWindowEx
Definition: winuser.h:5755
DWORD WINAPI GetSysColor(_In_ int)
#define ODS_SELECTED
Definition: winuser.h:2545
#define EDGE_SUNKEN
Definition: winuser.h:451
BOOL WINAPI TranslateMessage(_In_ const MSG *)
#define COLOR_WINDOW
Definition: winuser.h:918
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define DT_CENTER
Definition: winuser.h:527
#define LB_GETTEXT
Definition: winuser.h:2049
#define COLOR_WINDOWTEXT
Definition: winuser.h:921
#define WM_CREATE
Definition: winuser.h:1608
#define GWL_ID
Definition: winuser.h:859
BOOL WINAPI InvertRect(_In_ HDC, _In_ LPCRECT)
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define WM_SIZE
Definition: winuser.h:1611
#define COLOR_HIGHLIGHT
Definition: winuser.h:926
HBRUSH WINAPI GetSysColorBrush(_In_ int)
#define DT_SINGLELINE
Definition: winuser.h:540
#define CS_HREDRAW
Definition: winuser.h:653
#define IDC_ARROW
Definition: winuser.h:687
#define LB_ADDSTRING
Definition: winuser.h:2031
#define WM_DRAWITEM
Definition: winuser.h:1645
#define DrawText
Definition: winuser.h:5771
#define CreateWindow
Definition: winuser.h:5754
#define GetMessage
Definition: winuser.h:5790
BOOL WINAPI DrawEdge(_In_ HDC, _Inout_ LPRECT, _In_ UINT, _In_ UINT)
BOOL WINAPI UpdateWindow(_In_ HWND)
#define LoadCursor
Definition: winuser.h:5812
#define COLOR_HIGHLIGHTTEXT
Definition: winuser.h:927
HDC WINAPI GetDC(_In_opt_ HWND)
#define WM_MEASUREITEM
Definition: winuser.h:1646
#define DT_VCENTER
Definition: winuser.h:543
#define CW_USEDEFAULT
Definition: winuser.h:225
#define RegisterClass
Definition: winuser.h:5836
#define WM_DESTROY
Definition: winuser.h:1609
#define WS_EX_CLIENTEDGE
Definition: winuser.h:384
#define DispatchMessage
Definition: winuser.h:5765
BOOL WINAPI DrawFocusRect(_In_ HDC, _In_ LPCRECT)
BOOL WINAPI InflateRect(_Inout_ LPRECT, _In_ int, _In_ int)
#define BF_RECT
Definition: winuser.h:462
#define ODS_FOCUS
Definition: winuser.h:2549
BOOL WINAPI MoveWindow(_In_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ BOOL)
#define COLOR_BTNFACE
Definition: winuser.h:928
char TCHAR
Definition: xmlstorage.h:189
CHAR * LPTSTR
Definition: xmlstorage.h:192