|
|
1.1.1.4 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: /**************************************************************************** 13: 1.1.1.4 ! root 14: PROGRAM: Generic.c 1.1 root 15: 1.1.1.4 ! root 16: PURPOSE: Generic template for Windows applications 1.1 root 17: 1.1.1.4 ! root 18: FUNCTIONS: 1.1 root 19: 1.1.1.4 ! root 20: WinMain() - calls initialization function, processes message loop ! 21: InitApplication() - initializes window data and registers window ! 22: InitInstance() - saves instance handle and creates main window ! 23: WndProc() - processes messages ! 24: CenterWindow() - used to center the "About" box over application window ! 25: About() - processes messages for "About" dialog box ! 26: ! 27: COMMENTS: ! 28: ! 29: The Windows SDK Generic Application Example is a sample application ! 30: that you can use to get an idea of how to perform some of the simple ! 31: functionality that all Applications written for Microsoft Windows ! 32: should implement. You can use this application as either a starting ! 33: point from which to build your own applications, or for quickly ! 34: testing out functionality of an interesting Windows API. 1.1.1.3 root 35: 1.1.1.4 ! root 36: This application is source compatible for with Windows 3.1 and ! 37: Windows NT. 1.1 root 38: 39: ****************************************************************************/ 40: 1.1.1.3 root 41: #include <windows.h> // required for all Windows applications 1.1.1.4 ! root 42: #if !defined(_WIN32) && !defined(WIN32) 1.1.1.3 root 43: #include <ver.h> 44: #endif 45: #include "generic.h" // specific to this program 46: 47: #if !defined (APIENTRY) // Windows NT defines APIENTRY, but 3.x doesn't 48: #define APIENTRY far pascal 49: #endif 50: 1.1.1.4 ! root 51: #if !defined(_WIN32) && !defined(WIN32) // Windows 3.x uses a FARPROC for dialogs 1.1.1.3 root 52: #define DLGPROC FARPROC 53: #endif 54: 1.1.1.4 ! root 55: HINSTANCE hInst; // current instance 1.1 root 56: 1.1.1.3 root 57: char szAppName[] = "Generic"; // The name of this application 58: char szTitle[] = "Generic Sample Application"; // The title bar text 1.1 root 59: 60: /**************************************************************************** 61: 1.1.1.4 ! root 62: FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 1.1 root 63: 1.1.1.4 ! root 64: PURPOSE: calls initialization function, processes message loop 1.1 root 65: 1.1.1.4 ! root 66: COMMENTS: 1.1 root 67: 1.1.1.4 ! root 68: Windows recognizes this function by name as the initial entry point ! 69: for the program. This function calls the application initialization ! 70: routine, if no other instance of the program is running, and always ! 71: calls the instance initialization routine. It then executes a message ! 72: retrieval and dispatch loop that is the top-level control structure ! 73: for the remainder of execution. The loop is terminated when a WM_QUIT ! 74: message is received, at which time this function exits the application ! 75: instance by returning the value passed by PostQuitMessage(). 1.1 root 76: 1.1.1.4 ! root 77: If this function must abort before entering the message loop, it ! 78: returns the conventional value NULL. 1.1 root 79: 80: ****************************************************************************/ 1.1.1.3 root 81: int APIENTRY WinMain( 1.1.1.4 ! root 82: HINSTANCE hInstance, ! 83: HINSTANCE hPrevInstance, ! 84: LPSTR lpCmdLine, ! 85: int nCmdShow) 1.1 root 86: { 87: 1.1.1.4 ! root 88: MSG msg; ! 89: HANDLE hAccelTable; 1.1 root 90: 1.1.1.4 ! root 91: if (!hPrevInstance) { // Other instances of app running? ! 92: if (!InitApplication(hInstance)) { // Initialize shared things ! 93: return (FALSE); // Exits if unable to initialize ! 94: } ! 95: } 1.1 root 96: 1.1.1.4 ! root 97: /* Perform initializations that apply to a specific instance */ 1.1 root 98: 1.1.1.4 ! root 99: if (!InitInstance(hInstance, nCmdShow)) { ! 100: return (FALSE); ! 101: } 1.1 root 102: 1.1.1.4 ! root 103: hAccelTable = LoadAccelerators (hInstance, szAppName); 1.1 root 104: 1.1.1.4 ! root 105: /* Acquire and dispatch messages until a WM_QUIT message is received. */ 1.1 root 106: 1.1.1.4 ! root 107: while (GetMessage(&msg, // message structure ! 108: NULL, // handle of window receiving the message ! 109: 0, // lowest message to examine ! 110: 0)) // highest message to examine ! 111: { ! 112: if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg)) { ! 113: TranslateMessage(&msg);// Translates virtual key codes ! 114: DispatchMessage(&msg); // Dispatches message to window ! 115: } ! 116: } 1.1.1.3 root 117: 118: 1.1.1.4 ! root 119: return (msg.wParam); // Returns the value from PostQuitMessage 1.1.1.3 root 120: 1.1.1.4 ! root 121: lpCmdLine; // This will prevent 'unused formal parameter' warnings 1.1 root 122: } 123: 124: 125: /**************************************************************************** 126: 1.1.1.4 ! root 127: FUNCTION: InitApplication(HINSTANCE) 1.1 root 128: 1.1.1.4 ! root 129: PURPOSE: Initializes window data and registers window class 1.1 root 130: 1.1.1.4 ! root 131: COMMENTS: 1.1 root 132: 1.1.1.4 ! root 133: This function is called at initialization time only if no other ! 134: instances of the application are running. This function performs ! 135: initialization tasks that can be done once for any number of running ! 136: instances. 1.1 root 137: 1.1.1.4 ! root 138: In this case, we initialize a window class by filling out a data ! 139: structure of type WNDCLASS and calling the Windows RegisterClass() ! 140: function. Since all instances of this application use the same window ! 141: class, we only need to do this when the first instance is initialized. 1.1 root 142: 143: 144: ****************************************************************************/ 145: 1.1.1.4 ! root 146: BOOL InitApplication(HINSTANCE hInstance) 1.1 root 147: { 1.1.1.4 ! root 148: WNDCLASS wc; 1.1 root 149: 1.1.1.4 ! root 150: // Fill in window class structure with parameters that describe the ! 151: // main window. 1.1 root 152: 1.1.1.4 ! root 153: wc.style = CS_HREDRAW | CS_VREDRAW;// Class style(s). ! 154: wc.lpfnWndProc = (WNDPROC)WndProc; // Window Procedure ! 155: wc.cbClsExtra = 0; // No per-class extra data. ! 156: wc.cbWndExtra = 0; // No per-window extra data. ! 157: wc.hInstance = hInstance; // Owner of this class ! 158: wc.hIcon = LoadIcon (hInstance, szAppName); // Icon name from .RC ! 159: wc.hCursor = LoadCursor(NULL, IDC_ARROW);// Cursor ! 160: wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);// Default color ! 161: wc.lpszMenuName = szAppName; // Menu name from .RC ! 162: wc.lpszClassName = szAppName; // Name to register as 1.1 root 163: 1.1.1.4 ! root 164: // Register the window class and return success/failure code. ! 165: return (RegisterClass(&wc)); 1.1 root 166: } 167: 168: 169: /**************************************************************************** 170: 1.1.1.4 ! root 171: FUNCTION: InitInstance(HINSTANCE, int) 1.1.1.3 root 172: 1.1.1.4 ! root 173: PURPOSE: Saves instance handle and creates main window 1.1 root 174: 1.1.1.4 ! root 175: COMMENTS: 1.1 root 176: 1.1.1.4 ! root 177: This function is called at initialization time for every instance of ! 178: this application. This function performs initialization tasks that ! 179: cannot be shared by multiple instances. 1.1 root 180: 1.1.1.4 ! root 181: In this case, we save the instance handle in a static variable and ! 182: create and display the main program window. 1.1 root 183: 184: ****************************************************************************/ 185: 186: BOOL InitInstance( 1.1.1.4 ! root 187: HINSTANCE hInstance, ! 188: int nCmdShow) 1.1 root 189: { 1.1.1.4 ! root 190: HWND hWnd; // Main window handle. ! 191: ! 192: // Save the instance handle in static variable, which will be used in ! 193: // many subsequence calls from this application to Windows. 1.1.1.3 root 194: 1.1.1.4 ! root 195: hInst = hInstance; // Store instance handle in our global variable 1.1 root 196: 1.1.1.4 ! root 197: // Create a main window for this application instance. 1.1 root 198: 1.1.1.4 ! root 199: hWnd = CreateWindow( ! 200: szAppName, // See RegisterClass() call. ! 201: szTitle, // Text for window title bar. ! 202: WS_OVERLAPPEDWINDOW,// Window style. ! 203: CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, // Use default positioning ! 204: NULL, // Overlapped windows have no parent. ! 205: NULL, // Use the window class menu. ! 206: hInstance, // This instance owns this window. ! 207: NULL // We don't use any data in our WM_CREATE ! 208: ); ! 209: ! 210: // If window could not be created, return "failure" ! 211: if (!hWnd) { ! 212: return (FALSE); ! 213: } ! 214: ! 215: // Make the window visible; update its client area; and return "success" ! 216: ShowWindow(hWnd, nCmdShow); // Show the window ! 217: UpdateWindow(hWnd); // Sends WM_PAINT message ! 218: ! 219: /* ! 220: ** DEMO MODE - PostMessage used for Demonstration only ! 221: */ 1.1 root 222: 1.1.1.4 ! root 223: PostMessage(hWnd, WM_COMMAND, IDM_ABOUT, 0); 1.1 root 224: 1.1.1.4 ! root 225: return (TRUE); // We succeeded... 1.1 root 226: 227: } 228: 229: /**************************************************************************** 230: 1.1.1.4 ! root 231: FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 1.1 root 232: 1.1.1.4 ! root 233: PURPOSE: Processes messages 1.1 root 234: 1.1.1.4 ! root 235: MESSAGES: 1.1 root 236: 1.1.1.4 ! root 237: WM_COMMAND - application menu (About dialog box) ! 238: WM_DESTROY - destroy window 1.1 root 239: 1.1.1.4 ! root 240: COMMENTS: 1.1 root 241: 1.1.1.4 ! root 242: To process the IDM_ABOUT message, call MakeProcInstance() to get the ! 243: current instance address of the About() function. Then call Dialog ! 244: box which will create the box according to the information in your ! 245: generic.rc file and turn control over to the About() function. When ! 246: it returns, free the intance address. 1.1 root 247: 248: ****************************************************************************/ 249: 1.1.1.4 ! root 250: LRESULT CALLBACK WndProc( ! 251: HWND hWnd, // window handle ! 252: UINT message, // type of message ! 253: WPARAM uParam, // additional information ! 254: LPARAM lParam) // additional information 1.1 root 255: { 1.1.1.4 ! root 256: FARPROC lpProcAbout; // pointer to the "About" function ! 257: int wmId, wmEvent; 1.1 root 258: 1.1.1.4 ! root 259: switch (message) { 1.1.1.3 root 260: 1.1.1.4 ! root 261: case WM_COMMAND: // message: command from application menu 1.1.1.3 root 262: 263: // Message packing of uParam and lParam have changed for Win32, let us 264: // handle the differences in a conditional compilation: 1.1.1.4 ! root 265: #if defined (_WIN32) || defined(WIN32) ! 266: wmId = LOWORD(uParam); ! 267: wmEvent = HIWORD(uParam); 1.1.1.3 root 268: #else 1.1.1.4 ! root 269: wmId = uParam; ! 270: wmEvent = HIWORD(lParam); 1.1.1.3 root 271: #endif 272: 1.1.1.4 ! root 273: switch (wmId) { ! 274: case IDM_ABOUT: ! 275: lpProcAbout = MakeProcInstance((FARPROC)About, hInst); ! 276: ! 277: DialogBox(hInst, // current instance ! 278: "AboutBox", // dlg resource to use ! 279: hWnd, // parent handle ! 280: (DLGPROC)lpProcAbout); // About() instance address ! 281: ! 282: FreeProcInstance(lpProcAbout); ! 283: break; ! 284: ! 285: case IDM_EXIT: ! 286: DestroyWindow (hWnd); ! 287: break; ! 288: ! 289: case IDM_HELPCONTENTS: ! 290: if (!WinHelp (hWnd, "GENERIC.HLP", HELP_KEY,(DWORD)(LPSTR)"CONTENTS")) { ! 291: MessageBox (GetFocus(), ! 292: "Unable to activate help", ! 293: szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND); ! 294: } ! 295: break; ! 296: ! 297: case IDM_HELPSEARCH: ! 298: if (!WinHelp(hWnd, "GENERIC.HLP", HELP_PARTIALKEY, (DWORD)(LPSTR)"")) { ! 299: MessageBox (GetFocus(), ! 300: "Unable to activate help", ! 301: szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND); ! 302: } ! 303: break; ! 304: ! 305: case IDM_HELPHELP: ! 306: if(!WinHelp(hWnd, (LPSTR)NULL, HELP_HELPONHELP, 0)) { ! 307: MessageBox (GetFocus(), ! 308: "Unable to activate help", ! 309: szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND); ! 310: } ! 311: break; ! 312: ! 313: // Here are all the other possible menu options, ! 314: // all of these are currently disabled: ! 315: case IDM_NEW: ! 316: case IDM_OPEN: ! 317: case IDM_SAVE: ! 318: case IDM_SAVEAS: ! 319: case IDM_UNDO: ! 320: case IDM_CUT: ! 321: case IDM_COPY: ! 322: case IDM_PASTE: ! 323: case IDM_LINK: ! 324: case IDM_LINKS: ! 325: ! 326: default: ! 327: return (DefWindowProc(hWnd, message, uParam, lParam)); ! 328: } ! 329: break; ! 330: ! 331: case WM_DESTROY: // message: window being destroyed ! 332: PostQuitMessage(0); ! 333: break; ! 334: ! 335: default: // Passes it on if unproccessed ! 336: return (DefWindowProc(hWnd, message, uParam, lParam)); ! 337: } ! 338: return (0); 1.1.1.3 root 339: } 340: 341: /**************************************************************************** 342: 1.1.1.4 ! root 343: FUNCTION: CenterWindow (HWND, HWND) 1.1.1.3 root 344: 1.1.1.4 ! root 345: PURPOSE: Center one window over another 1.1.1.3 root 346: 1.1.1.4 ! root 347: COMMENTS: 1.1.1.3 root 348: 1.1.1.4 ! root 349: Dialog boxes take on the screen position that they were designed at, ! 350: which is not always appropriate. Centering the dialog over a particular ! 351: window usually results in a better position. 1.1.1.3 root 352: 353: ****************************************************************************/ 354: 355: BOOL CenterWindow (HWND hwndChild, HWND hwndParent) 356: { 1.1.1.4 ! root 357: RECT rChild, rParent; ! 358: int wChild, hChild, wParent, hParent; ! 359: int wScreen, hScreen, xNew, yNew; ! 360: HDC hdc; ! 361: ! 362: // Get the Height and Width of the child window ! 363: GetWindowRect (hwndChild, &rChild); ! 364: wChild = rChild.right - rChild.left; ! 365: hChild = rChild.bottom - rChild.top; ! 366: ! 367: // Get the Height and Width of the parent window ! 368: GetWindowRect (hwndParent, &rParent); ! 369: wParent = rParent.right - rParent.left; ! 370: hParent = rParent.bottom - rParent.top; ! 371: ! 372: // Get the display limits ! 373: hdc = GetDC (hwndChild); ! 374: wScreen = GetDeviceCaps (hdc, HORZRES); ! 375: hScreen = GetDeviceCaps (hdc, VERTRES); ! 376: ReleaseDC (hwndChild, hdc); ! 377: ! 378: // Calculate new X position, then adjust for screen ! 379: xNew = rParent.left + ((wParent - wChild) /2); ! 380: if (xNew < 0) { ! 381: xNew = 0; ! 382: } else if ((xNew+wChild) > wScreen) { ! 383: xNew = wScreen - wChild; ! 384: } ! 385: ! 386: // Calculate new Y position, then adjust for screen ! 387: yNew = rParent.top + ((hParent - hChild) /2); ! 388: if (yNew < 0) { ! 389: yNew = 0; ! 390: } else if ((yNew+hChild) > hScreen) { ! 391: yNew = hScreen - hChild; ! 392: } ! 393: ! 394: // Set it, and return ! 395: return SetWindowPos (hwndChild, NULL, ! 396: xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER); 1.1 root 397: } 398: 399: 400: /**************************************************************************** 401: 1.1.1.4 ! root 402: FUNCTION: About(HWND, UINT, WPARAM, LPARAM) 1.1 root 403: 1.1.1.4 ! root 404: PURPOSE: Processes messages for "About" dialog box 1.1 root 405: 1.1.1.4 ! root 406: MESSAGES: 1.1 root 407: 1.1.1.4 ! root 408: WM_INITDIALOG - initialize dialog box ! 409: WM_COMMAND - Input received 1.1 root 410: 1.1.1.4 ! root 411: COMMENTS: 1.1 root 412: 1.1.1.4 ! root 413: Display version information from the version section of the ! 414: application resource. 1.1 root 415: 1.1.1.4 ! root 416: Wait for user to click on "Ok" button, then close the dialog box. 1.1 root 417: 418: ****************************************************************************/ 419: 1.1.1.4 ! root 420: LRESULT CALLBACK About( ! 421: HWND hDlg, // window handle of the dialog box ! 422: UINT message, // type of message ! 423: WPARAM uParam, // message-specific information ! 424: LPARAM lParam) 1.1 root 425: { 1.1.1.4 ! root 426: static HFONT hfontDlg; ! 427: LPSTR lpVersion; ! 428: DWORD dwVerInfoSize; ! 429: DWORD dwVerHnd; ! 430: UINT uVersionLen; ! 431: WORD wRootLen; ! 432: BOOL bRetCode; ! 433: int i; ! 434: char szFullPath[256]; ! 435: char szResult[256]; ! 436: char szGetName[256]; ! 437: ! 438: switch (message) { ! 439: case WM_INITDIALOG: // message: initialize dialog box ! 440: // Create a font to use ! 441: hfontDlg = CreateFont(14, 0, 0, 0, 0, 0, 0, 0, ! 442: 0, 0, 0, 0, ! 443: VARIABLE_PITCH | FF_SWISS, ""); ! 444: ! 445: // Center the dialog over the application window ! 446: CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER)); ! 447: ! 448: // Get version information from the application ! 449: GetModuleFileName (hInst, szFullPath, sizeof(szFullPath)); ! 450: dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd); ! 451: if (dwVerInfoSize) { ! 452: // If we were able to get the information, process it: ! 453: LPSTR lpstrVffInfo; ! 454: HANDLE hMem; ! 455: hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize); ! 456: lpstrVffInfo = GlobalLock(hMem); ! 457: GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpstrVffInfo); ! 458: lstrcpy(szGetName, "\\StringFileInfo\\040904E4\\"); ! 459: wRootLen = lstrlen(szGetName); ! 460: ! 461: // Walk through the dialog items that we want to replace: ! 462: for (i = DLG_VERFIRST; i <= DLG_VERLAST; i++) { ! 463: GetDlgItemText(hDlg, i, szResult, sizeof(szResult)); ! 464: szGetName[wRootLen] = (char)0; ! 465: lstrcat (szGetName, szResult); ! 466: uVersionLen = 0; ! 467: lpVersion = NULL; ! 468: bRetCode = VerQueryValue((LPVOID)lpstrVffInfo, ! 469: (LPSTR)szGetName, ! 470: (LPVOID)&lpVersion, ! 471: #if defined (_WIN32) || defined(WIN32) ! 472: (LPDWORD)&uVersionLen); // For MIPS strictness 1.1.1.3 root 473: #else 1.1.1.4 ! root 474: (UINT *)&uVersionLen); 1.1.1.3 root 475: #endif 476: 1.1.1.4 ! root 477: if ( bRetCode && uVersionLen && lpVersion) { ! 478: // Replace dialog item text with version info ! 479: lstrcpy(szResult, lpVersion); ! 480: SetDlgItemText(hDlg, i, szResult); ! 481: SendMessage (GetDlgItem (hDlg, i), WM_SETFONT, (UINT)hfontDlg, TRUE); ! 482: } ! 483: } // for (i = DLG_VERFIRST; i <= DLG_VERLAST; i++) ! 484: ! 485: GlobalUnlock(hMem); ! 486: GlobalFree(hMem); ! 487: } // if (dwVerInfoSize) ! 488: ! 489: return (TRUE); ! 490: ! 491: case WM_COMMAND: // message: received a command ! 492: if (LOWORD(uParam) == IDOK // "OK" box selected? ! 493: || LOWORD(uParam) == IDCANCEL) { // System menu close command? ! 494: EndDialog(hDlg, TRUE); // Exit the dialog ! 495: DeleteObject (hfontDlg); ! 496: return (TRUE); ! 497: } ! 498: break; ! 499: } ! 500: return (FALSE); // Didn't process the message 1.1.1.2 root 501: 1.1.1.4 ! root 502: lParam; // This will prevent 'unused formal parameter' warnings 1.1.1.3 root 503: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.