home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / input / mouseinfo / mousinfo.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  11KB  |  336 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. // 
  3. // MODULE:      MOUSINFO.C
  4. //
  5. // DESCRIPTION: SDK sample for handling the new WM_MOUSEWHEEL message and 
  6. //              the new TrackMouseEvent() API.
  7. //
  8. //              Applet displays MouseButton, MouseWheel, MouseMovement, and
  9. //              any mouse messages in the title bar.
  10. //
  11. // PLATFORMS:   WinNT 4.0
  12. //
  13. //              Copyright (c) 1995 - 1997, Microsoft Corporation. 
  14. //                          All rights reserved
  15. // 
  16. //
  17. ///////////////////////////////////////////////////////////////////////////
  18. #include <windows.h>
  19. #include "mousinfo.h"
  20.  
  21. // Globals
  22. HINSTANCE ghInst;
  23.  
  24. //
  25. // Array of Mouse Message Strings
  26. //
  27. char garMsgStrings[][20] = {
  28.     "                   ",
  29.     "WM_LBUTTONUP       ",
  30.     "WM_RBUTTONUP       ",
  31.     "WM_MBUTTONUP       ",
  32.     "WM_LBUTTONDOWN     ",
  33.     "WM_RBUTTONDOWN     ",
  34.     "WM_MBUTTONDOWN     ",
  35.     "WM_LBUTTONDBLCLK   ",
  36.     "WM_MBUTTONDBLCLK   ",
  37.     "WM_RBUTTONDBLCLK   ",
  38.     "WM_MOUSEWHEEL      ",
  39.     "WM_MOUSEHOVER      ",
  40.     "WM_MOUSELEAVE      "
  41.     };
  42.  
  43.  
  44. //////////////////////////////////////////////////////////////////////////
  45. //
  46. // FUNCTION:    int WINAPI WinMain(HANDLE,HANDLE,LPSTR,int)
  47. //
  48. // DESCRIPTION:    Your basic WinMain()
  49. //
  50. /////////////////////////////////////////////////////////////////////////
  51. int WINAPI WinMain( HINSTANCE hInstance, 
  52.                     HINSTANCE hPrevInstance, 
  53.                     LPSTR  lpCmdLine, 
  54.                     int    nCmdShow)
  55. {
  56.     MSG msg;
  57.     WNDCLASS  wc;
  58.     HWND hwndMain;
  59.  
  60.     ghInst = hInstance;
  61.  
  62.     //
  63.     // Register our main window, only if first instance
  64.     //
  65.  
  66.     if( !hPrevInstance )
  67.     {
  68.         wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
  69.         wc.lpfnWndProc = (WNDPROC)MainWndProc;
  70.         wc.cbClsExtra = 0;
  71.         wc.cbWndExtra = 0;
  72.         wc.hInstance = ghInst;       
  73.         wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(MOUSINFO_ICON));
  74.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  75.         wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  76.         wc.lpszMenuName =  MAKEINTRESOURCE(IDM_MENU);
  77.         wc.lpszClassName = szClassName;
  78.  
  79.         if (!RegisterClass(&wc)) 
  80.             return FALSE;
  81.     }
  82.  
  83.   //
  84.   // Create the main window...
  85.   //  with an initial size of 0
  86.   //
  87.  
  88.   hwndMain = CreateWindow(
  89.       szClassName,
  90.       szAppName,
  91.       WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_THICKFRAME,
  92.       0,0,0,0,
  93.       NULL, NULL, ghInst, NULL );
  94.  
  95.  
  96.   //
  97.   // Show the window
  98.   //
  99.   ShowWindow(hwndMain,SW_SHOW);
  100.   UpdateWindow(hwndMain);
  101.   
  102.   //
  103.   // Message pump
  104.   //
  105.   while (GetMessage(&msg, NULL,0,0)) 
  106.   {
  107.         TranslateMessage(&msg);
  108.         DispatchMessage(&msg);
  109.   }
  110.  
  111.   return (msg.wParam);
  112. }
  113.  
  114.  
  115. //////////////////////////////////////////////////////////////////////////////////
  116. //
  117. // FUNCTION:    LRESULT WINAPI MainWndProc (HWND,UINT,WPARAM,LPARAM)
  118. //
  119. // DESCRIPTION:    Your standard Window Procedure.
  120. //
  121. // MESSAGE:        Handles the following Windows messages:
  122. //
  123. //                WM_CREATE
  124. //                WM_DESTROY
  125. //                WM_SYSCOMMAND
  126. //                WM_LBUTTONUP       
  127. //                WM_RBUTTONUP     
  128. //                WM_MBUTTONUP     
  129. //                WM_LBUTTONDOWN
  130. //                WM_RBUTTONDOWN   
  131. //                WM_MBUTTONDOWN
  132. //                WM_LBUTTONDBLCLK 
  133. //                WM_MBUTTONDBLCLK 
  134. //                WM_RBUTTONDBLCLK 
  135. //                WM_MOUSEMOVE    
  136. //
  137. //                And the new mouse message
  138. //                
  139. //                WM_MOUSEWHEEL
  140. //                                  
  141. //              And the new TrackMouseEvent() Messages
  142. //
  143. //              WM_MOUSEHOVER       // mouse hovered specified delay over client area
  144. //              WM_MOUSELEAVE       // mouse has left the client area
  145. //
  146. //
  147. //////////////////////////////////////////////////////////////////////////////////
  148. LRESULT WINAPI MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  149. {
  150.  
  151.     TEXTMETRIC tm;
  152.     HDC hDC;
  153.  
  154.     int MsgIndex = NO_MESSAGE;          // Index into garMsgStrings[] array
  155.       char szBuff[128];                   // String buffer
  156.     static int LB=0,RB=0,MB=0;          // State of the mouse buttons
  157.     static int X=0,Y=0;                 // Mouse position  
  158.     static char Roller=' ';             // State of the mouse wheel. '+' or '-'
  159.     static int fHasWheel=FALSE;         // Mouse has a wheel?? TRUE if yes, FALSE if no
  160.     
  161.     switch (message)
  162.     {
  163.             
  164.         case WM_CREATE:
  165.             //
  166.             // See if we have a mouse with a wheel attached to the system.
  167.             // Note that SM_MOUSEWHEELPRESENT is a new flag for GetSystemMetrics
  168.             //  and returns TRUE if a mouse with a wheel is present, FALSE if not.
  169.             //
  170.             fHasWheel = GetSystemMetrics( SM_MOUSEWHEELPRESENT );
  171.  
  172.             //
  173.             // Size the window so that it will display all the text
  174.             //
  175.             hDC = GetDC(hwnd);
  176.             GetTextMetrics(hDC,&tm);
  177.             SetWindowPos(hwnd,HWND_BOTTOM,0,0,tm.tmMaxCharWidth * MAX_TITLE_LEN,100,SWP_SHOWWINDOW);
  178.             ReleaseDC(hwnd, hDC);
  179.  
  180.             return 0;
  181.  
  182.  
  183.         case WM_DESTROY:
  184.             PostQuitMessage(0);
  185.             return 0;
  186.  
  187.           //
  188.         // Handle WM_MOUSE messages.
  189.         // First we setup up some variables and then let
  190.         //  control fall through to the end of the switch
  191.         //  construct, where we then update the title bar.
  192.         // 
  193.         case WM_LBUTTONUP:        LB=0; MsgIndex = LBU; break;
  194.         case WM_RBUTTONUP:      RB=0; MsgIndex = RBU; break;
  195.         case WM_MBUTTONUP:      MB=0; MsgIndex = MBU; break;
  196.         case WM_LBUTTONDOWN:    LB=1; MsgIndex = LBD; break;
  197.         case WM_RBUTTONDOWN:    RB=1; MsgIndex = RBD; break;
  198.         case WM_MBUTTONDOWN:    MB=1; MsgIndex = MBD; break; 
  199.         case WM_LBUTTONDBLCLK:  LB=0; MsgIndex = LB2; break; 
  200.         case WM_MBUTTONDBLCLK:  MB=0; MsgIndex = MB2; break; 
  201.         case WM_RBUTTONDBLCLK:  RB=0; MsgIndex = RB2; break; 
  202.         case WM_MOUSEHOVER:           MsgIndex = MH;  break;
  203.         case WM_MOUSELEAVE:           MsgIndex = ML;  break;
  204.         
  205.         case WM_MOUSEMOVE:        MsgIndex=NO_MESSAGE; Roller = ' ';  break; 
  206.         //
  207.         // Handle WM_MOUSEWHEEL message.
  208.         // We handle this message a little differently since the
  209.         //  roller movement info is in the HIWORD of wParam.
  210.         //
  211.         // The MouseWheel has 18 'detents'. As the wheel is rolled
  212.         //  and a detent is encountered, the OS will send a WM_MOUSEWHEEL
  213.         //  message with the HIWORD of wParam set to a value of +/- 120.
  214.         //  '+' if the wheel is being rolled forward (away from the user),
  215.         //  '-' if the wheel is being rolled backwards (towards the user). 
  216.         case WM_MOUSEWHEEL:     
  217.             //
  218.             // Mouse Wheel is being rolled forward
  219.             //
  220.             if( (short)HIWORD(wParam) > 0 )                    
  221.                 Roller = '+';
  222.  
  223.             //
  224.             // Mouse Wheel is being rolled backward
  225.             //
  226.             if( (short)HIWORD(wParam) < 0 )
  227.                 Roller = '-';
  228.  
  229.             MsgIndex = MW;      
  230.             break;
  231.  
  232.         case WM_COMMAND:
  233.             switch(wParam)
  234.             {
  235.                 //
  236.                 // Call the TrackMouseEvent() API, and set up a WM_MOUSEHOVER
  237.                 //  "one-shot" event. Exactly one and only one  WM_MOUSEHOVER
  238.                 //  message will be sent to the window specified in the hwndTrack
  239.                 //  member of the TRACKMOUSEEVENT structure, when the mouse has
  240.                 //  'hovered' over the client area an amount of time equal to that
  241.                 //  specified in the dwHoverTime member of TRACKMOUSEEVENT.
  242.                 //
  243.                 // NOTE that this message will be generated only once. The application
  244.                 //  must call the TrackMouseEvent() API again in order for the system
  245.                 //  to generate another WM_MOUSEHOVER message.
  246.                 //
  247.                 case IDM_HOVER:
  248.                 {
  249.                     TRACKMOUSEEVENT tme;
  250.  
  251.                     tme.cbSize      = sizeof(TRACKMOUSEEVENT);
  252.                     tme.dwFlags     = TME_HOVER;
  253.                     tme.hwndTrack   = hwnd;
  254.                     tme.dwHoverTime = HOVER_DEFAULT;
  255.  
  256.                     TrackMouseEvent(&tme);
  257.  
  258.                     return 0;
  259.                 }
  260.  
  261.                 //
  262.                 // Call the TrackMouseEvent() API, and set up a WM_MOUSELEAVE
  263.                 //  "one-shot" event. Exactly one and only one  WM_MOUSELEAVE
  264.                 //  message will be sent to the window specified in the hwndTrack
  265.                 //  member of the TRACKMOUSEEVENT structure, when the mouse has
  266.                 //  left the client area. 
  267.                 //
  268.                 // NOTE that this message will be generated only once. The application
  269.                 //  must call the TrackMouseEvent() API again in order for the system
  270.                 //  to generate another WM_MOUSELEAVE message. Also note that if the
  271.                 //  mouse pointer is not over the application, a call to TrackMouseEvent() 
  272.                 //  will result in the immediate posting of a WM_MOUSELEAVE
  273.                 //  message.
  274.                 //
  275.                 case IDM_LEAVE:
  276.                 {
  277.                     TRACKMOUSEEVENT tme;
  278.  
  279.                     tme.cbSize      = sizeof(TRACKMOUSEEVENT);
  280.                     tme.dwFlags     = TME_LEAVE;
  281.                     tme.hwndTrack   = hwnd;
  282.  
  283.                     TrackMouseEvent(&tme);
  284.  
  285.                     return 0;
  286.                 }
  287.  
  288.                 case IDM_ABOUT:
  289.                 {
  290.                     char buff[128];
  291.                     
  292.                     wsprintf(buff,"MousInfo.Exe\r\n"
  293.                                   "Copyright 1995-96, Microsoft Corp.\r\n"
  294.                                   "All rights reserved\r\n\r\n" );
  295.  
  296.                     MessageBox(hwnd,buff,szAppName,MB_OK);
  297.                     return 0;
  298.                 }
  299.             
  300.             }
  301.             break;
  302.             
  303.         default:
  304.             return (DefWindowProc(hwnd, message, wParam, lParam));
  305.  
  306.     }  // end switch (message)
  307.   
  308.  
  309.     //
  310.     // If the mouse has a wheel, display the state of the mouse buttons, 
  311.     //   wheel and position in the title bar.
  312.     //
  313.     if( fHasWheel )
  314.         wsprintf(szBuff,"L:%d M:%d R:%d W:%c x:%04d y:%04d %s",
  315.                   LB,MB,RB,Roller,
  316.                   LOWORD(lParam),HIWORD(lParam), 
  317.                   garMsgStrings[MsgIndex] );
  318.     //
  319.     // Else if mouse does not have a wheel, do not display the state
  320.     //  of the wheel (but do display everything else!).
  321.     //
  322.     else
  323.         wsprintf(szBuff,"L:%d M:%d R:%d x:%04d y:%04d %s",
  324.                   LB,MB,RB,LOWORD(lParam),HIWORD(lParam), 
  325.                   garMsgStrings[MsgIndex] );
  326.  
  327.     SetWindowText(hwnd,szBuff);
  328.  
  329.     return (DefWindowProc(hwnd, message, wParam, lParam));
  330.  
  331. }
  332.  
  333.  
  334.  
  335.  
  336.