|
|
1.1 ! root 1: /**************************************************************************** ! 2: ! 3: PROGRAM: Generic.c ! 4: ! 5: PURPOSE: Generic 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: COMMENTS: ! 16: ! 17: Windows can have several copies of your application running at the ! 18: same time. The variable hInst keeps track of which instance this ! 19: application is so that processing will be to the correct window. ! 20: ! 21: ****************************************************************************/ ! 22: ! 23: #define STRICT ! 24: ! 25: ! 26: #include <stddef.h> ! 27: #include "windows.h" /* required for all Windows applications */ ! 28: ! 29: #if defined(WIN32) ! 30: #define WIN32S 0x80000000l // no manifest constance yet??? ! 31: #else ! 32: #define APIENTRY FAR PASCAL ! 33: #define WINAPI FAR PASCAL ! 34: #define TCHAR char ! 35: #define LPTSTR LPSTR ! 36: #define UNREFERENCED_PARAMETER(x) x; ! 37: #define TEXT(x) x ! 38: ! 39: #define WF1_WINNT 0x40 /* A special version of GetWinFlags() will ! 40: * be provide in the WOW Layer which returns ! 41: * a MANEFEST CONSTANT signifing to Win16 ! 42: * apps that it's running in WOW instead of ! 43: * DOS/Windows. Unfortunately this value ! 44: * was not specified at the time of this ! 45: * sample. As *soon* as Development has set ! 46: * on a manifest contanst PSS will upload a ! 47: * KnowledgeBase article. Describing this flag ! 48: * and any other relevent details need to get ! 49: * this sample correctly identifing what platform ! 50: * it's running on. ! 51: */ ! 52: ! 53: #endif ! 54: ! 55: ! 56: #include "generic.h" /* specific to this program */ ! 57: ! 58: ! 59: HINSTANCE hInst; /* current instance */ ! 60: ! 61: ! 62: /**************************************************************************** ! 63: ! 64: FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int) ! 65: ! 66: PURPOSE: calls initialization function, processes message loop ! 67: ! 68: COMMENTS: ! 69: ! 70: Windows recognizes this function by name as the initial entry point ! 71: for the program. This function calls the application initialization ! 72: routine, if no other instance of the program is running, and always ! 73: calls the instance initialization routine. It then executes a message ! 74: retrieval and dispatch loop that is the top-level control structure ! 75: for the remainder of execution. The loop is terminated when a WM_QUIT ! 76: message is received, at which time this function exits the application ! 77: instance by returning the value passed by PostQuitMessage(). ! 78: ! 79: If this function must abort before entering the message loop, it ! 80: returns the conventional value NULL. ! 81: ! 82: ****************************************************************************/ ! 83: ! 84: int WINAPI WinMain(HINSTANCE hInstance, /* current instance */ ! 85: HINSTANCE hPrevInstance, /* previous instance */ ! 86: LPSTR lpCmdLine, /* command line */ ! 87: int nCmdShow) /* show-window type (open/icon) */ ! 88: { ! 89: MSG msg; /* message */ ! 90: ! 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: /* Perform initializations that apply to a specific instance */ ! 96: ! 97: if (!InitInstance(hInstance, nCmdShow)) ! 98: return (FALSE); ! 99: ! 100: /* Acquire and dispatch messages until a WM_QUIT message is received. */ ! 101: ! 102: while (GetMessage(&msg, /* message structure */ ! 103: NULL, /* handle of window receiving the message */ ! 104: 0, /* lowest message to examine */ ! 105: 0)) /* highest message to examine */ ! 106: { ! 107: TranslateMessage(&msg); /* Translates virtual key codes */ ! 108: DispatchMessage(&msg); /* Dispatches message to window */ ! 109: } ! 110: ! 111: return (msg.wParam); /* Returns the value from PostQuitMessage */ ! 112: UNREFERENCED_PARAMETER(lpCmdLine); ! 113: } ! 114: ! 115: ! 116: /**************************************************************************** ! 117: ! 118: FUNCTION: InitApplication(HANDLE) ! 119: ! 120: PURPOSE: Initializes window data and registers window class ! 121: ! 122: COMMENTS: ! 123: ! 124: This function is called at initialization time only if no other ! 125: instances of the application are running. This function performs ! 126: initialization tasks that can be done once for any number of running ! 127: instances. ! 128: ! 129: In this case, we initialize a window class by filling out a data ! 130: structure of type WNDCLASS and calling the Windows RegisterClass() ! 131: function. Since all instances of this application use the same window ! 132: class, we only need to do this when the first instance is initialized. ! 133: ! 134: ! 135: ****************************************************************************/ ! 136: ! 137: BOOL InitApplication(HANDLE hInstance) /* current instance */ ! 138: { ! 139: WNDCLASS wc; ! 140: ! 141: /* Fill in window class structure with parameters that describe the */ ! 142: /* main window. */ ! 143: ! 144: wc.style = 0; /* Class style(s). */ ! 145: wc.lpfnWndProc = (WNDPROC)MainWndProc; /* Function to retrieve messages for */ ! 146: /* windows of this class. */ ! 147: wc.cbClsExtra = 0; /* No per-class extra data. */ ! 148: wc.cbWndExtra = 0; /* No per-window extra data. */ ! 149: wc.hInstance = hInstance; /* Application that owns the class. */ ! 150: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); ! 151: wc.hCursor = LoadCursor(NULL, IDC_ARROW); ! 152: wc.hbrBackground = GetStockObject(WHITE_BRUSH); ! 153: wc.lpszMenuName = "GenericMenu"; /* Name of menu resource in .RC file. */ ! 154: wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */ ! 155: ! 156: /* Register the window class and return success/failure code. */ ! 157: ! 158: return (RegisterClass(&wc)); ! 159: ! 160: } ! 161: ! 162: ! 163: /**************************************************************************** ! 164: ! 165: FUNCTION: InitInstance(HANDLE, int) ! 166: ! 167: PURPOSE: Saves instance handle and creates main window ! 168: ! 169: COMMENTS: ! 170: ! 171: This function is called at initialization time for every instance of ! 172: this application. This function performs initialization tasks that ! 173: cannot be shared by multiple instances. ! 174: ! 175: In this case, we save the instance handle in a static variable and ! 176: create and display the main program window. ! 177: ! 178: ****************************************************************************/ ! 179: ! 180: BOOL InitInstance(HANDLE hInstance, /* Current instance identifier. */ ! 181: int nCmdShow ) /* Param for first ShowWindow() call. */ ! 182: { ! 183: HWND hWnd; /* Main window handle. */ ! 184: ! 185: /* Save the instance handle in static variable, which will be used in */ ! 186: /* many subsequence calls from this application to Windows. */ ! 187: ! 188: hInst = hInstance; ! 189: ! 190: /* Create a main window for this application instance. */ ! 191: ! 192: hWnd = CreateWindow( ! 193: "GenericWClass", /* See RegisterClass() call. */ ! 194: "Generic Sample Application", /* Text for window title bar. */ ! 195: WS_OVERLAPPEDWINDOW, /* Window style. */ ! 196: 10, /* Default horizontal position. */ ! 197: 10, /* Default vertical position. */ ! 198: 600, /* Default width. */ ! 199: 400, /* Default height. */ ! 200: NULL, /* Overlapped windows have no parent. */ ! 201: NULL, /* Use the window class menu. */ ! 202: hInstance, /* This instance owns this window. */ ! 203: NULL /* Pointer not needed. */ ! 204: ); ! 205: ! 206: /* If window could not be created, return "failure" */ ! 207: ! 208: if (!hWnd) ! 209: return (FALSE); ! 210: ! 211: /* Make the window visible; update its client area; and return "success" */ ! 212: ! 213: ShowWindow(hWnd, nCmdShow); /* Show the window */ ! 214: UpdateWindow(hWnd); /* Sends WM_PAINT message */ ! 215: return (TRUE); /* Returns the value from PostQuitMessage */ ! 216: ! 217: } ! 218: ! 219: /**************************************************************************** ! 220: ! 221: FUNCTION: MainWndProc(HWND, WORD, WPARAM, LPARAM) ! 222: ! 223: PURPOSE: Processes messages ! 224: ! 225: MESSAGES: ! 226: ! 227: WM_COMMAND - application menu (About dialog box) ! 228: WM_DESTROY - destroy window ! 229: ! 230: COMMENTS: ! 231: ! 232: To process the IDM_ABOUT message, call MakeProcInstance() to get the ! 233: current instance address of the About() function. Then call Dialog ! 234: box which will create the box according to the information in your ! 235: generic.rc file and turn control over to the About() function. When ! 236: it returns, free the intance address. ! 237: ! 238: ****************************************************************************/ ! 239: ! 240: long FAR PASCAL MainWndProc(HWND hWnd, /* window handle */ ! 241: UINT message, /* type of message */ ! 242: WPARAM wParam, /* additional information */ ! 243: LPARAM lParam ) /* additional information */ ! 244: { ! 245: DLGPROC lpProcAbout; ! 246: DWORD version, flags; ! 247: char buf[80]; ! 248: ! 249: switch (message) { ! 250: ! 251: case WM_COMMAND: /* message: command from application menu */ ! 252: if (LOWORD(wParam) == IDM_ABOUT) { ! 253: lpProcAbout = (DLGPROC)MakeProcInstance((FARPROC)About, hInst ); ! 254: ! 255: DialogBox(hInst, /* current instance */ ! 256: "AboutBox", /* resource to use */ ! 257: hWnd, /* parent handle */ ! 258: lpProcAbout); /* About() instance address */ ! 259: ! 260: FreeProcInstance((FARPROC)lpProcAbout); ! 261: break; ! 262: } ! 263: else if ( LOWORD(wParam) == IDM_PLATFORM ) { ! 264: version = GetVersion(); ! 265: wsprintf( buf, "The version of Windows you're running is %d.%d", ! 266: LOBYTE(LOWORD(version)), HIBYTE(LOWORD(version)) ); ! 267: ! 268: MessageBox( hWnd, buf, "Platform", MB_OK ); ! 269: #if defined(WIN32) ! 270: /* ! 271: * bugbug Currently HIWORD of version is 500. The highbit is not set ! 272: */ ! 273: ! 274: if ( version & WIN32S ) /* High bit set */ ! 275: wsprintf ( buf, "This is running on Win32s" ); ! 276: else ! 277: wsprintf ( buf, "This is running on Win32/NT" ); ! 278: ! 279: wsprintf( buf, "Verion = %#x", HIWORD(version) ); ! 280: MessageBox( hWnd, buf, "Platform", MB_OK ); ! 281: ! 282: ! 283: UNREFERENCED_PARAMETER(flags); ! 284: #else ! 285: flags = GetWinFlags(); ! 286: wsprintf ( buf, "flags are %#lx", flags ); ! 287: MessageBox ( hWnd, buf, NULL, MB_OK ); ! 288: ! 289: if ( flags & WF1_WINNT ) ! 290: wsprintf ( buf, "This is running in the WOW layer" ); ! 291: else ! 292: wsprintf( buf, "The version of MS-DOS you're running is %d.%d", ! 293: HIBYTE(HIWORD(version)), LOBYTE(HIWORD(version)) ); ! 294: #endif ! 295: MessageBox( hWnd, buf, "Platform", MB_OK ); ! 296: } ! 297: else /* Lets Windows process it */ ! 298: return (DefWindowProc(hWnd, message, wParam, lParam)); ! 299: ! 300: case WM_DESTROY: /* message: window being destroyed */ ! 301: PostQuitMessage(0); ! 302: break; ! 303: ! 304: default: /* Passes it on if unproccessed */ ! 305: return (DefWindowProc(hWnd, message, wParam, lParam)); ! 306: } ! 307: return ( 0 ); ! 308: } ! 309: ! 310: ! 311: /**************************************************************************** ! 312: ! 313: FUNCTION: About(HWND, unsigned, WORD, LONG) ! 314: ! 315: PURPOSE: Processes messages for "About" dialog box ! 316: ! 317: MESSAGES: ! 318: ! 319: WM_INITDIALOG - initialize dialog box ! 320: WM_COMMAND - Input received ! 321: ! 322: COMMENTS: ! 323: ! 324: No initialization is needed for this particular dialog box, but TRUE ! 325: must be returned to Windows. ! 326: ! 327: Wait for user to click on "Ok" button, then close the dialog box. ! 328: ! 329: ****************************************************************************/ ! 330: ! 331: BOOL FAR PASCAL About(HWND hDlg, /* window handle of the dialog box */ ! 332: UINT message, /* type of message */ ! 333: WPARAM wParam, /* message-specific information */ ! 334: LPARAM lParam ) ! 335: { ! 336: switch (message) { ! 337: case WM_INITDIALOG: /* message: initialize dialog box */ ! 338: return (TRUE); ! 339: ! 340: case WM_COMMAND: /* message: received a command */ ! 341: if (wParam == IDOK /* "OK" box selected? */ ! 342: || wParam == IDCANCEL) { /* System menu close command? */ ! 343: EndDialog(hDlg, TRUE); /* Exits the dialog box */ ! 344: return (TRUE); ! 345: } ! 346: break; ! 347: } ! 348: return (FALSE); /* Didn't process a message */ ! 349: UNREFERENCED_PARAMETER(lParam); ! 350: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.