home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 7.ddi / CURSOR.ZIP / CURSOR.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  6.2 KB  |  238 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <owl.h>
  4. #include <dialog.h>
  5.  
  6. #include <stdlib.h>
  7.  
  8. #include "cursor.h"
  9.  
  10. class TCursorDlg : public TDialog {
  11.     BOOL bUpdateDialog;
  12. public:
  13.     TCursorDlg( PTWindowsObject AParent, LPSTR AName )
  14.         : TDialog( AParent, AName ), bUpdateDialog( FALSE ) {}
  15.     void SetDlgItemText( int nIDDlgItem, LPSTR lpString );
  16.     virtual LPSTR GetClassName( void );
  17.     virtual void GetWindowClass( WNDCLASS& );
  18.     virtual void Ok( TMessage& Msg ) = [ ID_FIRST + IDOK ];
  19.     virtual void Cancel( TMessage& Msg ) = [ ID_FIRST + IDCANCEL ];
  20.     virtual void WMInitDialog( TMessage& Msg )
  21.         = [ WM_FIRST + WM_INITDIALOG ];
  22.    virtual void CloseWindow();
  23.     void UpdateDialog( void );
  24.    BOOL ShouldUpdate() { return bUpdateDialog; }
  25. };
  26.  
  27. /*
  28.     This function sets the caption or text of a control in the dialog
  29.     box. The control whose text is to be set is specified by
  30.     nIDDlgItem.
  31. */
  32. void TCursorDlg::SetDlgItemText( int nDlgItemID, LPSTR lpString )
  33. {
  34.     ::SetDlgItemText( HWindow, nDlgItemID, lpString );
  35. }
  36.  
  37. void TCursorDlg::WMInitDialog( TMessage& )
  38. {
  39.     SetupWindow();
  40.     bUpdateDialog = TRUE;
  41. }
  42.  
  43. // Override the meaning of 'OK' to do nothing.
  44. void TCursorDlg::Ok( TMessage& ) {}
  45. void TCursorDlg::Cancel( TMessage& ) {}
  46.  
  47. void TCursorDlg::CloseWindow()
  48. {
  49.     bUpdateDialog = FALSE;       // disable updating
  50.     TWindowsObject::CloseWindow();
  51. }
  52.  
  53. LPSTR TCursorDlg::GetClassName( void )
  54. {
  55.     return( "CursorDlg" );
  56. }
  57.  
  58. void TCursorDlg::GetWindowClass( WNDCLASS& AWndClass )
  59. {
  60.     TDialog::GetWindowClass( AWndClass );
  61.     AWndClass.hIcon = LoadIcon( GetApplication()->hInstance, "CursorDlg" );
  62. }
  63.  
  64. inline BOOL operator != ( POINT& p1, POINT& p2 )
  65. {
  66.     if ( ( p1.x != p2.x ) || ( p1.y != p2.y ) )
  67.         return TRUE;
  68.     else
  69.         return FALSE;
  70. }
  71.  
  72. inline BOOL operator != ( RECT& r1, RECT& r2 )
  73. {
  74.     if ( ( r1.left != r2.left ) || ( r1.top != r2.top ) ||
  75.          ( r1.right != r2.right ) || ( r1.bottom != r2.bottom ) )
  76.         return TRUE;
  77.     else
  78.         return FALSE;
  79. }
  80.  
  81.  
  82. void TCursorDlg::UpdateDialog( void )
  83. {
  84.     POINT Point;
  85.     static POINT prevPoint = { -1, -1 };
  86.     static HWND hPrevWnd = (HWND)-1, hPrevParent = (HWND)-1;
  87.     HWND hWndFromPt, hParent = hPrevParent;
  88.     HWND hFocus;
  89.     static HWND hPrevFocus = (HWND)-1;
  90.     static RECT Rect, prevRect = { -1, -1, -1, -1 };
  91.     char szBuffer[26];
  92.  
  93.     if ( bUpdateDialog )
  94.     {
  95.         GetCursorPos( &Point );
  96.  
  97.         // If the cursor position has changed...
  98.         if ( Point != prevPoint )
  99.         {
  100.             prevPoint = Point;
  101.             SetDlgItemText( IDD_SX,    ltoa( Point.x, szBuffer, 10 ) );
  102.             SetDlgItemText( IDD_SY,    ltoa( Point.y, szBuffer, 10 ) );
  103. // Turn off warning that struct is passed by value in call to WindowFromPoint.
  104. #pragma warn -stv
  105.             if ( ( hWndFromPt = WindowFromPoint( Point ) ) != 0 )
  106. #pragma warn +stv
  107.             {
  108.                 // Set the x and y coordinates in terms
  109.                 // of the client area of the underlying window.
  110.                 ScreenToClient( hWndFromPt, &Point );
  111.                 SetDlgItemText( IDD_WX,
  112.                     ltoa( Point.x, szBuffer, 10 ) );
  113.                 SetDlgItemText( IDD_WY,
  114.                     ltoa( Point.y, szBuffer, 10 ) );
  115.  
  116.                 if ( ( hParent = GetParent( hWndFromPt ) ) != 0 )
  117.                 {
  118.                     // Set the x and y coordinates in
  119.                     // terms of the client area of
  120.                     // the parent of the window.
  121.                     Point = prevPoint;
  122.                     ScreenToClient( hParent, &Point );
  123.                     SetDlgItemText( IDD_PX,
  124.                         ltoa( Point.x, szBuffer, 10 ) );
  125.                     SetDlgItemText( IDD_PY,
  126.                         ltoa( Point.y, szBuffer, 10 ) );
  127.                 }
  128.                 else
  129.                 {
  130.                     // If the window has no parent,
  131.                     // leave the x and y fields blank.
  132.                     SetDlgItemText( IDD_PX, "" );
  133.                     SetDlgItemText( IDD_PY, "" );
  134.                 }
  135.             }
  136.             // If there is no window at the current point,
  137.             // the x and y coordinates should be blank
  138.             // in the dialog box.
  139.             else
  140.             {
  141.                 SetDlgItemText( IDD_WX, "" );
  142.                 SetDlgItemText( IDD_WY, "" );
  143.             }
  144.             // Update the display of the handle of the
  145.             // underlying window if necessary.
  146.             if ( hWndFromPt != hPrevWnd )
  147.                 SetDlgItemText( IDD_HW,
  148.                     ltoa( (LONG)hWndFromPt, szBuffer, 10 ) );
  149.             // If the parent window has changed,
  150.             // update the display of its handle.
  151.             if ( hParent != hPrevParent )
  152.             {
  153.                 if( ( hWndFromPt == 0 ) || ( hParent == 0 ) )
  154.                     SetDlgItemText( IDD_HP, "" );
  155.                 else
  156.                     SetDlgItemText( IDD_HP,
  157.                         ltoa( (LONG)hParent, szBuffer, 10 ) );
  158.             }
  159.             else
  160.             {
  161.                 // If there is no underlying window,
  162.                 // do not display a handle for a parent.
  163.                 if ( hWndFromPt == 0 )
  164.                     SetDlgItemText( IDD_HP, "" );
  165.             }
  166.             hPrevWnd = hWndFromPt;
  167.             hPrevParent = hParent;
  168.         }
  169.  
  170.         // Update the focus display fields if necessary.
  171.         hFocus = GetFocus();
  172.         if ( hFocus == 0 )
  173.         {
  174.             if( hFocus != hPrevFocus )
  175.             {
  176.                 SetDlgItemText( IDD_HF, "" );
  177.                 SetDlgItemText( IDD_LEFT, "" );
  178.                 SetDlgItemText( IDD_TOP, "" );
  179.                 SetDlgItemText( IDD_RIGHT, "" );
  180.                 SetDlgItemText( IDD_BOTTOM, "" );
  181.             }
  182.         }
  183.         else
  184.         {
  185.             if( hFocus != hPrevFocus )
  186.             {
  187.                 SetDlgItemText( IDD_HF,
  188.                     ltoa( (LONG)hFocus, szBuffer, 10 ) );
  189.             }
  190.             GetWindowRect( hFocus, &Rect );
  191.             if ( Rect != prevRect )
  192.             {
  193.                 prevRect = Rect;
  194.                 SetDlgItemText( IDD_LEFT,
  195.                     ltoa( Rect.left, szBuffer, 10 ) );
  196.                 SetDlgItemText( IDD_TOP,
  197.                     ltoa( Rect.top, szBuffer, 10 ) );
  198.                 SetDlgItemText( IDD_RIGHT,
  199.                     ltoa( Rect.right, szBuffer, 10 ) );
  200.                 SetDlgItemText( IDD_BOTTOM,
  201.                     ltoa( Rect.bottom, szBuffer, 10 ) );
  202.             }
  203.         }
  204.         hPrevFocus = hFocus;
  205.     }
  206. }
  207.  
  208. class CursorApplication : public TApplication {
  209. public:
  210.     CursorApplication(LPSTR name, HINSTANCE hInstance,
  211.               HINSTANCE hPrevInstance, LPSTR lpCmd,
  212.                   int nCmdShow)
  213.             : TApplication(name, hInstance,
  214.                    hPrevInstance, lpCmd, nCmdShow) {};
  215.     virtual void InitMainWindow( void );
  216.         virtual void IdleAction(void);
  217. };
  218.  
  219. void CursorApplication::InitMainWindow()
  220. {
  221.     MainWindow = new TCursorDlg( NULL, "CursorDlg" );
  222. }
  223.  
  224. void CursorApplication::IdleAction( void )
  225. {
  226.   if ( MainWindow && ((TCursorDlg *)MainWindow)->ShouldUpdate() )
  227.           ((TCursorDlg*)MainWindow)->UpdateDialog();
  228. }
  229.  
  230. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  231.            LPSTR lpCmd, int nCmdShow)
  232. {
  233.     CursorApplication CursorApp ("Cursor Location", hInstance,
  234.                      hPrevInstance, lpCmd, nCmdShow);
  235.     CursorApp.Run();
  236.     return ( CursorApp.Status );
  237. }
  238.