移动无边框窗体
Posted 学习靠自己
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了移动无边框窗体相关的知识,希望对你有一定的参考价值。
开发环境:vs2010,步骤如下所示:
1、创建一个Windows应用程序,窗体默认Form1,将Form1的FormBorderStyle属性值设置为None
2、代码如下所示:
1 namespace Kaifafanli 2 { 3 public partial class Form1 : Form 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 } 9 //设置全局变量,初始化 10 bool beginMove = false; 11 int currentXPosition = 0; 12 int currentYPosition = 0; 13 private void Form1_MouseDown(object sender, MouseEventArgs e) 14 { 15 beginMove = true; 16 currentXPosition = MousePosition.X;//鼠标的x坐标为当前窗体左上角x坐标 17 currentYPosition = MousePosition.Y; 18 } 19 20 private void Form1_MouseMove(object sender, MouseEventArgs e) 21 { 22 if(beginMove) 23 { 24 this.Left += MousePosition.X - currentXPosition;//根据鼠标x坐标确定窗体的左边坐标x 25 this.Top += MousePosition.Y - currentYPosition; 26 currentXPosition = MousePosition.X; 27 currentYPosition = MousePosition.Y; 28 } 29 } 30 31 private void Form1_MouseUp(object sender, MouseEventArgs e) 32 { 33 beginMove = false; 34 } 35 36 private void Form1_MouseLeave(object sender, EventArgs e) 37 { 38 currentXPosition = 0; 39 currentYPosition = 0; 40 beginMove = false; 41 } 42 43 44 } 45 }
以上是关于移动无边框窗体的主要内容,如果未能解决你的问题,请参考以下文章