SnapWindow.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "stdafx.h"
  2. #include "SnapWindow.h"
  3. SnapWindow::SnapWindow()
  4. {
  5. snap_ModifierKey = VK_SHIFT;
  6. NONCLIENTMETRICS ncm = { 0 };
  7. ncm.cbSize = sizeof ncm;
  8. SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
  9. snap_Margin = ncm.iCaptionHeight;
  10. snap_cur_pos.x = 0;
  11. snap_cur_pos.y = 0;
  12. snap_rcWindow.bottom = 0;
  13. snap_rcWindow.left = 0;
  14. snap_rcWindow.right = 0;
  15. snap_rcWindow.top = 0;
  16. snap_x = 0;
  17. snap_y = 0;
  18. }
  19. SnapWindow::~SnapWindow()
  20. {
  21. }
  22. BOOL SnapWindow::isSnapClose(int a, int b)
  23. {
  24. return (abs(a - b) < snap_Margin);
  25. }
  26. LRESULT SnapWindow::OnSnapEnterSizeMove(HWND hWnd)
  27. {
  28. snap_cur_pos.x = 0;
  29. snap_cur_pos.y = 0;
  30. snap_rcWindow.bottom = 0;
  31. snap_rcWindow.left = 0;
  32. snap_rcWindow.right = 0;
  33. snap_rcWindow.top = 0;
  34. GetWindowRect(hWnd, &snap_rcWindow);
  35. GetCursorPos(&snap_cur_pos);
  36. snap_x = snap_cur_pos.x - snap_rcWindow.left;
  37. snap_y = snap_cur_pos.y - snap_rcWindow.top;
  38. return 0;
  39. }
  40. LRESULT SnapWindow::OnSnapMoving(HWND hWnd, LPRECT snap_prc)
  41. {
  42. //no snap if shift key pressed
  43. if (GetAsyncKeyState(snap_ModifierKey) < 0)
  44. return FALSE;
  45. snap_cur_pos.x = 0;
  46. snap_cur_pos.y = 0;
  47. snap_rcWindow.bottom = 0;
  48. snap_rcWindow.left = 0;
  49. snap_rcWindow.right = 0;
  50. snap_rcWindow.top = 0;
  51. GetCursorPos(&snap_cur_pos);
  52. OffsetRect(snap_prc,
  53. snap_cur_pos.x - (snap_prc->left + snap_x),
  54. snap_cur_pos.y - (snap_prc->top + snap_y));
  55. //it may change during app lifetime
  56. SystemParametersInfo(SPI_GETWORKAREA, 0, &snap_wa, 0);
  57. MONITORINFO mi;
  58. HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
  59. mi.cbSize = sizeof(mi);
  60. GetMonitorInfo(hMonitor, &mi);
  61. snap_wa = mi.rcWork;
  62. if (isSnapClose(snap_prc->left, snap_wa.left))
  63. {
  64. OffsetRect(snap_prc, snap_wa.left - snap_prc->left, 0);
  65. }
  66. else
  67. {
  68. if (isSnapClose(snap_wa.right, snap_prc->right))
  69. {
  70. OffsetRect(snap_prc, snap_wa.right - snap_prc->right, 0);
  71. }
  72. }
  73. if (isSnapClose(snap_prc->top, snap_wa.top))
  74. {
  75. OffsetRect(snap_prc, 0, snap_wa.top - snap_prc->top);
  76. }
  77. else
  78. {
  79. if (isSnapClose(snap_wa.bottom, snap_prc->bottom))
  80. {
  81. OffsetRect(snap_prc, 0, snap_wa.bottom - snap_prc->bottom);
  82. }
  83. }
  84. return TRUE;
  85. }