home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / dialog / mapdlgr.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  4KB  |  140 lines

  1. /*
  2.  *  Function (s) demonstrated in this program: MapDialogRect
  3.  *  Description:
  4.  *     When the About dialog box is brought up, it has two buttons.  The OK
  5.  *  button to end the dialog box, and the DRAW button which draws an
  6.  *  ellipse in the upper left hand corner.  Coordinates for the ellipse
  7.  *  are converted from dialog coordinates to screen coordinates using 
  8.  *  the MapDialogRect function.
  9.  */
  10.  
  11. #include "windows.h"    
  12. #include "mapdlgr.h"    
  13.  
  14. HANDLE hInst;
  15.  
  16. /**************************************************************************/
  17.  
  18. int     PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  19. HANDLE    hInstance;
  20. HANDLE    hPrevInstance;
  21. LPSTR     lpCmdLine;
  22. int       nCmdShow;
  23.   {
  24.   HWND hWnd;
  25.   MSG msg;
  26.   WNDCLASS WndClass;
  27.  
  28.   WndClass.style = NULL;
  29.   WndClass.lpfnWndProc = MapDlgRectWndProc;
  30.   WndClass.hInstance = hInstance;
  31.   WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  32.   WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
  33.   WndClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  34.   WndClass.lpszMenuName = (LPSTR) NULL;
  35.   WndClass.lpszClassName = (LPSTR) "MapDlgRect";
  36.  
  37.   if (!RegisterClass ((PWNDCLASS)&WndClass))
  38.     return FALSE;
  39.  
  40.   hInst = hInstance;
  41.  
  42.   hWnd = CreateWindow ("MapDlgRect", (LPSTR)"MapDlgRect Sample Application",               /* window name             */
  43.                        WS_OVERLAPPEDWINDOW,
  44.                        CW_USEDEFAULT, 0,
  45.                        CW_USEDEFAULT, 0,
  46.                        NULL, NULL, hInstance, NULL);
  47.   if (!hWnd)
  48.     return (NULL);
  49.  
  50.   ShowWindow (hWnd, nCmdShow);
  51.   UpdateWindow (hWnd);
  52.  
  53.   while (GetMessage (&msg, NULL, NULL, NULL))
  54.     {
  55.     TranslateMessage (&msg);
  56.     DispatchMessage (&msg);
  57.     }
  58.   return (msg.wParam);
  59.   }
  60.  
  61. long    FAR PASCAL MapDlgRectWndProc (hWnd, message, wParam, lParam)
  62. HWND      hWnd;
  63. unsigned  message;
  64. WORD      wParam;
  65. LONG      lParam;
  66.   {
  67.   FARPROC lpProcAbout;
  68.   HMENU hMenu;
  69.  
  70.   switch (message)
  71.     {
  72.     case WM_SYSCOMMAND:           /* message: command from system menu */
  73.       if (wParam == ID_ABOUT)
  74.         {
  75.         lpProcAbout = MakeProcInstance (About, hInst);
  76.         DialogBox (hInst, "AboutBox", hWnd,        lpProcAbout);
  77.         FreeProcInstance (lpProcAbout);
  78.         break;
  79.         }
  80.       else
  81.         return (DefWindowProc (hWnd, message, wParam, lParam));
  82.  
  83.     case WM_CREATE:
  84.       hMenu = GetSystemMenu (hWnd, FALSE);
  85.       ChangeMenu (hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
  86.       ChangeMenu (hMenu, NULL, "A&bout MapDlgRect...", ID_ABOUT,
  87.                   MF_APPEND | MF_STRING);
  88.       break;
  89.  
  90.     case WM_DESTROY:                /* message: window being destroyed */
  91.       PostQuitMessage (0);
  92.       break;
  93.  
  94.     default:                        /* Passes it on if unproccessed    */
  95.       return (DefWindowProc (hWnd, message, wParam, lParam));
  96.     }
  97.   return (NULL);
  98.   }
  99.  
  100. BOOL FAR PASCAL About (hDlg, message, wParam, lParam)
  101. HWND       hDlg;
  102. unsigned   message;
  103. WORD       wParam;
  104. LONG       lParam;
  105.   {
  106.   RECT      rRect;     /*  Rectangle to draw elipse onto Dialog box  */
  107.   HDC       hDC;       /*  Handle to display context  */
  108.  
  109.   switch (message)
  110.     {
  111.     case WM_INITDIALOG:                /* message: initialize dialog box */
  112.       return (TRUE);
  113.  
  114.     case WM_COMMAND:                    /* message: received a command */
  115.       if (wParam == IDOK)
  116.         {         /* "OK" box selected?          */
  117.         EndDialog (hDlg, NULL);          /* Exits the dialog box        */
  118.         return (TRUE);
  119.         }
  120.       if (wParam == ID_DRAW)  /*  If they hit the Draw button, draw
  121.                                *  an ellipse in the dialog box  */
  122.         {
  123.         hDC = GetDC (hDlg);      /* get DC for the dialog box */
  124.         rRect.left = 0;          /* These coordinates are in dialog
  125.                                   * box units.  They will be converted
  126.                                   * to screen coordinates by the
  127.                                   * MapDialogRect function  */
  128.         rRect.top = 0;
  129.         rRect.right = 20;
  130.         rRect.bottom = 10;
  131.         MapDialogRect (hDlg, (LPRECT) & rRect);
  132.         Ellipse (hDC, rRect.left, rRect.top, rRect.right, rRect.bottom);
  133.         ReleaseDC (hDlg, hDC);
  134.         return (TRUE);
  135.         }
  136.       break;
  137.     }
  138.   return (FALSE);
  139.   }
  140.