2 // ------------------------------------------------------------------
3 // Windows 2000 Graphics API Black Book
4 // Chapter 5 - Listing 5.1 (Output Primitives Demo)
6 // Created by Damon Chandler <dmc27@ee.cornell.edu>
7 // Updates can be downloaded at: <www.coriolis.com>
9 // Please do not hesistate to e-mail me at dmc27@ee.cornell.edu
10 // if you have any questions about this code.
11 // ------------------------------------------------------------------
14 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
18 // for the MakeFont() function...
20 //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 const char* WndClassName = "GMainWnd";
25 LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
29 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR,
35 memset(&wc, 0, sizeof(WNDCLASS));
37 wc.style = CS_VREDRAW | CS_HREDRAW;
38 wc.lpszClassName = WndClassName;
39 wc.lpfnWndProc = MainWndProc;
41 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
42 wc.hbrBackground = reinterpret_cast<HBRUSH>(
46 if (RegisterClass(&wc))
50 WndClassName, TEXT("Output Primitives Demo"),
51 WS_OVERLAPPEDWINDOW | WS_CAPTION |
52 WS_VISIBLE | WS_CLIPCHILDREN,
53 CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
54 NULL, NULL, hInst, NULL
59 ShowWindow(hWnd, nCmdShow);
63 while (GetMessage(&msg, NULL, 0, 0))
65 TranslateMessage(&msg);
66 DispatchMessage(&msg);
72 //------------------------------------------------------------------
76 opLine, opBezier, opRectangle, opRoundRect,
77 opEllipse, opArc, opPie, opChord, opCustom
80 void DrawPrimitive(IN HDC hDC, IN const RECT& RPrimitive,
81 IN OutPrimitive PrimitiveID)
84 InflateRect(&R, -10, -10);
90 const POINT PLine[] = {{R.left, R.top}, {R.right, R.bottom}};
91 Polyline(hDC, PLine, 2);
96 const POINT PControlPoints[] = {
98 {(R.right - R.left) / 2, R.top},
99 {(R.right - R.left) / 2, R.bottom},
102 PolyBezier(hDC, PControlPoints, 4);
107 Rectangle(hDC, R.left, R.top, R.right, R.bottom);
112 RoundRect(hDC, R.left, R.top, R.right, R.bottom, 20, 20);
117 Ellipse(hDC, R.left, R.top, R.right, R.bottom);
122 const POINT PRads[] = {
123 {(R.right - R.left) / 3 + R.left, R.top},
124 {(R.right - R.left) / 3 + R.left, R.bottom}
126 Arc(hDC, R.left, R.top, R.right, R.bottom,
127 PRads[0].x, PRads[0].y, PRads[1].x, PRads[1].y);
132 const POINT PRads[] = {
133 {(R.right - R.left) / 3 + R.left, R.top},
134 {(R.right - R.left) / 3 + R.left, R.bottom}
136 Pie(hDC, R.left, R.top, R.right, R.bottom,
137 PRads[0].x, PRads[0].y, PRads[1].x, PRads[1].y);
142 const POINT PRads[] = {
143 {(R.right - R.left) / 3 + R.left, R.top},
144 {(R.right - R.left) / 3 + R.left, R.bottom}
146 Chord(hDC, R.left, R.top, R.right, R.bottom,
147 PRads[0].x, PRads[0].y, PRads[1].x, PRads[1].y);
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}
158 Polygon(hDC, PVertices, 4);
163 //------------------------------------------------------------------
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
171 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam,
180 WS_EX_CLIENTEDGE, TEXT("LISTBOX"), TEXT(""),
181 WS_CHILD | WS_VISIBLE | WS_VSCROLL |
182 LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT |
185 hWnd, NULL, hInst, NULL
187 assert(hListBox != NULL);
190 hListBox, GWL_ID, reinterpret_cast<LONG>(hListBox)
193 SNDMSG(hListBox, LB_ADDSTRING, 0,
194 reinterpret_cast<LPARAM>(TEXT("Line")));
195 SNDMSG(hListBox, LB_ADDSTRING, 0,
196 reinterpret_cast<LPARAM>(TEXT("Bezier Curve")));
197 SNDMSG(hListBox, LB_ADDSTRING, 0,
198 reinterpret_cast<LPARAM>(TEXT("Rectangle")));
199 SNDMSG(hListBox, LB_ADDSTRING, 0,
200 reinterpret_cast<LPARAM>(TEXT("Rounded Rectangle")));
201 SNDMSG(hListBox, LB_ADDSTRING, 0,
202 reinterpret_cast<LPARAM>(TEXT("Ellipse")));
203 SNDMSG(hListBox, LB_ADDSTRING, 0,
204 reinterpret_cast<LPARAM>(TEXT("Arc")));
205 SNDMSG(hListBox, LB_ADDSTRING, 0,
206 reinterpret_cast<LPARAM>(TEXT("Pie Slice")));
207 SNDMSG(hListBox, LB_ADDSTRING, 0,
208 reinterpret_cast<LPARAM>(TEXT("Chord")));
209 SNDMSG(hListBox, LB_ADDSTRING, 0,
210 reinterpret_cast<LPARAM>(TEXT("Custom")));
212 hListBrush = CreateSolidBrush(PALETTERGB(64, 192, 64));
213 hListPen = CreatePen(PS_SOLID, 3, PALETTERGB(0, 0, 0));
214 HDC hScreenDC = GetDC(NULL);
219 // MakeFont() from Chapter 4
220 hListFont = font::MakeFont(
221 hScreenDC, "Impact", 20, ANSI_CHARSET,
222 font::FS_BOLD | font::FS_UNDERLINE
229 ReleaseDC(NULL, hScreenDC);
231 ReleaseDC(NULL, hScreenDC);
237 LPMEASUREITEMSTRUCT lpmis =
238 reinterpret_cast<LPMEASUREITEMSTRUCT>(lParam);
239 assert(lpmis != NULL);
241 if (lpmis->CtlID == reinterpret_cast<UINT>(hListBox))
243 lpmis->itemHeight = 150;
250 LPDRAWITEMSTRUCT lpdis =
251 reinterpret_cast<LPDRAWITEMSTRUCT>(lParam);
252 assert(lpdis != NULL);
254 if (lpdis->CtlID == reinterpret_cast<UINT>(hListBox))
261 SelectObject(lpdis->hDC, hListBrush);
262 SelectObject(lpdis->hDC, hListPen);
263 SelectObject(lpdis->hDC, hListFont);
265 bool selected = (lpdis->itemState & ODS_SELECTED);
266 COLORREF clrText = GetSysColor(
267 selected ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT
269 HBRUSH hListBrush = GetSysColorBrush(
270 selected ? COLOR_HIGHLIGHT : COLOR_WINDOW
273 // fill the background
274 FillRect(lpdis->hDC, &lpdis->rcItem, hListBrush);
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
283 lpdis->hDC, &RPrimitive,
284 reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1)
287 lpdis->hDC, RPrimitive,
288 static_cast<OutPrimitive>(lpdis->itemID)
290 if (selected) InvertRect(lpdis->hDC, &RPrimitive);
291 DrawEdge(lpdis->hDC, &RPrimitive, EDGE_SUNKEN, BF_RECT);
295 if (SNDMSG(hListBox, LB_GETTEXT, lpdis->itemID,
296 reinterpret_cast<LPARAM>(&text)) != LB_ERR)
298 RECT RText = RPrimitive;
299 RText.left = RPrimitive.right;
300 RText.right = lpdis->rcItem.right;
302 SelectObject(lpdis->hDC, hListFont);
303 SetBkMode(lpdis->hDC, TRANSPARENT);
304 SetTextColor(lpdis->hDC, clrText);
306 DrawText(lpdis->hDC, text, -1, &RText,
307 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
310 // indicate keyboard focus
311 if (lpdis->itemState & ODS_FOCUS)
313 RECT RFocus = lpdis->rcItem;
314 InflateRect(&RFocus, -1, -1);
315 DrawFocusRect(lpdis->hDC, &RFocus);
322 RestoreDC(lpdis->hDC, -1);
324 RestoreDC(lpdis->hDC, -1);
332 hListBox, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE
338 if (hListBrush) DeleteObject(hListBrush);
339 if (hListPen) DeleteObject(hListPen);
340 if (hListFont) DeleteObject(hListFont);
346 return DefWindowProc(hWnd, msg, wParam, lParam);
348 //-------------------------------------------------------------------------