这里无标题 指的是: Window 标签属性 caption="0,0,0,0"
方法1:
LRESULT Mydlg::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
{
if (uMsg == WM_LBUTTONDOWN)
{
POINT point = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
ReleaseCapture();
SendMessage(WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
}
方法二:
在使用过程中有特需需求添加移动功能. 具体代码:
bool m_bStartMove = false;
POINT m_StartPt;
LRESULT CTestDialog::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
{
if (uMsg == WM_MOUSEMOVE) {
if (::GetAsyncKeyState(VK_LBUTTON) != 0 && m_bStartMove)
{
if (!IsDragRect(pt)) return 0;
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
int dx = pt.x - m_StartPt.x;
int dy = pt.y - m_StartPt.y;
RECT curRect;
::GetWindowRect(m_hWnd, &curRect);
int cx = curRect.left + dx;
int cy = curRect.top + dy;
int w = curRect.right - curRect.left;
int h = curRect.bottom - curRect.top;
SetWindowPos(m_hWnd, NULL, cx, cy, w, h, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
} else if (uMsg == WM_LBUTTONDOWN){
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
m_bStartMove = TRUE;
m_StartPt = pt;
}else if (uMsg == WM_LBUTTONUP)
{
m_bStartMove = FALSE;
}
return FALSE;
}
// 这里实现类似标题栏的功能. 只有在 特定高度操作才生效:
bool CTestDialog::IsDragRect(POINT pt)
{
RECT rcWnd;
BOOL ret = ::GetWindowRect(m_hWnd, &rcWnd);
int captionHeight = 60; //caption heigth.
if (pt.y < captionHeight) {
return true;
}
return false;
}