VC++无标题窗口以及控件拖动的方法(修正)

无标题窗口拖动:

方法一:直接在OnNcHitTest中虚拟发送HTCAPTION消息

UINT CMainFrame::OnNcHitTest(CPoint point) 
{
 RECT rectWindows, rectClient;
 this->GetWindowRect(&rectWindows);
 this->GetClientRect(&rectClient);
 
 if (point.y > rectWindows.top && point.y < rectWindows.top + 25)
 {
  return HTCAPTION;//标题栏形式
 }
 else
 {
  return CFrameWnd::OnNcHitTest(point);
 }
} 

方法二:直接在OnLButtonDown中虚拟发送WM_NCLBUTTONDOWN,HTCAPTION消息

 

void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if (point.y < 26)
		PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
	
	CDialog::OnLButtonDown(nFlags, point);
} 


方法三:通过在OnMouseMove中手动进行处理

void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
       static CPoint PrePoint = CPoint(0, 0);
       if(MK_LBUTTON == nFlags)
       {
             if(point != PrePoint)
             {
                    CPoint ptTemp = point - PrePoint;
                    CRect rcWindow;
                    GetWindowRect(&rcWindow);
                    rcWindow.OffsetRect(ptTemp.x, ptTemp.y);
                    MoveWindow(&rcWindow);
                    return ;
              }

       }

       PrePoint = point;
       CDialog::OnMouseMove(nFlags, point);
}

 

控件拖动:

控件拖动只能采用上述的第三种方法

你可能感兴趣的:(vc++)