mfc获取鼠标在其他窗口中坐标_C井编程,稍加修改,将之前“会跑的按钮”改成“会跑的窗口”...

开发工具:VS2010;.net framework 3.5

步骤:

1、打开VS2010

2、新建WinForm项目

3、窗体放一个标签Lable控件显示文字信息,一个定时器Timer控件10ms间隔循环检查鼠标坐标是不是靠近窗口,是:让窗口跑,否:什么也不做

4、Timer控件Tick事件下输入主要代码

5、完成

重点:获取屏幕尺寸;获取窗口相对屏幕位置;获取鼠标相对屏幕的坐标

一、程序运行截图:

ba793c73093fad23d8fe87d27ece7238.gif

二、主要代码:

private void timer1_Tick(object sender, EventArgs e)

{

Rectangle ScreenArea = Screen.GetWorkingArea(this);//获取屏幕大小

int margin = 30;

int border = 50;

int x = Control.MousePosition.X;//鼠标相对屏幕坐标

int y = Control.MousePosition.Y;//鼠标相对屏幕坐标

int left = this.DesktopLocation.X;//窗口相对屏幕坐标

int right = this.DesktopLocation.X + this.Width;

int top = this.DesktopLocation.Y;//窗口相对屏幕坐标

int bottom = this.DesktopLocation.Y + this.Height;

//鼠标到窗体附近(margin个象素)

if (x > left - border && x < right + border && y > top - border && y < bottom + border)

{

this.Top += (y > top ? -margin : margin);

if (this.Top > ScreenArea.Height || this.Bottom < 0)

{

this.Top = ScreenArea.Height / 2;

}

this.Left += (x > left ? -margin : margin);

if (this.Left > ScreenArea.Width || this.Right < 0)

{

this.Left = ScreenArea.Width / 2;

}

}

}

三、提示:

查看操作实录视频请关注“业余码农说”,查看发布的同名视频即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值