Saturday, February 21, 2009

How do I let the user drag a window which has no titlebar?

There are several ways. One quick-n-dirty method is handle the WM_NCHITTEST message and return HTCAPTION for the places where you want the user to be able to drag (i.e., pretend your window has a caption when it doesn't).

For someone who wants to get nitty-gritty and cut the code themselves (and doesn't care about the inefficiencies involved), the code below demonstrates dragging using the right mouse button :

void CTestbed2Dlg::OnRButtonDown(UINT nFlags, CPoint point)
{
ClientToScreen (&point);
m_LastPoint = point;
m_bMoving = TRUE;
}

// ------------------------------------------------------------------------

void CTestbed2Dlg::OnMouseMove(UINT nFlags, CPoint point)
{
RECT cr;

ClientToScreen (&point);

if (m_bMoving)
{
if ((point.x != m_LastPoint.x) ||
(point.y != m_LastPoint.y) )
{
GetWindowRect (&cr);

int iX = cr.left - (m_LastPoint.x - point.x);
int iY = cr.top - (m_LastPoint.y - point.y);
int iWidth = cr.right - cr.left;
int iHeight = cr.bottom - cr.top;

MoveWindow (iX, iY, iWidth, iHeight);
}
m_LastPoint = point;
}
else
CDialog::OnMouseMove(nFlags, point);
}

// ------------------------------------------------------------------------

void CTestbed2Dlg::OnRButtonUp(UINT nFlags, CPoint point)
{
if (m_bMoving)
m_bMoving = FALSE;
else
CDialog::OnMButtonUp(nFlags, point);
}

Author : Bob Moore

0 nhận xét:

Post a Comment