WindowForm窗体的最小化、最大化、拖动窗体、关闭窗体、窗体阴影效果

窗体最小化

this.WindowState = FormWindowState.Minimized;

窗体最大化

 if (this.WindowState == FormWindowState.Maximized)
     {
         this.WindowState = FormWindowState.Normal;
     }
     else
     {
         this.WindowState = FormWindowState.Maximized;
     }

窗体关闭

在c#中退出WinForm程序包括有很多方法,他们各自的方法不一样,下面我们就来详细介绍一下。
1.只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出;

this.Close(); 

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

Application.Exit();

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

Application.ExitThread(); 

4.这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净。

System.Environment.Exit(0);  

等还有很多方法可以退出。


窗体拖动

private Point offset;
        /// 
        /// 拖动窗体
        /// 
        /// 
        /// 
        private void Bar_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            Point cur = MousePosition;
            this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
        }
        /// 
        /// 拖动窗体
        /// 
        /// 
        /// 
        private void Bar_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            Point cur = this.PointToScreen(e.Location);
            offset = new Point(cur.X - this.Left, cur.Y - this.Top);
        }

窗体阴影效果

using System.Runtime.InteropServices;//窗体边框阴影效果引用

 #region 窗体边框阴影效果变量申明
        const int CS_DropSHADOW = 0x20000;
        const int GCL_STYLE = (-26);
        //声明Win32 API
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);
        #endregion


//使用阴影效果,API函数加载,实现窗体边框阴影效果
 SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); 

你可能感兴趣的:(c#)