ReactOS  0.4.14-dev-55-g2da92ac
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 
24 const char* WndClassName = "GMainWnd";
26  LPARAM LParam);
27 
28 
30  int nCmdShow)
31 {
32  hInst = hInstance;
33 
34  WNDCLASS wc;
35  memset(&wc, 0, sizeof(WNDCLASS));
36 
40  wc.hInstance = hInst;
42  wc.hbrBackground = reinterpret_cast<HBRUSH>(
43  COLOR_BTNFACE + 1
44  );
45 
46  if (RegisterClass(&wc))
47  {
48  HWND hWnd =
50  WndClassName, TEXT("Output Primitives Demo"),
53  CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
54  NULL, NULL, hInst, NULL
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 
80 void 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 
166 HWND hListBox = NULL; // handle to the list box
167 HBRUSH hListBrush = NULL; // handle to the list box brush
168 HPEN hListPen = NULL; // handle to the list box pen
169 HFONT hListFont = NULL; // handle to the list box font
170 
172  LPARAM lParam)
173 {
174  switch (msg)
175  {
176  case WM_CREATE:
177  {
178  hListBox =
180  WS_EX_CLIENTEDGE, TEXT("LISTBOX"), TEXT(""),
184  0, 0, 640, 480,
185  hWnd, NULL, hInst, NULL
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  );
269  HBRUSH hListBrush = GetSysColorBrush(
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  {
331  MoveWindow(
333  );
334  return 0;
335  }
336  case WM_DESTROY:
337  {
341 
342  PostQuitMessage(0);
343  return 0;
344  }
345  }
346  return DefWindowProc(hWnd, msg, wParam, lParam);
347 }
348 //-------------------------------------------------------------------------
349 
350 
351 
#define IN
Definition: typedefs.h:38
#define DispatchMessage
Definition: winuser.h:5666
#define R(b, x)
Definition: sha2.c:134
BOOL WINAPI TranslateMessage(_In_ const MSG *)
BOOL WINAPI InflateRect(_Inout_ LPRECT, _In_ int, _In_ int)
#define TRUE
Definition: types.h:120
HPEN WINAPI CreatePen(_In_ int, _In_ int, _In_ COLORREF)
#define COLOR_HIGHLIGHT
Definition: winuser.h:916
const char * WndClassName
Definition: primitives.cpp:24
long y
Definition: polytest.cpp:48
#define CreateWindow
Definition: winuser.h:5655
#define LB_ADDSTRING
Definition: winuser.h:2013
BOOL Polygon(CONST PPOINT UnsafePoints, int Count, int polyFillMode)
Definition: polytest.cpp:730
long x
Definition: polytest.cpp:48
HDC WINAPI GetDC(_In_opt_ HWND)
BOOL WINAPI Rectangle(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
const WCHAR * text
Definition: package.c:1827
#define GWL_ID
Definition: winuser.h:853
#define LBS_HASSTRINGS
Definition: pedump.c:684
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
BOOL WINAPI DrawFocusRect(_In_ HDC, _In_ LPCRECT)
static HDC
Definition: imagelist.c:92
#define CALLBACK
Definition: compat.h:27
BOOL WINAPI UpdateWindow(_In_ HWND)
HWND hWnd
Definition: settings.c:17
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
#define ODS_FOCUS
Definition: winuser.h:2524
#define assert(x)
Definition: debug.h:53
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define CS_HREDRAW
Definition: winuser.h:648
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
void DrawPrimitive(IN HDC hDC, IN const RECT &RPrimitive, IN OutPrimitive PrimitiveID)
Definition: primitives.cpp:80
UINT_PTR WPARAM
Definition: windef.h:207
HBRUSH hbrBackground
Definition: winuser.h:3144
#define WS_CHILD
Definition: pedump.c:617
LONG left
Definition: windef.h:291
BOOL WINAPI ShowWindow(_In_ HWND, _In_ int)
#define WS_CLIPCHILDREN
Definition: pedump.c:619
BOOL WINAPI InvertRect(_In_ HDC, _In_ LPCRECT)
LONG right
Definition: windef.h:293
#define COLOR_WINDOW
Definition: winuser.h:908
#define SetWindowLongPtr
Definition: treelist.c:70
DWORD WINAPI GetSysColor(_In_ int)
WPARAM wParam
Definition: combotst.c:138
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define DrawText
Definition: winuser.h:5672
#define LBS_OWNERDRAWFIXED
Definition: pedump.c:682
#define BF_RECT
Definition: winuser.h:462
#define PS_SOLID
Definition: wingdi.h:585
int WINAPI SetBkMode(_In_ HDC, _In_ int)
Definition: dc.c:1032
HINSTANCE hInst
Definition: primitives.cpp:23
HINSTANCE hInstance
Definition: charmap.c:20
struct @1576 Msg[]
#define LB_ERR
Definition: winuser.h:2407
BOOL WINAPI RoundRect(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int)
#define LBS_NOINTEGRALHEIGHT
Definition: pedump.c:686
BOOL WINAPI Arc(_In_ HDC hdc, _In_ INT xLeft, _In_ INT yTop, _In_ INT xRight, _In_ INT yBottom, _In_ INT xStartArc, _In_ INT yStartArc, _In_ INT xEndArc, _In_ INT yEndArc)
Definition: arc.c:5
HBRUSH hListBrush
Definition: primitives.cpp:167
BOOL WINAPI DrawEdge(_In_ HDC, _Inout_ LPRECT, _In_ UINT, _In_ UINT)
static char selected[MAX_PATH+1]
Definition: dirdlg.c:7
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
HCURSOR hCursor
Definition: winuser.h:3143
#define CreateWindowEx
Definition: winuser.h:5656
#define TRANSPARENT
Definition: wingdi.h:949
#define CW_USEDEFAULT
Definition: winuser.h:225
static const ULONG FS_UNDERLINE
Definition: mk_font.h:27
smooth NULL
Definition: ftsmooth.c:416
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
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow)
Definition: primitives.cpp:29
LONG_PTR LPARAM
Definition: windef.h:208
#define DT_VCENTER
Definition: winuser.h:543
#define COLOR_HIGHLIGHTTEXT
Definition: winuser.h:917
#define DefWindowProc
Definition: ros2win.h:31
#define WM_DESTROY
Definition: winuser.h:1591
#define WS_CAPTION
Definition: pedump.c:624
#define CS_VREDRAW
Definition: winuser.h:653
char TCHAR
Definition: xmlstorage.h:189
BOOL WINAPI Pie(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int, _In_ int)
#define WM_SIZE
Definition: winuser.h:1593
DWORD COLORREF
Definition: windef.h:285
#define COLOR_WINDOWTEXT
Definition: winuser.h:911
#define WM_MEASUREITEM
Definition: winuser.h:1628
#define LB_GETTEXT
Definition: winuser.h:2031
#define COLOR_BTNFACE
Definition: winuser.h:918
LPCSTR lpszClassName
Definition: winuser.h:3146
BOOL WINAPI MoveWindow(_In_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ BOOL)
#define LoadCursor
Definition: winuser.h:5713
UINT style
Definition: winuser.h:3137
#define SNDMSG
Definition: treelist.h:31
COLORREF WINAPI SetTextColor(_In_ HDC, _In_ COLORREF)
Definition: text.c:888
static HDC hDC
Definition: 3dtext.c:33
#define DT_SINGLELINE
Definition: winuser.h:540
#define TEXT(s)
Definition: k32.h:26
#define WS_EX_CLIENTEDGE
Definition: winuser.h:384
BOOL WINAPI Ellipse(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
HINSTANCE hInstance
Definition: winuser.h:3141
#define PALETTERGB(r, g, b)
Definition: wingdi.h:2937
unsigned int UINT
Definition: ndis.h:50
int WINAPI SaveDC(_In_ HDC)
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
#define WS_VSCROLL
Definition: pedump.c:627
#define GetMessage
Definition: winuser.h:5691
BOOL WINAPI PolyBezier(_In_ HDC hdc, _In_reads_(cpt) const POINT *apt, _In_ DWORD cpt)
Definition: painting.c:263
WNDPROC lpfnWndProc
Definition: winuser.h:3138
#define WM_DRAWITEM
Definition: winuser.h:1627
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
#define msg(x)
Definition: auth_time.c:54
#define IDC_ARROW
Definition: winuser.h:682
#define ODS_SELECTED
Definition: winuser.h:2520
#define WM_CREATE
Definition: winuser.h:1590
BOOL WINAPI Polyline(_In_ HDC hdc, _In_reads_(cpt) const POINT *apt, _In_ int cpt)
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
#define HIWORD(l)
Definition: typedefs.h:246
#define DT_CENTER
Definition: winuser.h:527
#define EDGE_SUNKEN
Definition: winuser.h:451
#define RegisterClass
Definition: winuser.h:5737
HPEN hListPen
Definition: primitives.cpp:168
HWND hListBox
Definition: primitives.cpp:166
#define ANSI_CHARSET
Definition: wingdi.h:382
OutPrimitive
Definition: primitives.cpp:75
LONG_PTR LRESULT
Definition: windef.h:209
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
#define WS_VISIBLE
Definition: pedump.c:620
HBRUSH WINAPI GetSysColorBrush(_In_ int)
static const ULONG FS_BOLD
Definition: mk_font.h:25
#define memset(x, y, z)
Definition: compat.h:39
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
Definition: primitives.cpp:171
BOOL WINAPI RestoreDC(_In_ HDC, _In_ int)
HFONT hListFont
Definition: primitives.cpp:169
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
#define APIENTRY
Definition: api.h:79