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

  1. /*
  2.  *   UpdateWindow
  3.  *   updtwin.c
  4.  *
  5.  *   This program demonstrates the use of the function UpdateWindow.
  6.  *   UpdateWindow sends a WM_PAINT message directly to the paint procedure
  7.  *   of the given window if the update region for the window is not empty.
  8.  *   The paint procedure invokes the TextOut function to let the operator
  9.  *   know that the WM_PAINT message is being processed.  A message box is
  10.  *   then shown to let the operator know that the call to UpdateWindow is
  11.  *   complete.
  12.  *
  13.  */
  14.  
  15. #include "windows.h"
  16. #include "string.h"
  17.  
  18. BOOL PASCAL SampleInit(HANDLE , HANDLE, int);
  19. BOOL PASCAL SamplePaint(HANDLE);
  20. long    FAR PASCAL SampleWndProc(HWND, unsigned, WORD, LONG);
  21.  
  22. static HANDLE  hInst;  /* Can be used througout the program but not normally
  23.                         * passed to other intances */
  24.  
  25. int    PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )
  26. HANDLE hInstance, hPrevInstance;
  27. LPSTR lpszCmdLine;
  28. int    nCmdShow;
  29. {
  30.   HWND  hWnd;
  31.   MSG   msg;
  32.   HWND hWindow;    /* For use in the UpdateWindow demonstration section */
  33.  
  34.   if (!hPrevInstance)
  35.     SampleInit(hInstance, hPrevInstance, nCmdShow);
  36.  
  37.   hInst = hInstance;                      /* So window procedures can use */
  38.   hWnd = CreateWindow((LPSTR)"UpdateWindow",
  39.       (LPSTR)"UpdateWindow",
  40.       WS_OVERLAPPEDWINDOW,
  41.       CW_USEDEFAULT,
  42.       CW_USEDEFAULT,
  43.       CW_USEDEFAULT,
  44.       CW_USEDEFAULT,
  45.       (HWND)NULL,        /* no parent */
  46.   (HMENU)NULL,       /* use class menu */
  47.   (HANDLE)hInstance, /* handle to window instance */
  48.   (LPSTR)NULL        /* no params to pass on */
  49.   );
  50.  
  51. /* Make window visible according to the way the app is activated */
  52.   ShowWindow(hWnd, nCmdShow);
  53.   hWindow = GetActiveWindow();
  54.   UpdateWindow( hWindow ); /* Send a WM_PAINT message directly to the
  55.                               * window procedure, bypassing the application
  56.                                 * queue.   */
  57.   MessageBox(hWindow,
  58.       (LPSTR)"UpdateWindow and processed a WM_PAINT message.",
  59.       (LPSTR)"I called ...",
  60.       MB_OK | MB_SYSTEMMODAL );
  61.  
  62. /* Polling messages from event queue */
  63.   while (GetMessage((LPMSG) & msg, NULL, 0, 0)) 
  64.   {
  65.     TranslateMessage((LPMSG) & msg);
  66.     DispatchMessage((LPMSG) & msg);
  67.   }
  68.   return (int)msg.wParam;
  69. }
  70.  
  71.  
  72. /* Procedure called when the application is loaded for the first time */
  73. BOOL PASCAL SampleInit( hInstance, hPrevInstance, nCmdShow )
  74. HANDLE hInstance, hPrevInstance;
  75. int    nCmdShow;
  76. {
  77.   PWNDCLASS   pSampleClass;
  78.  
  79.   pSampleClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  80.  
  81.   pSampleClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  82.   pSampleClass->hIcon        = LoadIcon( hInstance, NULL);
  83.   pSampleClass->lpszMenuName   = (LPSTR)NULL;
  84.   pSampleClass->lpszClassName  = (LPSTR)"UpdateWindow";
  85.   pSampleClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  86.   pSampleClass->hInstance      = hInstance;
  87.   pSampleClass->style          = CS_HREDRAW | CS_VREDRAW;
  88.   pSampleClass->lpfnWndProc    = SampleWndProc;
  89.  
  90.   if (!RegisterClass((LPWNDCLASS)pSampleClass))
  91. /* Initialization failed.
  92.        * Windows will automatically deallocate all allocated memory.
  93.        */
  94.     return FALSE;
  95.  
  96.   LocalFree((HANDLE)pSampleClass);
  97.   return TRUE;
  98. }
  99.  
  100.  
  101. /* Every message for this window will be delevered right here.         */
  102. long    FAR PASCAL SampleWndProc(hWnd, message, wParam, lParam)
  103. HWND hWnd;
  104. unsigned    message;
  105. WORD wParam;
  106. LONG lParam;
  107. {
  108.   switch (message)
  109.   {
  110.   case WM_DESTROY:
  111.     PostQuitMessage(0);
  112.     break;
  113.  
  114.   case WM_PAINT:
  115.     SamplePaint(hWnd);
  116.     break;
  117.  
  118.   default:
  119.     return DefWindowProc(hWnd, message, wParam, lParam);
  120.     break;
  121.   }
  122.   return(0L);
  123. }
  124.  
  125.  
  126. BOOL PASCAL SamplePaint(hWnd)
  127. HWND hWnd;
  128. {
  129.   PAINTSTRUCT ps;
  130.   HDC         hDC;
  131.  
  132.   BeginPaint( hWnd, (LPPAINTSTRUCT) & ps );
  133.   hDC = ps.hdc;
  134.  
  135.   TextOut( hDC, 10, 10,
  136.       (LPSTR)"The TextOut function is called in the paint procedure",
  137.       strlen("The TextOut function is called in the paint procedure"));
  138.  
  139.   EndPaint( hWnd, (LPPAINTSTRUCT) & ps );
  140.  
  141.   return TRUE;
  142. }
  143.  
  144.  
  145.