home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_sdk / icon / icon.c next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  4.6 KB  |  201 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Icon.c
  4.  
  5.     PURPOSE: Demonstrates using an icon for the About box and minimize box
  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 "icon.h"
  19.  
  20. HANDLE hInst;
  21.  
  22. /****************************************************************************
  23.  
  24.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  25.  
  26.     PURPOSE: calls initialization function, processes message loop
  27.  
  28. ****************************************************************************/
  29.  
  30. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  31. HANDLE hInstance;
  32. HANDLE hPrevInstance;
  33. LPSTR lpCmdLine;
  34. int nCmdShow;
  35. {
  36.     MSG msg;
  37.  
  38.     if (!hPrevInstance)
  39.     if (!InitApplication(hInstance))
  40.         return (FALSE);
  41.  
  42.     if (!InitInstance(hInstance, nCmdShow))
  43.         return (FALSE);
  44.  
  45.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  46.     TranslateMessage(&msg);
  47.     DispatchMessage(&msg);
  48.     }
  49.     return (msg.wParam);
  50. }
  51.  
  52.  
  53. /****************************************************************************
  54.  
  55.     FUNCTION: InitApplication(HANDLE)
  56.  
  57.     PURPOSE: Initializes window data and registers window class
  58.  
  59. ****************************************************************************/
  60.  
  61. BOOL InitApplication(hInstance)
  62. HANDLE hInstance;
  63. {
  64.     WNDCLASS  wc;
  65.  
  66.     wc.style = NULL;
  67.     wc.lpfnWndProc = MainWndProc;
  68.     wc.cbClsExtra = 0;
  69.     wc.cbWndExtra = 0;
  70.     wc.hInstance = hInstance;
  71.     wc.hIcon = LoadIcon(hInstance, "MyIcon");           /* loads icon */
  72.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  73.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  74.     wc.lpszMenuName =  "IconMenu";
  75.     wc.lpszClassName = "IconWClass";
  76.  
  77.     return (RegisterClass(&wc));
  78. }
  79.  
  80.  
  81. /****************************************************************************
  82.  
  83.     FUNCTION:  InitInstance(HANDLE, int)
  84.  
  85.     PURPOSE:  Saves instance handle and creates main window
  86.  
  87. ****************************************************************************/
  88.  
  89. BOOL InitInstance(hInstance, nCmdShow)
  90.     HANDLE          hInstance;
  91.     int             nCmdShow;
  92. {
  93.     HWND            hWnd;
  94.  
  95.     hInst = hInstance;
  96.  
  97.     hWnd = CreateWindow(
  98.         "IconWClass",
  99.         "Icon Sample Application",
  100.         WS_OVERLAPPEDWINDOW,
  101.         CW_USEDEFAULT,
  102.         CW_USEDEFAULT,
  103.         CW_USEDEFAULT,
  104.         CW_USEDEFAULT,
  105.         NULL,
  106.         NULL,
  107.         hInstance,
  108.         NULL
  109.     );
  110.  
  111.     if (!hWnd)
  112.         return (FALSE);
  113.  
  114.     ShowWindow(hWnd, nCmdShow);
  115.     UpdateWindow(hWnd);
  116.     return (TRUE);
  117.  
  118. }
  119.  
  120. /****************************************************************************
  121.  
  122.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  123.  
  124.     PURPOSE:  Processes messages
  125.  
  126.     MESSAGES:
  127.  
  128.     WM_COMMAND    - application menu (About dialog box)
  129.     WM_DESTROY    - destroy window
  130.  
  131. ****************************************************************************/
  132.  
  133. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  134. HWND hWnd;
  135. unsigned message;
  136. WORD wParam;
  137. LONG lParam;
  138. {
  139.     FARPROC lpProcAbout;
  140.  
  141.     switch (message) {
  142.     case WM_COMMAND:
  143.         if (wParam == IDM_ABOUT) {
  144.         lpProcAbout = MakeProcInstance(About, hInst);
  145.  
  146.         DialogBox(hInst,
  147.             "AboutBox",
  148.             hWnd,
  149.             lpProcAbout);
  150.  
  151.         FreeProcInstance(lpProcAbout);
  152.         break;
  153.         }
  154.         else
  155.         return (DefWindowProc(hWnd, message, wParam, lParam));
  156.  
  157.     case WM_DESTROY:
  158.         PostQuitMessage(0);
  159.         break;
  160.  
  161.     default:
  162.         return (DefWindowProc(hWnd, message, wParam, lParam));
  163.     }
  164.     return (NULL);
  165. }
  166.  
  167.  
  168. /****************************************************************************
  169.  
  170.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  171.  
  172.     PURPOSE:  Processes messages for "About" dialog box
  173.  
  174.     MESSAGES:
  175.  
  176.     WM_INITDIALOG - initialize dialog box
  177.     WM_COMMAND    - Input received
  178.  
  179. ****************************************************************************/
  180.  
  181. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  182. HWND hDlg;
  183. unsigned message;
  184. WORD wParam;
  185. LONG lParam;
  186. {
  187.     switch (message) {
  188.     case WM_INITDIALOG:
  189.         return (TRUE);
  190.  
  191.     case WM_COMMAND:
  192.         if (wParam == IDOK
  193.                 || wParam == IDCANCEL) {
  194.         EndDialog(hDlg, TRUE);
  195.         return (TRUE);
  196.         }
  197.         break;
  198.     }
  199.     return (FALSE);
  200. }
  201.