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

  1. /*
  2.  *  Function Name:   EnableWindow
  3.  *  Program Name:    enawin.c
  4.  *
  5.  *  Description:
  6.  *   This function can enable or disable keyboard and mouse input to a
  7.  *   particular window.  The program below will disable input to the
  8.  *   window when it loses the focus. The problem is that then you
  9.  *   can't get the focus back. Oh Well....
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  16.  
  17.  
  18. /* Procedure called when the application is loaded for the first time */
  19. BOOL WinInit( hInstance )
  20.     HANDLE hInstance;
  21.     {
  22.     WNDCLASS   wcClass;
  23.  
  24.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  25.     wcClass.lpfnWndProc    = WndProc;
  26.     wcClass.cbClsExtra     =0;
  27.     wcClass.cbWndExtra     =0;
  28.     wcClass.hInstance      = hInstance;
  29.     wcClass.hIcon          = LoadIcon( hInstance,NULL );
  30.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  31.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  32.     wcClass.lpszMenuName   = (LPSTR)NULL;
  33.     wcClass.lpszClassName  = (LPSTR)"EnableWindow";
  34.  
  35.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  36.         return FALSE;
  37.  
  38.     return TRUE;        /* Initialization succeeded */
  39.     }
  40.  
  41.  
  42. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  43.     HANDLE hInstance, hPrevInstance;
  44.     LPSTR lpszCmdLine;
  45.     int cmdShow;
  46.     {
  47.     MSG   msg;
  48.     HWND  hWnd;
  49.  
  50.     if (!hPrevInstance)
  51.         {
  52.         /* Call initialization procedure if this is the first instance */
  53.         if (!WinInit( hInstance ))
  54.             return FALSE;
  55.         }
  56.  
  57.     hWnd = CreateWindow((LPSTR)"EnableWindow",
  58.                         (LPSTR)"EnableWindow()",
  59.                         WS_OVERLAPPEDWINDOW,
  60.                         CW_USEDEFAULT,
  61.                         CW_USEDEFAULT,
  62.                         CW_USEDEFAULT,
  63.                         CW_USEDEFAULT,
  64.                         (HWND)NULL,        /* no parent */
  65.                         (HMENU)NULL,       /* use class menu */
  66.                         (HANDLE)hInstance, /* handle to window instance */
  67.                         (LPSTR)NULL        /* no params to pass on */
  68.                         );
  69.  
  70.     /* Make window visible according to the way the app is activated */
  71.     ShowWindow( hWnd, cmdShow );
  72.     UpdateWindow( hWnd );
  73.  
  74.     MessageBox(hWnd, "This window is disabled when it loses \
  75. the input focus.", "EnableWindow", MB_OK);
  76.  
  77.     /* Polling messages from event queue */
  78.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  79.         {
  80.         TranslateMessage((LPMSG)&msg);
  81.         DispatchMessage((LPMSG)&msg);
  82.         }
  83.  
  84.     return (int)msg.wParam;
  85.     }
  86.  
  87. /* Procedures which make up the window class. */
  88. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  89.     HWND hWnd;
  90.     unsigned message;
  91.     WORD wParam;
  92.     LONG lParam;
  93.     {
  94.     switch (message)
  95.     {
  96.     /* Disable window if focus is moved to another window */
  97.     case WM_KILLFOCUS:
  98.         EnableWindow(hWnd, FALSE);
  99.         break;
  100.  
  101.     case WM_DESTROY:
  102.         PostQuitMessage(0);
  103.         break;
  104.  
  105.     default:
  106.         return DefWindowProc( hWnd, message, wParam, lParam );
  107.         break;
  108.     }
  109.     return(0L);
  110.     }
  111.