1- #include " stdafx.h"
1+ #include " stdafx.h"
22#include " WindowImpl.h"
33#include " PlatformImpls.h"
44#include < mutex>
@@ -7,6 +7,8 @@ using namespace Snowing::PlatformImpls::WindowsImpl;
77
88static constexpr auto dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
99
10+ #define GET_X_LPARAM (lp ) ((int )(short )LOWORD(lp))
11+ #define GET_Y_LPARAM (lp ) ((int )(short )HIWORD(lp))
1012
1113#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
1214
@@ -64,10 +66,30 @@ static void hwndDeleter(void* hwnd)
6466 UnregisterClass (className, GetModuleHandle (nullptr ));
6567}
6668
69+ // /////// 这里有一堆用于处理窗口移动的屎
70+ // 当这个变量不存在时,不在移动中
71+ // 当这个变量存在时,记录了鼠标位置到窗口左上角的距离
72+ static std::optional<Snowing::Math::Vec2<int >> movingMousePositionToLTCorner;
73+
6774static void ProcesSysCommand (HWND wnd, UINT msg, WPARAM w, LPARAM l)
6875{
6976 switch (w & 0xFFF0 )
7077 {
78+ case SC_MOVE:
79+ {
80+ RECT r;
81+ if (!GetWindowRect (wnd, &r))
82+ throw std::runtime_error{ " Can not get window rect for move." };
83+ POINT mousePos;
84+ if (!GetCursorPos (&mousePos))
85+ throw std::runtime_error{ " Can not get mouse position for move." };
86+
87+ movingMousePositionToLTCorner.emplace (Snowing::Math::Vec2<int >{
88+ mousePos.x - r.left ,
89+ mousePos.y - r.top
90+ });
91+ break ;
92+ }
7193 case SC_KEYMENU:
7294 case SC_MOUSEMENU:
7395 break ;
@@ -82,14 +104,15 @@ static LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM w, LPARAM l)
82104 auto currentWindow = &WindowImpl::Get ();
83105 switch (msg)
84106 {
107+ case WM_MOVE:
108+ case WM_MOVING:
85109 case WM_NCRBUTTONDOWN:
86110 break ;
87111
88112 case WM_SYSCOMMAND:
89113 ProcesSysCommand (wnd, msg, w, l);
90114 break ;
91115
92-
93116 case WM_CLOSE:
94117 if (currentWindow)
95118 if (currentWindow->GetWMCloseHandler ())
@@ -191,6 +214,32 @@ Snowing::PlatformImpls::WindowsImpl::WindowImpl::WindowImpl(const wchar_t* title
191214
192215bool Snowing::PlatformImpls::WindowsImpl::WindowImpl::Update ()
193216{
217+ // 处理窗口移动的屎的开始
218+ if (movingMousePositionToLTCorner.has_value ())
219+ {
220+ // 如果鼠标松开则退出窗口移动模式
221+ if ((GetAsyncKeyState (VK_LBUTTON) & 0x8001 ) == 0 )
222+ movingMousePositionToLTCorner.reset ();
223+ else
224+ {
225+
226+ POINT mousePoint;
227+ if (!GetCursorPos (&mousePoint))
228+ throw std::runtime_error{ " Can not get mouse point for move." };
229+ RECT r;
230+ if (!GetWindowRect (hwnd_.Get <HWND>(), &r))
231+ throw std::runtime_error{ " Can not get window rect for move." };
232+ MoveWindow (
233+ hwnd_.Get <HWND>(),
234+ mousePoint.x - movingMousePositionToLTCorner->x ,
235+ mousePoint.y - movingMousePositionToLTCorner->y ,
236+ r.right - r.left ,
237+ r.bottom - r.top ,
238+ FALSE );
239+ }
240+ }
241+ // 处理窗口移动的屎的结束
242+
194243 MSG msg = { 0 };
195244
196245 while (PeekMessage (&msg, hwnd_.Get <HWND>(), 0 , 0 , PM_REMOVE))
0 commit comments