窗体的鼠标拖动,添加无边框窗体的鼠标拖动可

作者:计算机知识

窗体的鼠标拖动,添加无边框窗体的鼠标拖动可以移动效果。起来加多变量:

C#分界面设计,增加无边框窗体的鼠标拖动能够移动作效果果,

始发增多变量:

Point mouseOff;//鼠标移动地点变量

bool leftFlag;//标签是或不是为左键

 

事件部分:

 

 1  private void Login_MouseMove(object sender, MouseEventArgs e)
 2         {
 3             //鼠标指向检测
 4             if (leftFlag)
 5 
 6             {
 7 
 8                 Point mouseSet = Control.MousePosition;
 9 
10                 mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
11 
12                 Location = mouseSet;
13 
14             }
15             //
16         }
17 
18         private void Login_MouseUp(object sender, MouseEventArgs e)
19         {
20 
21             if (leftFlag)
22 
23             {
24 
25                 leftFlag = false;//释放鼠标后标注为false;
26 
27             }
28         }
29 
30         private void Login_MouseDown(object sender, MouseEventArgs e)
31         {
32             if (e.Button == MouseButtons.Left)
33 
34             {
35 
36                 mouseOff = new Point(-e.X, -e.Y); //得到变量的值
37 
38                 leftFlag = true;                  //点击左键按下时标注为true;
39 
40             }
41         }

ps:很常用的几串字符。

  记住! 记住! 记住!

早先增多变量: Point mouseOff;//鼠标移动地方变量 bool leftFlag;//标签是还是不是为左键 事件...

为了赏心悦目,去掉窗体的标题栏,给窗体的FormBorderStyle属性设置为None,那时,使用鼠标拖动和缩放窗口就能够失灵。上边三种简易方法来苏醒所需效果。

Point mouseOff;//鼠标移动地方变量

鼠标的拖动

  • 注册 窗体的MouseDownMouseMove事件,通过安装窗体的Location来落到实处移动。(较轻便,无需丰裕任何因素)
public partial class FmMain : Form {
        private Point formPoint = new Point();

        public FmMain() {
            InitializeComponent();
        }

        private void FmMain_MouseMove(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Left) {
                Point myPosittion = MousePosition;
                myPosittion.Offset(-formPoint.X, -formPoint.Y);
                Location = myPosittion;
            }
        }

        private void FmMain_MouseDown(object sender, MouseEventArgs e) {
            formPoint.X = e.X;
            formPoint.Y = e.Y;
        }
}
  • 安装一个透明的 panel 置于底层,通过设置 panel 的MouseDownMouseMove事件,来兑现窗体的拖动。(多增添了贰个成分,完结格局和第2种大概)
public partial class FmMain : Form {
        private Point mousePoint = new Point();

        public FmMain() {
            InitializeComponent();
        }

        private void panelMain_MouseDown(object sender, MouseEventArgs e) {
            base.OnMouseDown(e);
            this.mousePoint.X = e.X;
            this.mousePoint.Y = e.Y;
        }

        private void panelMain_MouseMove(object sender, MouseEventArgs e) {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left) {
                this.Top = Control.MousePosition.Y - mousePoint.Y;
                this.Left = Control.MousePosition.X - mousePoint.X;
            }
        }
 }

英特网还应该有其余方法,相相比那二种多少有一点复杂。

bool leftFlag;//标签是或不是为左键

本文由bwin必赢发布,转载请注明来源

关键词: C# 亚洲必赢cc366net