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

  1. /*
  2.  *   SwapMouseButton
  3.  *   swapbtn
  4.  *
  5.  *   This program demonstrates the use of the function SwapMouseButton.
  6.  *   First, a message box opens and only the left mouse button will close
  7.  *   it.  The functionality of the mouse buttons are switched using
  8.  *   SetMouseButton and another message box is displayed.  This time only
  9.  *   the right mouse button will close the message box.  The mouse buttons
  10.  *   are finally reset before closing.
  11.  *
  12.  */
  13.  
  14. #include "windows.h"
  15.  
  16. /* Forward References */
  17.  
  18. BOOL FAR PASCAL SampleInit(HANDLE);
  19.  
  20. long FAR PASCAL SampleWndProc(HWND, unsigned, WORD, LONG);
  21.  
  22. /***************************************************************************/
  23.  
  24. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  25. HANDLE hInstance, hPrevInstance;
  26. LPSTR lpszCmdLine;
  27. int cmdShow;
  28. {
  29.     MSG   msg;
  30.     HWND  hWnd;
  31.     HMENU hMenu;
  32.  
  33.     SampleInit( hInstance );
  34.     hWnd = CreateWindow((LPSTR)"SwapMouseButton",
  35.             (LPSTR)"SwapMouseButton",
  36.             WS_OVERLAPPEDWINDOW,
  37.             CW_USEDEFAULT,
  38.             CW_USEDEFAULT,
  39.             CW_USEDEFAULT,
  40.             CW_USEDEFAULT,
  41.                         (HWND)NULL,        /* no parent */
  42.                         (HMENU)NULL,       /* use class menu */
  43.                         (HANDLE)hInstance, /* handle to window instance */
  44.                         (LPSTR)NULL        /* no params to pass on */
  45.                         );
  46.  
  47.     /* Make window visible according to the way the app is activated */
  48.     ShowWindow( hWnd, cmdShow );
  49.     UpdateWindow( hWnd );
  50. /************************************************************************/
  51. /* Beginning of SwapMouseButton section */
  52.  
  53. MessageBox (hWnd, (LPSTR)"The functionality of the mouse buttons will be switched using the fuction SetMouseButton.",
  54.             (LPSTR)"Done", MB_OK);
  55.  
  56. SwapMouseButton(FALSE);                  /* Initialize the mouse buttons */
  57.  
  58. MessageBox (hWnd, (LPSTR)"Only the left mouse button will close this message box.",
  59.             (LPSTR)"Done", MB_OK);
  60.  
  61. SwapMouseButton(TRUE);                   /* Switch the mouse buttons */
  62.  
  63. MessageBox (hWnd, (LPSTR)"Only the right mouse button will close this message box.",
  64.             (LPSTR)"Done", MB_OK);
  65.  
  66. SwapMouseButton(FALSE);                  /* Reset the mouse buttons */
  67.  
  68. MessageBox (hWnd, (LPSTR)"The mouse buttons are now reset.",
  69.             (LPSTR)"Done", MB_OK);
  70.  
  71. /************************************************************************/
  72.  
  73.     /* Polling messages from event queue */
  74.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  75.         TranslateMessage((LPMSG)&msg);
  76.         DispatchMessage((LPMSG)&msg);
  77.         }
  78.  
  79.     return (int)msg.wParam;
  80. }
  81.  
  82. /***************************************************************************/
  83.  
  84. /* Procedure called when the application is loaded for the first time */
  85. BOOL FAR PASCAL SampleInit( hInstance )
  86. HANDLE hInstance;
  87. {
  88.     PWNDCLASS   pSampleClass;
  89.  
  90.     pSampleClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  91.  
  92.     pSampleClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  93.     pSampleClass->hIcon              = LoadIcon( hInstance,NULL);
  94.     pSampleClass->lpszMenuName   = (LPSTR)NULL;
  95.     pSampleClass->lpszClassName  = (LPSTR)"SwapMouseButton";
  96.     pSampleClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  97.     pSampleClass->hInstance      = hInstance;
  98.     pSampleClass->style          = CS_HREDRAW | CS_VREDRAW;
  99.     pSampleClass->lpfnWndProc    = SampleWndProc;
  100.  
  101.     if (!RegisterClass( (LPWNDCLASS)pSampleClass ) )
  102.         /* Initialization failed.
  103.          * Windows will automatically deallocate all allocated memory.
  104.          */
  105.         return FALSE;
  106.  
  107.     LocalFree( (HANDLE)pSampleClass );
  108.     return TRUE;        /* Initialization succeeded */
  109. }
  110.  
  111.  
  112. /**************************************************************************/
  113. /* Every message for this window will be delevered right here.         */
  114.  
  115. long FAR PASCAL SampleWndProc( hWnd, message, wParam, lParam )
  116. HWND hWnd;
  117. unsigned message;
  118. WORD wParam;
  119. LONG lParam;
  120. {
  121.     switch (message)
  122.     {
  123.     case WM_DESTROY:
  124.         PostQuitMessage( 0 );
  125.         break;
  126.  
  127.     default:
  128.         return DefWindowProc( hWnd, message, wParam, lParam );
  129.         break;
  130.     }
  131.     return(0L);
  132. }
  133.