home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / display / getdcpos.c next >
C/C++ Source or Header  |  1988-08-10  |  6KB  |  193 lines

  1. /*
  2.  
  3. Function(s) demonstrated in this program: GetCurrentPosition
  4.  
  5. Windows version:  2.03
  6.  
  7. Windows SDK version:  2.00
  8.  
  9. Compiler version:  C 5.10
  10.  
  11. Description:  This function returns the current position of the Display
  12.    Context.
  13.  
  14. Additional Comments:
  15.  
  16. */
  17.  
  18. #define NOMINMAX
  19. #include <windows.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "GetDCPos.h"
  23.  
  24.  
  25. HWND     hWndParent1, hWndParent2, hWndChild1, hWndChild2;
  26. HANDLE   hInstMain;
  27.  
  28. char     szOutputBuffer1 [70];
  29. char     szOutputBuffer2 [500];
  30.  
  31.  
  32. /****************************************************************************/
  33. /************************    Message Structure      *************************/
  34. /****************************************************************************/
  35.  
  36. struct { char *szMessage; }
  37.        Messages [] = {
  38. "About\0",
  39. "     This is a sample application to demonstrate the\n\
  40. use of the GetCurrentPosition Windows function.",
  41.  
  42. "Help Message",
  43. "     This program uses the GetCurrentPosition Windows\n\
  44. function return the current position of the Display\n\
  45. context, this position is then displayed in a Message\n\
  46. Box.  The position has been moved to (x,y) = (100,100)\n\
  47. so this should be displayed when option is chosen\n\
  48. from the menu.",
  49.  
  50. };    
  51.  
  52. /****************************************************************************/
  53.  
  54. void ProcessMessage (HWND, int); 
  55.  
  56. void ProcessMessage (hWnd, MessageNumber) 
  57.      HWND     hWnd;
  58.      int      MessageNumber;
  59. {
  60.      sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  61.      sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  62.      MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  63. }       
  64.  
  65. /****************************************************************************/
  66.  
  67. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  68.      HANDLE      hInstance, hPrevInstance ;
  69.      LPSTR       lpszCmdLine ;
  70.      int         nCmdShow ;
  71.      {
  72.      static char szAppName [] = "GetDCPos" ;
  73.      HWND        hWnd ;
  74.      WNDCLASS    wndclass ;
  75.      MSG msg;
  76.      short       xScreen, yScreen ;
  77.  
  78.      if (!hPrevInstance) 
  79.           {
  80.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  81.           wndclass.lpfnWndProc   = WndProc ;
  82.           wndclass.cbClsExtra    = 0 ;
  83.           wndclass.cbWndExtra    = 0 ;
  84.           wndclass.hInstance     = hInstance ;
  85.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  86.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  87.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  88.           wndclass.lpszMenuName  = szAppName ;
  89.           wndclass.lpszClassName = szAppName ;
  90.  
  91.           if (!RegisterClass (&wndclass))
  92.                return FALSE ;
  93.           }
  94.  
  95.      xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  96.      yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  97.  
  98.      hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  99.                     "GetCurrentPosition",       /* window caption          */
  100.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  101.                     CW_USEDEFAULT,              /* initial x position      */
  102.                     0,                          /* initial y position      */
  103.                     CW_USEDEFAULT,              /* initial x size          */
  104.                     0,                          /* initial y size          */
  105.                     NULL,                       /* parent window handle    */
  106.                     NULL,                       /* window menu handle      */
  107.                     hInstance,                  /* program instance handle */
  108.                     NULL) ;                     /* create parameters       */
  109.  
  110.      ShowWindow (hWndParent1, nCmdShow) ;
  111.      UpdateWindow (hWndParent1) ;
  112.  
  113.      hInstMain = hInstance;
  114.  
  115.      while (GetMessage(&msg, NULL, 0, 0))
  116.      {
  117.       TranslateMessage(&msg);
  118.       DispatchMessage(&msg);
  119.      } 
  120.      return (msg.wParam) ;     
  121.      }
  122.  
  123. /****************************************************************************/
  124.  
  125. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  126. HWND     hWnd ;
  127. unsigned iMessage ;
  128. WORD     wParam ;
  129. LONG     lParam ;
  130. {
  131.  HMENU       hMenu;
  132.  HDC         hDC;
  133.  PAINTSTRUCT ps;
  134.  static int  xClient, yClient;
  135.  DWORD       dWord;
  136.  char        szOutputBuffer [40];
  137.  
  138.  switch(iMessage)
  139.  {
  140.   case WM_CREATE:
  141.        hMenu = GetSystemMenu (hWnd, FALSE);
  142.  
  143.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  144.                    MF_APPEND | MF_STRING);
  145.        break;
  146.  
  147.   case WM_SYSCOMMAND:
  148.        switch (wParam) {
  149.           case IDM_ABOUT:
  150.                ProcessMessage (hWnd, 0);
  151.                break;
  152.           default:
  153.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  154.        }
  155.        break;
  156.  
  157.   case WM_COMMAND:
  158.        switch (wParam) {
  159.           case IDM_GETPOSITION:
  160.                hDC = GetDC ( hWnd );
  161.                MoveTo ( hDC, 100, 100 );
  162.                dWord = GetCurrentPosition (hDC); 
  163.                sprintf (szOutputBuffer1, 
  164.                           "The Current Display Context Position = (%i,%i)",
  165.                         LOWORD (dWord), HIWORD (dWord));
  166.                MessageBox (hWnd, szOutputBuffer1, "GetCurrentPostion", MB_OK);
  167.                ReleaseDC(hWnd, hDC);
  168.                break;
  169.  
  170.           case IDM_HELP:
  171.                ProcessMessage (hWnd, 2);
  172.                break;
  173.        }
  174.        break;
  175.  
  176.   case WM_PAINT:
  177.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  178.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  179.        break;
  180.  
  181.   case WM_DESTROY:
  182.        PostQuitMessage(0);
  183.        break;
  184.  
  185.   default:
  186.   {
  187.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  188.   }
  189.  }
  190.  return (0L); 
  191. }
  192.  
  193.