MFC基于Dialog的小程序:你能抓住我吗?

1. 在Dialog添加一个Button控件

2. 创建一个基于CButton的新的类:CmyButton

3. 在创建好的Button控件添加CmyButton(Contral)成员变量

4. 在CmyButton类中添加 WM_MOUSEMOVE消息处理函数:

void CmyButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
// 	ShowWindow(SW_HIDE);
// 	m_pbtn->ShowWindow(SW_SHOW);

	static BOOL isRect = TRUE;
	static int max_x;
	static int max_y;
	static int m_width;
	static int m_height;
	static int m_x = 0;
	static int m_y = 0;
	if (isRect)
	{
		CRect pRect,sRect;
		GetWindowRect(&sRect);
		GetParent()->GetWindowRect(&pRect);
		max_x = pRect.Width() - sRect.Width();
		max_y = pRect.Height() - sRect.Height() 
							- GetSystemMetrics(SM_CYCAPTION);;
		m_width = sRect.Width();
		m_height = sRect.Height();
		isRect = FALSE;
	}

	srand((unsigned)time(NULL));
	int x = m_x, y =m_y;
	while (abs(m_x-x) <= m_width && abs(m_y-y) <= m_height)
	{
		x = rand()%max_x;
		y = rand()%max_y;
	}
	m_x = x;
	m_y = y;

	SetWindowPos(this, m_x, m_y, m_width, m_height, SWP_NOZORDER);
	CButton::OnMouseMove(nFlags, point);
}

参考知识:

1.获取屏幕的高宽

g_nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
g_nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
这两个肯定不会有误
2.获取标题栏、 菜单 栏高度
g_nScreenCaption = GetSystemMetrics(SM_CYCAPTION);
g_nScreenMenu = GetSystemMetrics(SM_CYMENU);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值