MFC无边框对话框鼠标拖动和缩放

三步实现MFC对话框Boder为None对话框鼠标拖动缩放功能。

第一步,实现对话框的WM_NCHITTEST消息,代码如下:

LRESULT CTestDlg::OnNcHitTest(CPoint point)
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	CRect rect;
	GetWindowRect(&rect);
	if (point.x <= rect.left + 3 && point.y <= rect.top + 3){
		return HTTOPLEFT;
	}
	else if (point.x >= rect.right - 3 && point.y <= rect.top + 3){
		return HTTOPRIGHT;
	}
	else if (point.x <= rect.left + 3 && point.y >= rect.bottom - 3){
		return HTBOTTOMLEFT;
	}
	else if (point.x >= rect.right - 3 && point.y >= rect.bottom - 3){
		return HTBOTTOMRIGHT;
	}
	else if (point.x <= rect.left + 3){
		return HTLEFT;
	}
	else if (point.x >= rect.right - 3){
		return HTRIGHT;
	}
	else if (point.y <= rect.top + 3){
		return HTTOP;
	}
	else if (point.y >= rect.bottom - 3){
		return HTBOTTOM;
	}
	return ((GetStyle() & WS_CHILD) == 0) ? HTCAPTION : HTTRANSPARENT;
}

第二步,实现WM_SETCURSOR消息,代码如下:

BOOL CTestDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	if (nHitTest == HTCAPTION || nHitTest == HTSYSMENU || nHitTest == HTMENU || nHitTest == HTCLIENT){
		SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
	}
	else if (nHitTest == HTTOP || nHitTest == HTBOTTOM){
		SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
	}
	else if (nHitTest == HTLEFT || nHitTest == HTRIGHT){
		SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
	}
	else if (nHitTest == HTTOPLEFT || nHitTest == HTBOTTOMRIGHT){
		SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
	}
	else if (nHitTest == HTTOPRIGHT || nHitTest == HTBOTTOMLEFT){
		SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
	}
	else{
		SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
	}
	return TRUE;
}

到此,鼠标移动到对话的边界上,鼠标样式会对应变化,但对话框是不能拖动缩放的。
第三步,实现WM_NCLBUTTONDOWN消息,代码如下:

void CTestDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	if (nHitTest == HTTOP)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTBOTTOM)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTLEFT)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTRIGHT)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTTOPLEFT)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTTOPRIGHT)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTBOTTOMLEFT)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
	else if (nHitTest == HTBOTTOMRIGHT)
		SendMessage(WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
	else
		CDialogEx::OnNcLButtonDown(nHitTest, point);
}

至此,对话框就可以跟着鼠标实现缩放效果,代码完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值