home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / CURSOR.PAK / CURSOR.CPP next >
C/C++ Source or Header  |  1995-08-29  |  6KB  |  205 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //----------------------------------------------------------------------------
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <owl\dialog.h>
  7. #include <owl\framewin.h>
  8. #include <stdlib.h>
  9. #include "cursor.h"
  10.  
  11. class TCursorDlg : public TDialog {
  12.   public:
  13.     TCursorDlg(TWindow* parent, const char far* name);
  14.  
  15.     void   UpdateDialog();
  16.     BOOL   ShouldUpdate() {return Update;}
  17.  
  18.   protected:
  19.     void   SetupWindow();
  20.     void   CloseWindow(int);
  21.  
  22.     void   CmOk() {}     // Override the meaning of OK & Cancel to do nothing.
  23.     void   CmCancel() {}
  24.  
  25.   private:
  26.     BOOL    Update;
  27.  
  28.   DECLARE_RESPONSE_TABLE(TCursorDlg);
  29. };
  30.  
  31. DEFINE_RESPONSE_TABLE1(TCursorDlg, TDialog)
  32.   EV_COMMAND(IDOK, CmOk),
  33.   EV_COMMAND(IDCANCEL, CmCancel),
  34. END_RESPONSE_TABLE;
  35.  
  36.  
  37. TCursorDlg::TCursorDlg(TWindow* parent, const char far* name)
  38.   : TWindow(parent),
  39.     TDialog(parent, name),
  40.     Update(FALSE)
  41. {
  42.   EnableAutoCreate();
  43. }
  44.  
  45. void
  46. TCursorDlg::SetupWindow()
  47. {
  48.   TDialog::SetupWindow();
  49.   Update = TRUE;
  50. }
  51.  
  52. void
  53. TCursorDlg::CloseWindow(int retVal)
  54. {
  55.   Update = FALSE;       // disable updating
  56.   TDialog::CloseWindow(retVal);
  57. }
  58.  
  59. void
  60. TCursorDlg::UpdateDialog()
  61. {
  62.   TPoint Point;
  63.   static TPoint prevPoint(-1,-1);
  64.   static HWND hPrevWnd = (HWND)-1;
  65.   static HWND hPrevParent = (HWND)-1;
  66.   HWND hWndFromPt;
  67.   HWND hParent = hPrevParent;
  68.   HWND hFocus;
  69.   static HWND hPrevFocus = (HWND)-1;
  70.   static TRect Rect;
  71.   static TRect prevRect(-1, -1, -1, -1);
  72.   char buffer[26];
  73.  
  74.   if (Update) {
  75.     GetCursorPos(Point);
  76.  
  77.     // If the cursor position has changed...
  78.     //
  79.     if (Point != prevPoint) {
  80.       prevPoint = Point;
  81.       SetDlgItemText(IDD_SX, ltoa(Point.x, buffer, 10));
  82.       SetDlgItemText(IDD_SY, ltoa(Point.y, buffer, 10));
  83.       if ((hWndFromPt = ::WindowFromPoint(Point)) != 0) {
  84.         // Set the x and y coordinates in terms
  85.         // of the client area of the underlying window.
  86.         //
  87.         ::ScreenToClient(hWndFromPt, &Point);
  88.         SetDlgItemText(IDD_WX, ltoa(Point.x, buffer, 10));
  89.         SetDlgItemText(IDD_WY, ltoa(Point.y, buffer, 10));
  90.  
  91.         if ((hParent = ::GetParent(hWndFromPt)) != 0) {
  92.           // Set the x and y coordinates in terms of the client area of
  93.           // the parent of the window.
  94.           //
  95.           Point = prevPoint;
  96.           ::ScreenToClient(hParent, &Point);
  97.           SetDlgItemText(IDD_PX, ltoa(Point.x, buffer, 10));
  98.           SetDlgItemText(IDD_PY, ltoa(Point.y, buffer, 10));
  99.  
  100.         } else {
  101.           // If the window has no parent,
  102.           // leave the x and y fields blank.
  103.           //
  104.           SetDlgItemText(IDD_PX, "");
  105.           SetDlgItemText(IDD_PY, "");
  106.         }
  107.  
  108.       // If there is no window at the current point,
  109.       // the x and y coordinates should be blank
  110.       // in the dialog box.
  111.       //
  112.       } else {
  113.         SetDlgItemText(IDD_WX, "");
  114.         SetDlgItemText(IDD_WY, "");
  115.       }
  116.  
  117.       // Update the display of the handle of the
  118.       // underlying window if necessary.
  119.       //
  120.       if (hWndFromPt != hPrevWnd)
  121.         SetDlgItemText(IDD_HW, itoa((UINT)hWndFromPt, buffer, 16));
  122.  
  123.       // If the parent window has changed,
  124.       // update the display of its handle.
  125.       //
  126.       if (hParent != hPrevParent) {
  127.         if (!hWndFromPt || !hParent)
  128.           SetDlgItemText(IDD_HP, "");
  129.         else
  130.           SetDlgItemText(IDD_HP, itoa((UINT)hParent, buffer, 16));
  131.  
  132.       } else {
  133.         // If there is no underlying window,
  134.         // do not display a handle for a parent.
  135.         //
  136.         if (!hWndFromPt)
  137.           SetDlgItemText(IDD_HP, "");
  138.       }
  139.       hPrevWnd = hWndFromPt;
  140.       hPrevParent = hParent;
  141.     }
  142.  
  143.     // Update the focus display fields if necessary.
  144.     //
  145.     hFocus = ::GetFocus();
  146.     if (!hFocus) {
  147.       if (hFocus != hPrevFocus) {
  148.         SetDlgItemText(IDD_HF, "");
  149.         SetDlgItemText(IDD_LEFT, "");
  150.         SetDlgItemText(IDD_TOP, "");
  151.         SetDlgItemText(IDD_RIGHT, "");
  152.         SetDlgItemText(IDD_BOTTOM, "");
  153.       }
  154.     } else {
  155.       if (hFocus != hPrevFocus) {
  156.         SetDlgItemText(IDD_HF, itoa((UINT)hFocus, buffer, 16));
  157.       }
  158.       ::GetWindowRect(hFocus, &Rect);
  159.       if (Rect != prevRect) {
  160.         prevRect = Rect;
  161.         SetDlgItemText(IDD_LEFT, ltoa(Rect.left, buffer, 10));
  162.         SetDlgItemText(IDD_TOP, ltoa(Rect.top, buffer, 10));
  163.         SetDlgItemText(IDD_RIGHT, ltoa(Rect.right, buffer, 10));
  164.         SetDlgItemText(IDD_BOTTOM, ltoa(Rect.bottom, buffer, 10));
  165.       }
  166.     }
  167.     hPrevFocus = hFocus;
  168.   }
  169. }
  170.  
  171. //----------------------------------------------------------------------------
  172.  
  173. class TCursorApplication : public TApplication {
  174.   public:
  175.     TCursorApplication() : TApplication("Cursor Location App") {}
  176.     void InitMainWindow() {
  177.       EnableCtl3d();
  178.       Dialog = new TCursorDlg(0, "CursorDlg");
  179.       MainWindow = new TFrameWindow(0, "Cursor Location", Dialog, TRUE);
  180.       MainWindow->SetIcon(this, "CursorDlg");
  181.       MainWindow->Attr.Style &= ~(WS_MAXIMIZEBOX|WS_THICKFRAME);
  182.     }
  183.  
  184.     BOOL IdleAction(long) {
  185.       if (Dialog && Dialog->ShouldUpdate())
  186.         Dialog->UpdateDialog(); 
  187.       return TRUE;                // Keep calling us
  188.     }
  189.       
  190.   private:
  191.     TCursorDlg* Dialog;
  192. };
  193.  
  194. int
  195. OwlMain(int /*argc*/, char* /*argv*/ [])
  196. {
  197. #if defined(__WIN32__)
  198.   if (!(::GetVersion()&0x80000000)) {
  199.     ::MessageBox(0, "This is not a Win NT Example", "OWL Examples", MB_OK);
  200.     return 0;
  201.   }
  202. #endif
  203.   return TCursorApplication().Run();
  204. }
  205.