FocusHighlightDlg.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // FocusHighlightDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "FocusHighlight.h"
  5. #include "FocusHighlightDlg.h"
  6. #include "afxdialogex.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. CFocusHighlightDlg::CFocusHighlightDlg(CWnd* pParent /*=NULL*/)
  11. : CDialogEx(CFocusHighlightDlg::IDD, pParent)
  12. {
  13. }
  14. void CFocusHighlightDlg::DoDataExchange(CDataExchange* pDX)
  15. {
  16. CDialogEx::DoDataExchange(pDX);
  17. DDX_Control(pDX, IDC_EDIT_ACTIVE, m_activeEdit);
  18. DDX_Control(pDX, IDC_EDIT_FOCUS, m_focusEdit);
  19. DDX_Control(pDX, IDC_CHECK_TRACK_FOCUS_CHANGES, m_trackFocusChangesCheck);
  20. }
  21. BEGIN_MESSAGE_MAP(CFocusHighlightDlg, CDialogEx)
  22. ON_WM_TIMER()
  23. ON_BN_CLICKED(IDC_BUTTON_HIGHLIGHT_ACTIVE, &CFocusHighlightDlg::OnBnClickedButtonHighlightActive)
  24. ON_BN_CLICKED(IDC_BUTTON_HIGHLIGHT_FOCUS, &CFocusHighlightDlg::OnBnClickedButtonHighlightFocus)
  25. END_MESSAGE_MAP()
  26. BOOL CFocusHighlightDlg::OnInitDialog()
  27. {
  28. CDialogEx::OnInitDialog();
  29. StayOnTop();
  30. ::CheckDlgButton(m_hWnd, IDC_CHECK_TRACK_FOCUS_CHANGES, BST_CHECKED);
  31. SetTimer(1, 2000, NULL);
  32. return TRUE; // return TRUE unless you set the focus to a control
  33. }
  34. void CFocusHighlightDlg::OnTimer(UINT_PTR nIDEvent)
  35. {
  36. switch(nIDEvent)
  37. {
  38. case 1:
  39. if(::IsDlgButtonChecked(m_hWnd, IDC_CHECK_TRACK_FOCUS_CHANGES) != 0)
  40. {
  41. m_tracker.TrackActiveWnd();
  42. CString cs;
  43. cs.Format(_T("0x%08x - %s"), m_tracker.ActiveWnd(), m_tracker.ActiveWndName());
  44. m_activeEdit.SetWindowText(cs);
  45. cs.Format(_T("0x%08x - %s"), m_tracker.FocusWnd(), m_tracker.FocusWndName());
  46. m_focusEdit.SetWindowText(cs);
  47. }
  48. break;
  49. }
  50. CDialogEx::OnTimer(nIDEvent);
  51. }
  52. void CFocusHighlightDlg::StayOnTop()
  53. {
  54. CRect rect;
  55. // get the current window size and position
  56. GetWindowRect( rect );
  57. // now change the size, position, and Z order
  58. // of the window.
  59. ::SetWindowPos(m_hWnd , // handle to window
  60. HWND_TOPMOST, // placement-order handle
  61. rect.left, // horizontal position
  62. rect.top, // vertical position
  63. rect.Width(), // width
  64. rect.Height(), // height
  65. SWP_SHOWWINDOW); // window-positioning options);
  66. }
  67. void CFocusHighlightDlg::OnBnClickedButtonHighlightActive()
  68. {
  69. HWND activeWnd = m_tracker.ActiveWnd();
  70. CRect cr;
  71. ::GetWindowRect(activeWnd, &cr);
  72. ::ScreenToClient(activeWnd, &cr.TopLeft());
  73. ::ScreenToClient(activeWnd, &cr.BottomRight());
  74. HDC hdc = ::GetWindowDC(activeWnd);
  75. for(int i = 0; i < 10; i++)
  76. {
  77. ::DrawFocusRect(hdc, cr);
  78. Sleep(200);
  79. }
  80. ::ReleaseDC(activeWnd, hdc);
  81. }
  82. void CFocusHighlightDlg::OnBnClickedButtonHighlightFocus()
  83. {
  84. HWND focusWnd = m_tracker.FocusWnd();
  85. CRect cr;
  86. ::GetWindowRect(focusWnd, &cr);
  87. ::ScreenToClient(focusWnd, &cr.TopLeft());
  88. ::ScreenToClient(focusWnd, &cr.BottomRight());
  89. HDC hdc = ::GetWindowDC(focusWnd);
  90. for(int i = 0; i < 10; i++)
  91. {
  92. ::DrawFocusRect(hdc, cr);
  93. Sleep(200);
  94. }
  95. ::ReleaseDC(focusWnd, hdc);
  96. }