|
|
1.1 root 1: /**************************************************************************\
2: * Simple sample to show how to dynamically create Win32 dialogs.
3: *
4: * Steve Firebaugh
5: * Microsoft Developer Support
6: * Copyright (c) 1992, 1993 Microsoft Corporation
7: *
8: *
9: \**************************************************************************/
10:
11: #define UNICODE
12: #include <windows.h>
13: #include "dyndlg.h"
14:
15: LRESULT APIENTRY MainWndProc(HWND, UINT, UINT, LONG);
16: LRESULT APIENTRY About(HWND, UINT, WPARAM, LPARAM );
17:
18: int Create1(HWND);
19: int Create2(HWND);
20:
21: HINSTANCE ghInst;
22:
23: /**************************************************************************\
24: *
25: * function: WinMain()
26: *
27: * input parameters: c.f. generic sample
28: *
29: \**************************************************************************/
30: int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
31: LPSTR lpCmdLine, int nCmdShow)
32: {
33: HWND hwnd;
34: MSG msg;
35: HANDLE hLibrary;
36:
37: UNREFERENCED_PARAMETER( lpCmdLine );
38:
39: ghInst = hInstance;
40:
41: /* Check for previous instance. If none, then register class. */
42: if (!hPrevInstance) {
43: WNDCLASS wc;
44:
45: wc.style = 0;
46: wc.lpfnWndProc = (WNDPROC)MainWndProc;
47:
48: wc.cbClsExtra = 0;
49: wc.cbWndExtra = 0;
50: wc.hInstance = hInstance;
51: wc.hIcon = LoadIcon(hInstance, TEXT("dyndlgIcon"));
52: wc.hCursor = LoadCursor(NULL, IDC_ARROW);
53: wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
54: wc.lpszMenuName = TEXT("dyndlgMenu");
55: wc.lpszClassName = TEXT("dyndlg");
56:
57: if (!RegisterClass(&wc)) return (FALSE);
58: } /* class registered o.k. */
59:
60:
61: /* Create the main window. Return false if CreateWindow() fails */
62: hwnd = CreateWindow(
63: TEXT("dyndlg"),
64: TEXT("dyndlg"),
65: WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE,
66: CW_USEDEFAULT,
67: CW_USEDEFAULT,
68: CW_USEDEFAULT,
69: CW_USEDEFAULT,
70: NULL,
71: NULL,
72: hInstance,
73: NULL);
74:
75: if (!hwnd) return (FALSE);
76:
77:
78: /***** CUSTOM CONTROL
79: * Load the DLL containing the custom control.
80: *****/
81: hLibrary = LoadLibrary (TEXT("..\\spincube\\SPINCUBE.DLL"));
82: if (hLibrary == NULL)
83: MessageBox (hwnd, TEXT("LoadLibrary (..\\spincube\\SPINCUBE.DLL) failed"),
84: TEXT("Error, this app requires spincube."), MB_OK | MB_ICONEXCLAMATION);
85: /***** CUSTOM CONTROL *****/
86:
87:
88: /* Demo: Just for fun, start out with one of the dialogs created. */
89: PostMessage (hwnd, WM_COMMAND, IDM_DIALOG2, 0);
90:
91: /* Loop getting messages and dispatching them. */
92: while (GetMessage(&msg,NULL, 0, 0)) {
93: TranslateMessage(&msg);
94: DispatchMessage(&msg);
95: }
96:
97: if (hLibrary != NULL) FreeLibrary (hLibrary);
98:
99: return (msg.wParam);
100: }
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111: /***************************************************************************\
112: * FUNCTION: MainWndProc
113: \***************************************************************************/
114: LRESULT APIENTRY MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
115: {
116: switch (message) {
117:
118: /**********************************************************************\
119: * Menu item support.
120: *
121: \**********************************************************************/
122: case WM_COMMAND:
123: switch (LOWORD(wParam)) {
124:
125: case IDM_DIALOG1:
126: Create1 (hwnd);
127: break;
128:
129: case IDM_DIALOG2:
130: Create2 (hwnd);
131: break;
132:
133: case IDM_HELP:
134: WinHelp( hwnd, TEXT("dyndlg.hlp"), HELP_INDEX, (DWORD) NULL );
135: break;
136:
137: case IDM_ABOUT:
138: DialogBox (GetModuleHandle(NULL), TEXT("aboutBox"), hwnd, (DLGPROC)About);
139: return 0;
140:
141: } /* end switch */
142: break; /* end wm_command */
143:
144:
145:
146: case WM_DESTROY:
147: WinHelp( hwnd, TEXT("dyndlg.hlp"), (UINT) HELP_QUIT, (DWORD) NULL );
148: PostQuitMessage(0);
149: break;
150:
151: } /* end switch */
152: return (DefWindowProc(hwnd, message, wParam, lParam));
153: }
154:
155:
156: /****************************************************************************
157: FUNCTION: About
158: ****************************************************************************/
159: LRESULT CALLBACK About(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
160: {
161: if ((message == WM_COMMAND) && (LOWORD(wParam) == IDOK)) {
162: EndDialog (hwnd, TRUE);
163: return TRUE;
164: }
165: if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE)) {
166: EndDialog (hwnd, TRUE);
167: return TRUE;
168: }
169: return FALSE;
170: }
171:
172:
173:
174:
175:
176:
177:
178:
179: /*+++
180:
181:
182: Create the first dialog dynamically. Notice that we are NOT using
183: structures here because too many of the fields are of variable length.
184: Instead, just allocate some memory to play with, and start filling in
185: the data at that pointer.
186:
187: p - pointer which is moved down through the DLGTEMPLATE information.
188: pdlgtemplate - pointer to the TOP of the DLGTEMPLATE information.
189:
190:
191: Notice that UNICODE is defined to be on in this module. That means:
192: 1. All strings included in TEXT() macro will be made unicode strings
193: by the compiler.
194: 2. wsprintf() will accept a unicode string as input, and will fill
195: its lpOut buffer with a unicode string. Notice that in any case,
196: the return value is the number of *characters* not the number of
197: bytes.
198: 3. Any system call which may be dependent upon unicode will be mapped
199: to its wide character version (*W not *A) by the header files.
200: Notice that this does not matter for the CreateDialogIndirect() call.
201: Both the A and W versions expect the dialog template to contain wide
202: character strings.
203:
204:
205: Here we create a simple dialog with one item. The dialog has a title,
206: the item has text, and the item class is specified by ordinal. There
207: is no font information.
208:
209: ---*/
210:
211: Create1(HWND hwnd)
212: {
213: WORD *p, *pdlgtemplate;
214: int nchar;
215:
216: /* declare variables purely for ease of reading the names provide. */
217: DWORD lStyle;
218: DWORD lExtendedStyle;
219: WORD NumberOfItems;
220: WORD x;
221: WORD y;
222: WORD cx;
223: WORD cy;
224:
225: WORD wId;
226:
227: /* allocate some memory to play with */
228: pdlgtemplate = p = (PWORD) LocalAlloc (LPTR, 1000);
229:
230: lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | WS_VISIBLE;
231: lExtendedStyle = 0;
232: NumberOfItems = 1;
233: x = 10;
234: y = 10;
235: cx = 100;
236: cy = 100;
237:
238: /* start to fill in the dlgtemplate information. addressing by WORDs */
239: *p++ = LOWORD (lStyle);
240: *p++ = HIWORD (lStyle);
241: *p++ = LOWORD (lExtendedStyle);
242: *p++ = HIWORD (lExtendedStyle);
243: *p++ = NumberOfItems;
244: *p++ = x ;
245: *p++ = y ;
246: *p++ = cx;
247: *p++ = cy;
248: *p++ = 0; // Menu
249: *p++ = 0; // Class
250:
251: /* copy the title of the dialog, null terminate the string. */
252: nchar = wsprintf (p, TEXT("Title 1"));
253: p += nchar;
254: *p++ = 0;
255:
256: /* add in the wPointSize and szFontName here iff the DS_SETFONT bit on */
257:
258: /* make sure the first item starts on a DWORD boundary */
259: { ULONG l;
260:
261: l = (ULONG) p;
262: l +=3;
263: l >>=2;
264: l <<=2;
265: p = (PWORD) l;
266: }
267:
268:
269: /* now start with the first item */
270: lStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD;
271: x = 10;
272: y = 70;
273: cx = 80;
274: cy = 20;
275: wId = IDOK;
276:
277:
278: *p++ = LOWORD (lStyle);
279: *p++ = HIWORD (lStyle);
280: *p++ = LOWORD (lExtendedStyle);
281: *p++ = HIWORD (lExtendedStyle);
282: *p++ = x ;
283: *p++ = y ;
284: *p++ = cx;
285: *p++ = cy;
286: *p++ = wId;
287:
288: /* fill in class i.d. Button in this case */
289: *p++ = (WORD)0xffff;
290: *p++ = (WORD)0x0080;
291:
292: /* copy the text of the first item, null terminate the string. */
293: nchar = wsprintf (p, TEXT("OK"));
294: p += nchar;
295: *p++ = 0;
296:
297: *p++ = 0; // advance pointer over nExtraStuff WORD
298:
299: CreateDialogIndirect (ghInst, (LPDLGTEMPLATE) pdlgtemplate, hwnd, (DLGPROC) About);
300:
301: LocalFree (LocalHandle (pdlgtemplate));
302:
303: return 0;
304: }
305:
306:
307:
308:
309:
310:
311:
312: /*+++
313:
314:
315: Create the second dialog dynamically.
316:
317: Here we create a dialog which has font information (DS_SETFONT),
318: and which has two items with the item class specified by name.
319:
320:
321: ---*/
322: Create2(HWND hwnd)
323: {
324: WORD *p, *pdlgtemplate;
325: int nchar;
326:
327: /* declare variables purely for ease of reading the names provide. */
328: DWORD lStyle;
329: DWORD lExtendedStyle;
330: WORD NumberOfItems;
331: WORD x;
332: WORD y;
333: WORD cx;
334: WORD cy;
335:
336: WORD wId;
337:
338: /* allocate some memory to play with */
339: pdlgtemplate = p = (PWORD) LocalAlloc (LPTR, 1000);
340:
341: lStyle = WS_CAPTION | WS_SYSMENU | WS_VISIBLE | DS_SETFONT;
342: lExtendedStyle = 0;
343: NumberOfItems = 2;
344: x = 210;
345: y = 10;
346: cx = 100;
347: cy = 100;
348:
349: /* start to fill in the dlgtemplate information. addressing by WORDs */
350: *p++ = LOWORD (lStyle);
351: *p++ = HIWORD (lStyle);
352: *p++ = LOWORD (lExtendedStyle);
353: *p++ = HIWORD (lExtendedStyle);
354: *p++ = NumberOfItems;
355: *p++ = x ;
356: *p++ = y ;
357: *p++ = cx;
358: *p++ = cy;
359: *p++ = 0; // Menu
360: *p++ = 0; // Class
361:
362: /* copy the title of the dialog, null terminate the string. */
363: nchar = wsprintf (p, TEXT("Title 2"));
364: p += nchar;
365: *p++ = 0;
366:
367: /* Font information because of DS_SETFONT */
368: *p++ = 18; // point size
369: nchar = wsprintf (p, TEXT("Times New Roman")); // Face name
370: p += nchar;
371: *p++ = 0;
372:
373:
374: /* make sure the first item starts on a DWORD boundary */
375: { ULONG l;
376:
377: l = (ULONG) p;
378: l +=3;
379: l >>=2;
380: l <<=2;
381: p = (PWORD) l;
382: }
383:
384:
385: /* now start with the first item */
386: lStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
387: x = 10;
388: y = 60;
389: cx = 80;
390: cy = 20;
391: wId = IDOK;
392:
393:
394: *p++ = LOWORD (lStyle);
395: *p++ = HIWORD (lStyle);
396: *p++ = LOWORD (lExtendedStyle);
397: *p++ = HIWORD (lExtendedStyle);
398: *p++ = x ;
399: *p++ = y ;
400: *p++ = cx;
401: *p++ = cy;
402: *p++ = wId;
403:
404:
405: /* fill in class i.d., this time by name */
406: nchar = wsprintf (p, TEXT("BUTTON"));
407: p += nchar;
408: *p++ = 0;
409:
410:
411: /* copy the text of the first item, null terminate the string. */
412: nchar = wsprintf (p, TEXT("OK"));
413: p += nchar;
414: *p++ = 0;
415:
416: *p++ = 0; // advance pointer over nExtraStuff WORD
417:
418: /* make sure the second item starts on a DWORD boundary */
419: { ULONG l;
420:
421: l = (ULONG) p;
422: l +=3;
423: l >>=2;
424: l <<=2;
425: p = (PWORD) l;
426: }
427:
428: #define SS_INMOTION 0x0002 /* from spincube.h */
429: lStyle = WS_VISIBLE | WS_CHILD | SS_INMOTION;
430: x = 20;
431: y = 5;
432: cx = 65;
433: cy = 45;
434: wId = 57;
435:
436:
437: *p++ = LOWORD (lStyle);
438: *p++ = HIWORD (lStyle);
439: *p++ = LOWORD (lExtendedStyle);
440: *p++ = HIWORD (lExtendedStyle);
441: *p++ = x ;
442: *p++ = y ;
443: *p++ = cx;
444: *p++ = cy;
445: *p++ = wId;
446:
447:
448: /* fill in class i.d., this time by name */
449:
450: /***** CUSTOM CONTROL
451: * Fill in the class name that is specified in the DLL
452: * See the \q_a\samples\spincube sample for the source to this.
453: *****/
454: nchar = wsprintf (p, TEXT("Spincube"));
455: p += nchar;
456: *p++ = 0;
457:
458: /* copy the text of the second item, null terminate the string. */
459: nchar = wsprintf (p, TEXT(""));
460: p += nchar;
461: *p++ = 0;
462:
463: *p++ = 0; // advance pointer over nExtraStuff WORD
464:
465: CreateDialogIndirect (ghInst, (LPDLGTEMPLATE) pdlgtemplate, hwnd, (DLGPROC) About);
466:
467: LocalFree (LocalHandle (pdlgtemplate));
468:
469: return 0;
470: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.