移动自绘制对话框的两种方法。

1、原理:左键按下记录一个点坐标,并且设置拖拽变量true,抬起来又记录一个点,有了位移差,在移动响应里面去移动窗体就行。
1.1 关键成员:

public:
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
protected:
	CRect m_rcTitle;
	CRect m_rcClose;
	CRect m_rcMin;
	bool m_bDraging;
	CPoint m_ptOffset;

1.2 构造函数里面赋给拖拽变量false。

CMoveWindowDemoDlg::CMoveWindowDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMoveWindowDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bDraging = false;
}

1.3 关键成员实现:

void CMoveWindowDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_bDraging = false;

	if (m_rcClose.PtInRect(point))
	{
		OnBnClickedCancel();
	}

	if (m_rcTitle.PtInRect(point))
	{
		m_bDraging = true;
		m_ptOffset = point;
		SetCapture();//非常重要,无此句,拖拽过程中会丢失窗口。
		return;
	}
	CDialogEx::OnLButtonDown(nFlags, point);
}


void CMoveWindowDemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_bDraging = false;
	ReleaseCapture();
	CDialogEx::OnLButtonUp(nFlags, point);
}


void CMoveWindowDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	if (m_bDraging)
	{
		CRect rc;
		GetWindowRect(&rc);
		CPoint pts;
		GetCursorPos(&pts);

		rc.MoveToXY(pts.x - m_ptOffset.x, pts.y - m_ptOffset.y);
		MoveWindow(&rc);
	}
	Invalidate();
	CDialogEx::OnMouseMove(nFlags, point);
}

2、原理:响应非客户区点击响应,改变点击响应效果。

LRESULT CMoveWindowDemoDlg::OnNcHitTest(CPoint point)
{
	LRESULT uHitTest = CDialogEx::OnNcHitTest(point);

	//获取客户区大小
	CRect rect, rcCaption;//定义矩形区,标题区
	GetClientRect(&rect);//获取客户区
	rcCaption = rect;
	rcCaption.bottom = 30;

	ScreenToClient(&point);//将给定的屏幕坐标位置或在中演示的矩形到客户端协调。

	if (rcCaption.PtInRect(point) && !m_rcClose.PtInRect(point) && !m_rcMin.PtInRect(point))
	{
		if (uHitTest == HTCLIENT)
		{
			uHitTest = HTCAPTION;
		}
	}
	return uHitTest;

	return CDialogEx::OnNcHitTest(point);
}

3、拖动效果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值