winform窗体扁平化设置,窗体移动,关闭

1,Winform窗体:设置扁平化

 

2,窗体移动

【2.1】主要代码:

        #region 窗体移动

        private Point mouseOff;//鼠标移动位置变量
        private bool leftFlag;//标签是否为左键
        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }
        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        private void Frm_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }

        #endregion

 

【2.2】 窗体移动代码使用:把代码复制到这个位置

 

 

 

【2.3】 绑定窗体移动代码

注意:需要点击哪个控件移动,就绑定哪个控件的事件(我这里司绑定上面的Panel控件)

 

3,其他的窗体移动代码

【3.1】其他代码1:这个带需要绑定2个事件代码更简洁

        #region 窗体移动

        private Point mPoint;

        private void Frm_MouseDown(object sender, MouseEventArgs e)
        {

            mPoint = new Point(e.X, e.Y);
        }

        private void Frm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location=new Point(this.Location.X+e.X-mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        #endregion

 

【3.2】其他代码2:调用windows的api实现无边框窗体移动,只用绑定一个事件

        #region 调用windows的api实现无边框窗体移动


        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }


        #endregion

 

4,程序退出,关闭窗口

        private void Exit()
        {
            //1.this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出;

            //2.Application.Exit(); 强制所有消息中止,退出所有的窗体,但是若有托管线程(非主线程),也无法干净地退出;

            //3.Application.ExitThread(); 强制中止调用线程上的所有消息,同样面临其它线程无法正确退出的问题;

            //4.System.Environment.Exit(0); 这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。(主要针对在任务栏关闭窗口而没有关闭端口号的问题))

            //【关闭当前窗口】
            System.Environment.Exit(0);
        }

  

 

posted @ 2021-11-26 22:57  包子789654  阅读(517)  评论(0编辑  收藏  举报