C#中无边框窗体拖动
xiancheng·zhang 人气:01 //定义一个bool变量标识是否拖动窗体 2 private bool isMove = false; 3 //记录鼠标的位置 4 private Point point;
1 /// <summary> 2 /// 窗体的鼠标按下事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void Form1_MouseDown(object sender, MouseEventArgs e) 7 { 8 isMove = true; 9 //记录鼠标的位置 10 point = e.Location; 11 }
1 /// <summary> 2 /// 窗体的鼠标移动事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void Form1_MouseMove(object sender, MouseEventArgs e) 7 { 8 //是否移动窗体 9 if (isMove) 10 { 11 //记录鼠标移动后的值 12 Point p = e.Location; 13 //计算鼠标按下和移动后的距离 14 int x = p.X - point.X; 15 int y = p.Y - point.Y; 16 //窗体的位置 17 this.Location = new Point(this.Location.X + x, this.Location.Y + y); 18 } 19 }
1 /// <summary> 2 /// 鼠标弹起事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void Form1_MouseUp(object sender, MouseEventArgs e) 7 { 8 //不允许在移动窗体 9 isMove = false; 10 }
加载全部内容