home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CURSOR.PAK / CURSOR.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.9 KB  |  236 lines

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