|
|
1.1 root 1: /****************************************************************************
2:
3: PROGRAM: Output.c
4:
5: PURPOSE: Output template for Windows applications
6:
7: FUNCTIONS:
8:
9: WinMain() - calls initialization function, processes message loop
10: InitApplication() - initializes window data and registers window
11: InitInstance() - saves instance handle and creates main window
12: MainWndProc() - processes messages
13: About() - processes messages for "About" dialog box
14:
15: ****************************************************************************/
16:
17: #include "windows.h"
18: #include "string.h"
19: #include "output.h"
20:
21: HANDLE hInst;
22:
23: HPEN hDashPen; /* "---" pen handle */
24: HPEN hDotPen; /* "..." pen handle */
25: HBRUSH hOldBrush; /* old brush handle */
26: HBRUSH hRedBrush; /* red brush handle */
27: HBRUSH hGreenBrush; /* green brush handle */
28: HBRUSH hBlueBrush; /* blue brush handle */
29:
30:
31: /****************************************************************************
32:
33: FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
34:
35: PURPOSE: calls initialization function, processes message loop
36:
37: ****************************************************************************/
38:
39: int APIENTRY WinMain(
40: HANDLE hInstance,
41: HANDLE hPrevInstance,
42: LPSTR lpCmdLine,
43: int nCmdShow
44: )
45: {
46: MSG msg;
47:
48: UNREFERENCED_PARAMETER( lpCmdLine );
49:
50: if (!hPrevInstance)
51: if (!InitApplication(hInstance))
52: return (FALSE);
53:
54: if (!InitInstance(hInstance, nCmdShow))
55: return (FALSE);
56:
57: while (GetMessage(&msg, NULL, NULL, NULL)) {
58: TranslateMessage(&msg);
59: DispatchMessage(&msg);
60: }
61: return (msg.wParam);
62: }
63:
64:
65: /****************************************************************************
66:
67: FUNCTION: InitApplication(HANDLE)
68:
69: PURPOSE: Initializes window data and registers window class
70:
71: ****************************************************************************/
72: BOOL InitApplication(HANDLE hInstance)
73: {
74: WNDCLASS wc;
75:
76: wc.style = NULL;
77: wc.lpfnWndProc = (WNDPROC) MainWndProc;
78: wc.cbClsExtra = 0;
79: wc.cbWndExtra = 0;
80: wc.hInstance = hInstance;
81: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
82: wc.hCursor = LoadCursor(NULL, IDC_ARROW);
83: wc.hbrBackground = GetStockObject(WHITE_BRUSH);
84: wc.lpszMenuName = "OutputMenu";
85: wc.lpszClassName = "OutputWClass";
86:
87: return (RegisterClass(&wc));
88: }
89:
90:
91: /****************************************************************************
92:
93: FUNCTION: InitInstance(HANDLE, int)
94:
95: PURPOSE: Saves instance handle and creates main window
96:
97: ****************************************************************************/
98:
99: BOOL InitInstance(
100: HANDLE hInstance,
101: INT nCmdShow)
102: {
103: HWND hWnd;
104:
105: hInst = hInstance;
106:
107: hWnd = CreateWindow(
108: "OutputWClass",
109: "Output Sample Application",
110: WS_OVERLAPPEDWINDOW,
111: 0,
112: 0,
113: GetSystemMetrics(SM_CXSCREEN),
114: GetSystemMetrics(SM_CYSCREEN),
115: NULL,
116: NULL,
117: hInstance,
118: NULL
119: );
120:
121: if (!hWnd)
122: return (FALSE);
123:
124: ShowWindow(hWnd, nCmdShow);
125: UpdateWindow(hWnd);
126: return (TRUE);
127:
128: }
129:
130: /****************************************************************************
131:
132: FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
133:
134: PURPOSE: Processes messages
135:
136: MESSAGES:
137:
138: WM_COMMAND - application menu (About dialog box)
139: WM_CREATE - create window and objects
140: WM_PAINT - update window, draw objects
141: WM_DESTROY - destroy window
142:
143: COMMENTS:
144:
145: Handles to the objects you will use are obtained when the WM_CREATE
146: message is received, and deleted when the WM_DESTROY message is
147: received. The actual drawing is done whenever a WM_PAINT message is
148: received.
149:
150: ****************************************************************************/
151:
152: LONG APIENTRY MainWndProc(
153: HWND hWnd,
154: UINT message,
155: UINT wParam,
156: LONG lParam)
157: {
158: FARPROC lpProcAbout;
159:
160: HDC hDC; /* display-context variable */
161: PAINTSTRUCT ps; /* paint structure */
162: RECT rcTextBox; /* rectangle around the text */
163: HPEN hOldPen; /* old pen handle */
164:
165:
166:
167: switch (message) {
168: case WM_COMMAND:
169: if (LOWORD(wParam) == IDM_ABOUT) {
170: lpProcAbout = MakeProcInstance((FARPROC)About, hInst);
171:
172: DialogBox(hInst,
173: "AboutBox",
174: hWnd,
175: (WNDPROC)lpProcAbout);
176:
177: FreeProcInstance(lpProcAbout);
178: break;
179: }
180: else
181: return (DefWindowProc(hWnd, message, wParam, lParam));
182:
183: case WM_CREATE:
184:
185: /* Create the brush objects */
186:
187: hRedBrush = CreateSolidBrush(RGB(255, 0, 0));
188: hGreenBrush = CreateSolidBrush(RGB( 0, 255, 0));
189: hBlueBrush = CreateSolidBrush(RGB( 0, 0, 255));
190:
191: /* Create the "---" pen */
192:
193: hDashPen = CreatePen(PS_DASH, /* style */
194: 1, /* width */
195: RGB(0, 0, 0)); /* color */
196:
197: /* Create the "..." pen */
198:
199: hDotPen = CreatePen(2, /* style */
200: 1, /* width */
201: RGB(0, 0, 0)); /* color */
202: break;
203:
204: case WM_PAINT:
205: {
206: TEXTMETRIC textmetric;
207: INT nDrawX;
208: INT nDrawY;
209: CHAR szText[300];
210:
211: /* Set up a display context to begin painting */
212:
213: hDC = BeginPaint (hWnd, &ps);
214:
215: /* Get the size characteristics of the current font. */
216: /* This information will be used for determining the */
217: /* vertical spacing of text on the screen. */
218:
219: GetTextMetrics (hDC, &textmetric);
220:
221: /* Initialize drawing position to 1/4 inch from the top */
222: /* and from the left of the top, left corner of the */
223: /* client area of the main windows. */
224:
225: nDrawX = GetDeviceCaps(hDC, LOGPIXELSX) / 4; /* 1/4 inch */
226: nDrawY = GetDeviceCaps(hDC, LOGPIXELSY) / 4; /* 1/4 inch */
227:
228: /* Send characters to the screen. After displaying each */
229: /* line of text, advance the vertical position for the */
230: /* next line of text. The pixel distance between the top */
231: /* of each line of text is equal to the standard height of */
232: /* the font characters (tmHeight), plus the standard */
233: /* amount of spacing (tmExternalLeading) between adjacent */
234: /* lines. */
235:
236: strcpy (szText, "These characters are being painted using ");
237: TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
238: nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
239:
240: strcpy (szText, "the TextOut() function, which is fast and ");
241: TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
242: nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
243:
244: strcpy (szText, "allows programmer control of placement and ");
245: TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
246: nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
247:
248: strcpy (szText, "formatting details. However, TextOut() ");
249: TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
250: nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
251:
252: strcpy (szText, "does not provide any automatic formatting.");
253: TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
254: nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
255:
256: /* Put text in a 5-inch by 1-inch rectangle and display it. */
257: /* First define the size of the rectangle around the text */
258:
259: nDrawY += GetDeviceCaps(hDC, LOGPIXELSY) / 4; /* 1/4 inch */
260: SetRect (
261: &rcTextBox
262: , nDrawX
263: , nDrawY
264: , nDrawX + (5 * GetDeviceCaps(hDC, LOGPIXELSX)) /* 5" */
265: , nDrawY + (1 * GetDeviceCaps(hDC, LOGPIXELSY)) /* 1" */
266: );
267:
268: /* Draw the text within the bounds of the above rectangle */
269:
270: strcpy (szText, "This text is being displayed with a single "
271: "call to DrawText(). DrawText() isn't as fast "
272: "as TextOut(), and it is somewhat more "
273: "constrained, but it provides numerous optional "
274: "formatting features, such as the centering and "
275: "line breaking used in this example.");
276: DrawText (
277: hDC
278: , szText
279: , strlen (szText)
280: , &rcTextBox
281: , DT_CENTER | DT_EXTERNALLEADING | DT_NOCLIP
282: | DT_NOPREFIX | DT_WORDBREAK
283: );
284:
285: /* Paint the next object immediately below the bottom of */
286: /* the above rectangle in which the text was drawn. */
287:
288: nDrawY = rcTextBox.bottom;
289:
290: /* The (x,y) pixel coordinates of the objects about to be */
291: /* drawn are below, and to the right of, the current */
292: /* coordinate (nDrawX,nDrawY). */
293:
294: /* Draw a red rectangle.. */
295:
296: hOldBrush = SelectObject(hDC, hRedBrush);
297: Rectangle (
298: hDC
299: , nDrawX
300: , nDrawY
301: , nDrawX + 50
302: , nDrawY + 30
303: );
304:
305: /* Draw a green ellipse */
306:
307: SelectObject(hDC, hGreenBrush);
308: Ellipse (
309: hDC
310: , nDrawX + 150
311: , nDrawY
312: , nDrawX + 150 + 50
313: , nDrawY + 30
314: );
315:
316: /* Draw a blue pie shape */
317:
318: SelectObject(hDC, hBlueBrush);
319: Pie (
320: hDC
321: , nDrawX + 300
322: , nDrawY
323: , nDrawX + 300 + 50
324: , nDrawY + 50
325: , nDrawX + 300 + 50
326: , nDrawY
327: , nDrawX + 300 + 50
328: , nDrawY + 50
329: );
330:
331: nDrawY += 50;
332:
333: /* Restore the old brush */
334:
335: SelectObject(hDC, hOldBrush);
336:
337: /* Select a "---" pen, save the old value */
338:
339: nDrawY += GetDeviceCaps(hDC, LOGPIXELSY) / 4; /* 1/4 inch */
340: hOldPen = SelectObject(hDC, hDashPen);
341:
342: /* Move to a specified point */
343:
344: MoveToEx(hDC, nDrawX, nDrawY, NULL );
345:
346: /* Draw a line */
347:
348: LineTo(hDC, nDrawX + 350, nDrawY);
349:
350: /* Select a "..." pen */
351:
352: SelectObject(hDC, hDotPen);
353:
354: /* Draw an arc connecting the line */
355:
356: Arc (
357: hDC
358: , nDrawX
359: , nDrawY - 20
360: , nDrawX + 350
361: , nDrawY + 20
362: , nDrawX
363: , nDrawY
364: , nDrawX + 350
365: , nDrawY
366: );
367:
368: /* Restore the old pen */
369:
370: SelectObject(hDC, hOldPen);
371:
372: /* Tell Windows you are done painting */
373:
374: EndPaint (hWnd, &ps);
375: }
376: break;
377:
378: case WM_DESTROY:
379: DeleteObject(hRedBrush);
380: DeleteObject(hGreenBrush);
381: DeleteObject(hBlueBrush);
382: DeleteObject(hDashPen);
383: DeleteObject(hDotPen);
384: PostQuitMessage(0);
385: break;
386:
387: default:
388: return (DefWindowProc(hWnd, message, wParam, lParam));
389: }
390: return (NULL);
391: }
392:
393:
394: /****************************************************************************
395:
396: FUNCTION: About(HWND, unsigned, WORD, LONG)
397:
398: PURPOSE: Processes messages for "About" dialog box
399:
400: MESSAGES:
401:
402: WM_INITDIALOG - initialize dialog box
403: WM_COMMAND - Input received
404:
405: ****************************************************************************/
406:
407: BOOL APIENTRY About(
408: HWND hDlg,
409: UINT message,
410: UINT wParam,
411: LONG lParam)
412: {
413: switch (message) {
414: case WM_INITDIALOG:
415: return (TRUE);
416:
417: case WM_COMMAND:
418: if (LOWORD(wParam) == IDOK
419: || LOWORD(wParam) == IDCANCEL) {
420: EndDialog(hDlg, TRUE);
421: return (TRUE);
422: }
423: break;
424: }
425: return (FALSE);
426: UNREFERENCED_PARAMETER(lParam);
427: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.