home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / menu / gsysmenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.8 KB  |  136 lines

  1. /*
  2.  *
  3.  *   This program demonstrates the use of the GetSystemMenu() function.
  4.  *   GetSystemMenu() returns a handle to the system menu of the specified
  5.  *   Window. GetSystemMenu() is called in WinMain and then the handle
  6.  *   returned by GetSystemMenu() is used to add a "Get....." option to
  7.  *   the system menu.
  8.  *
  9.  *   Windows Version 2.0 function demonstration application
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. #define IDS_MY_OPTION 99
  16.  
  17. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  18.  
  19. /* Procedure called when the application is loaded for the first time */
  20. BOOL HelloInit( hInstance )
  21. HANDLE hInstance;
  22. {
  23.     PWNDCLASS   pHelloClass;
  24.  
  25.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  26.  
  27.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  28.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  29.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  30.     pHelloClass->lpszClassName    = (LPSTR)"GetSystemMenu";
  31.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  32.     pHelloClass->hInstance      = hInstance;
  33.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  34.     pHelloClass->lpfnWndProc    = HelloWndProc;
  35.  
  36.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  37.         /* Initialization failed.
  38.          * Windows will automatically deallocate all allocated memory.
  39.          */
  40.         return FALSE;
  41.  
  42.     LocalFree( (HANDLE)pHelloClass );
  43.     return TRUE;        /* Initialization succeeded */
  44. }
  45.  
  46. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  47. HANDLE hInstance, hPrevInstance;
  48. LPSTR lpszCmdLine;
  49. int cmdShow;
  50. {
  51.     MSG   msg;
  52.     HWND  hWnd;
  53.     HMENU hMenu;
  54.  
  55.     if (!HelloInit( hInstance ))
  56.     return (FALSE);
  57.  
  58.     hWnd = CreateWindow((LPSTR)"GetSystemMenu",
  59.             (LPSTR)"GetSystemMenu()",
  60.             WS_OVERLAPPEDWINDOW,
  61.             CW_USEDEFAULT,
  62.             CW_USEDEFAULT,
  63.             CW_USEDEFAULT,
  64.             CW_USEDEFAULT,
  65.                         (HWND)NULL,        /* no parent */
  66.                         (HMENU)NULL,       /* use class menu */
  67.                         (HANDLE)hInstance, /* handle to window instance */
  68.                         (LPSTR)NULL        /* no params to pass on */
  69.                         );
  70.  
  71. /*** retrieve the system menu as it currently exists ***/
  72.     hMenu = GetSystemMenu(hWnd, FALSE);
  73.  
  74. /*   Insert "Get....." into system menu   */
  75.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  76.     ChangeMenu(hMenu, 0, (LPSTR)"Get.....", IDS_MY_OPTION, MF_APPEND | MF_STRING);
  77.  
  78.     /* Make window visible according to the way the app is activated */
  79.     ShowWindow( hWnd, cmdShow );
  80.     UpdateWindow( hWnd );
  81.  
  82.     /* Polling messages from event queue */
  83.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  84.         TranslateMessage((LPMSG)&msg);
  85.         DispatchMessage((LPMSG)&msg);
  86.         }
  87.  
  88.     return (int)msg.wParam;
  89. }
  90.  
  91. /* Procedures which make up the window class. */
  92. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  93. HWND hWnd;
  94. unsigned message;
  95. WORD wParam;
  96. LONG lParam;
  97. {
  98.     PAINTSTRUCT ps;
  99.  
  100.     switch (message)
  101.     {
  102.     case WM_SYSCOMMAND:
  103.         switch (wParam)
  104.         {
  105.     case IDS_MY_OPTION:
  106.         MessageBox(NULL,
  107.                (LPSTR)"'Get.....' option chosen",
  108.                (LPSTR)"System Menu:",
  109.                MB_OK);
  110.         break;
  111.         default:
  112.             return DefWindowProc( hWnd, message, wParam, lParam );
  113.         }
  114.         break;
  115.  
  116.     case WM_DESTROY:
  117.         PostQuitMessage( 0 );
  118.         break;
  119.  
  120.     case WM_PAINT:
  121.     BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  122.     TextOut(ps.hdc,
  123.         5,
  124.         5,
  125.         (LPSTR)"The system menu has had an option added to it ('Get.....')",
  126.         (long)58);
  127.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  128.         break;
  129.  
  130.     default:
  131.         return DefWindowProc( hWnd, message, wParam, lParam );
  132.         break;
  133.     }
  134.     return(0L);
  135. }
  136.