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

  1. #include "windows.h"
  2.  
  3. int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  4. BOOL GetCRectInit(HANDLE);
  5. long FAR PASCAL GetCRectWndProc(HWND, unsigned, WORD, LONG);
  6.  
  7. HANDLE hInst;
  8.  
  9. /**************************************************************************/
  10.  
  11. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  12. HANDLE hInstance;           /* current instance             */
  13. HANDLE hPrevInstance;       /* previous instance            */
  14. LPSTR lpCmdLine;            /* command line                 */
  15. int nCmdShow;               /* show-window type (open/icon) */
  16. {
  17.     HWND hWnd;              /* window handle                */
  18.     MSG msg;                /* message                      */
  19.  
  20.  
  21.     if (!hPrevInstance)     /* Has application been initialized? */
  22.         if (!GetCRectInit(hInstance))
  23.             return (NULL);  /* Exits if unable to initialize     */
  24.  
  25.     hInst = hInstance;      /* Saves the current instance        */
  26.  
  27.     hWnd = CreateWindow("GetCRect",     /* window class            */
  28.         "GetCRect Sample Application",  /* window name             */
  29.         WS_OVERLAPPEDWINDOW,           /* window style            */
  30.         CW_USEDEFAULT,                 /* x position              */
  31.         CW_USEDEFAULT,                 /* y position              */
  32.         CW_USEDEFAULT,                 /* width                   */
  33.         CW_USEDEFAULT,                 /* height                  */
  34.         NULL,                          /* parent handle           */
  35.         NULL,                          /* menu or child ID        */
  36.         hInstance,                     /* instance                */
  37.         NULL);                         /* additional info         */
  38.  
  39.     if (!hWnd)                         /* Was the window created? */
  40.         return (NULL);
  41.  
  42.     ShowWindow(hWnd, nCmdShow);        /* Shows the window        */
  43.     UpdateWindow(hWnd);                /* Sends WM_PAINT message  */
  44.  
  45.     while (GetMessage(&msg,     /* message structure                      */
  46.             NULL,               /* handle of window receiving the message */
  47.             NULL,               /* lowest message to examine              */
  48.             NULL))              /* highest message to examine             */
  49.         {
  50.         TranslateMessage(&msg); /* Translates virtual key codes           */
  51.         DispatchMessage(&msg);  /* Dispatches message to window           */
  52.     }
  53.     return (msg.wParam);        /* Returns the value from PostQuitMessage */
  54. }
  55.  
  56.  
  57. /*************************************************************************/
  58.  
  59. BOOL GetCRectInit(hInstance)
  60. HANDLE hInstance;                 /* current instance           */
  61. {
  62.     HANDLE hMemory;               /* handle to allocated memory */
  63.     PWNDCLASS pWndClass;          /* structure pointer          */
  64.     BOOL bSuccess;                /* RegisterClass() result     */
  65.  
  66.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  67.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  68.  
  69.     pWndClass->style = NULL;
  70.     pWndClass->lpfnWndProc = GetCRectWndProc;
  71.     pWndClass->hInstance = hInstance;
  72.     pWndClass->hIcon = NULL;
  73.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  74.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  75.     pWndClass->lpszMenuName = (LPSTR) "GetCRectMenu";
  76.     pWndClass->lpszClassName = (LPSTR) "GetCRect";
  77.  
  78.     bSuccess = RegisterClass(pWndClass);
  79.  
  80.     LocalUnlock(hMemory);                           /* Unlocks the memory    */
  81.     LocalFree(hMemory);                             /* Returns it to Windows */
  82.  
  83.     return (bSuccess);           /* Returns result of registering the window */
  84. }
  85.  
  86. /**************************************************************************/
  87.  
  88. long FAR PASCAL GetCRectWndProc(hWnd, message, wParam, lParam)
  89. HWND hWnd;                      /* window handle                   */
  90. unsigned message;               /* type of message                 */
  91. WORD wParam;                    /* additional information          */
  92. LONG lParam;                    /* additional information          */
  93. {
  94.     FARPROC lpProcAbout;        /* pointer to the "About" function */
  95.     HMENU hMenu;                /* handle to the System menu       */
  96.     RECT  rRect;                /* Will hold client rectangle  */
  97.     HANDLE hDC;                 /* Handle to the display context  */
  98.     PAINTSTRUCT ps;             /* Paint Structure  */
  99.  
  100.     switch (message) {
  101.         case WM_SIZE:
  102.         case WM_PAINT:
  103.             GetClientRect( hWnd,           /*  Window handle  */
  104.                          (LPRECT)&rRect ); /*  Structure holding  */
  105.             InvalidateRect( hWnd, (LPRECT)&rRect, TRUE );
  106.                                /*  Erase the background  */
  107.             hDC = BeginPaint ( hWnd, &ps );   /*  Get the display context  */
  108. /*  We are going to draw an X through the window  */            
  109.             MoveTo( hDC, rRect.left, rRect.top );
  110.             LineTo( hDC, rRect.right, rRect.bottom );
  111.             MoveTo( hDC, rRect.right, rRect.top );
  112.             LineTo( hDC, rRect.left, rRect.bottom );
  113.             EndPaint( hWnd, &ps );
  114.             break;
  115.  
  116.         case WM_DESTROY:                  /* message: window being destroyed */
  117.             PostQuitMessage(0);
  118.             break;
  119.  
  120.         default:                          /* Passes it on if unproccessed    */
  121.             return (DefWindowProc(hWnd, message, wParam, lParam));
  122.     }
  123.     return (NULL);
  124. }
  125.  
  126.  
  127.