C#Winform窗口移动方法
作者:Maybe_ch 时间:2023-04-10 04:46:18
在我们将Winform自带的边框隐藏之后,我们需要自己编写窗口的移动。
思路就是
1.获得点击左键时当前鼠标的坐标
2.获得移动后鼠标的坐标
3.窗体的坐标=移动后的鼠标坐标-移动前的鼠标坐标
private Point mouseOff;//鼠标移动位置变量
private bool leftFlag;//鼠标是否为左键
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
mouseOff = new Point(-e.X, -e.Y);//获得当前鼠标的坐标
leftFlag = true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (leftFlag)
{
Point mouseSet = Control.MousePosition;//获得移动后鼠标的坐标
mouseSet.Offset(mouseOff.X, mouseOff.Y);//设置移动后的位置
Location = mouseSet;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (leftFlag)
{
leftFlag = false;
}
}
来源:https://blog.csdn.net/Maybe_ch/article/details/81482054
标签:C#,Winform,窗口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven工程中jar包瘦身的五种方法
2023-11-04 05:01:54
关于jdk环境变量的配置方式解读
2023-04-22 14:53:05
![](https://img.aspxhome.com/file/2023/6/61306_0s.png)
Android如何实现锁屏状态下弹窗
2021-05-27 03:38:41
Android实现图片缓存与异步加载
2022-02-25 14:21:06
Android Recyclerview实现多选,单选,全选,反选,批量删除的功能
2021-12-10 21:57:24
![](https://img.aspxhome.com/file/2023/6/96776_0s.jpg)
Android中判断网络连接是否可用及监控网络状态
2022-09-07 05:24:25
Java硬币翻转倍数递增试算实例
2021-09-29 08:39:22
详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)
2023-11-06 21:35:12
![](https://img.aspxhome.com/file/2023/0/58690_0s.png)
c#循环中产生伪随机数
2023-05-17 23:09:40
Springboot使用filter对response内容进行加密方式
2023-11-17 15:29:25
![](https://img.aspxhome.com/file/2023/5/90955_0s.png)
C#9特性record 类型、模式匹配、init 属性详情
2023-07-09 04:48:25
![](https://img.aspxhome.com/file/2023/9/126019_0s.png)
Kotlin语言使用WebView示例介绍
2021-10-14 05:56:21
![](https://img.aspxhome.com/file/2023/8/138338_0s.jpg)
ToStringBuilder类的一些心得
2022-10-10 04:02:27
SpringMVC实现上传下载文件
2022-11-10 03:18:14
![](https://img.aspxhome.com/file/2023/9/72409_0s.jpg)
java实现二叉树的创建及5种遍历方法(总结)
2022-03-14 09:00:28
使用JPA主键@Id,@IdClass,@Embeddable,@EmbeddedId问题
2022-07-07 18:35:41
Android实现多级树形菜单并支持多选功能
2023-08-29 20:01:04
![](https://img.aspxhome.com/file/2023/7/103907_0s.jpg)
C语言算法打卡回文串验证算法题解
2022-05-29 22:54:10
![](https://img.aspxhome.com/file/2023/0/127520_0s.png)
Spring Security配置多个数据源并添加登录验证码的实例代码
2022-11-19 13:49:26
![](https://img.aspxhome.com/file/2023/6/69126_0s.png)
SpringCloud之@FeignClient()注解的使用方式
2022-05-16 04:22:40
![](https://img.aspxhome.com/file/2023/9/82979_0s.jpg)