Winform 最小化 最大化 关闭 点击任务栏隐藏显示 点击鼠标左键移动窗体
Posted biind
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winform 最小化 最大化 关闭 点击任务栏隐藏显示 点击鼠标左键移动窗体相关的知识,希望对你有一定的参考价值。
一 最大化
二 最小化
三 关闭
四 点击任务栏隐藏显示
五 点击鼠标左键移动窗体
六 阴影效果鼠标左键移动窗口
#region UI设置 最大化、最小化、关闭、鼠标移动窗口、点击任务栏切换窗口 this.pnlMin.MouseHover += new EventHandler(pnlMin_MouseMove);//最小化按钮的鼠标经过样式改变事件 this.pnlMin.MouseLeave += new EventHandler(pnlMin_MouseLeave);//最小化按钮的鼠标离开样式改变事件 this.pnlMin.Click += new EventHandler(pnlMin_Click);//最小化按钮单击事件 this.pnlMax.MouseHover += new EventHandler(pnlMax_MouseMove);//最小化按钮的鼠标经过样式改变事件 this.pnlMax.MouseLeave += new EventHandler(pnlMax_MouseLeave);//最小化按钮的鼠标离开样式改变事件 this.pnlMax.Click += new EventHandler(pnlMax_Click);//最小化按钮单击事件 this.pnlClose.MouseHover += new EventHandler(pnlClose_MouseMove);//鼠标经过关闭按钮时的样式改变 this.pnlClose.MouseLeave += new EventHandler(pnlClose_MouseLeave);//鼠标离开关闭按钮事件 this.pnlClose.Click += new EventHandler(pnlClose_Click);//关闭按钮的单击事件 this.pnlHead1.MouseDown += new MouseEventHandler(sysSet_MouseDown); //鼠标移动窗体位置 this.pnlHead1.MouseMove += new MouseEventHandler(sysSet_MouseMove);//鼠标移动窗体位置 this.pnlHead1.MouseUp += new MouseEventHandler(sysSet_MouseUp);//鼠标移动窗体位置 #endregion
#region UI设置 最大化、最小化、关闭、鼠标移动窗口、点击任务栏切换窗口 #region 单击任务栏,最小化 protected override CreateParams CreateParams //点击任务栏实现最小化与还原 { get { const int WS_MINIMIZEBOX = 0x00020000; CreateParams cp = base.CreateParams; cp.Style = cp.Style | WS_MINIMIZEBOX; // 允许最小化操作 return cp; } } #endregion #region 最小化按钮 //最小化按钮的鼠标经过改变样式事件 private void pnlMin_MouseMove(object sender, EventArgs e) { tip.SetToolTip(this.pnlMin, "最小化"); this.pnlMin.BackColor = Color.FromArgb(30, 255, 255, 255); } //最小化按钮的鼠标离开改变样式事件 private void pnlMin_MouseLeave(object sender, EventArgs e) { this.pnlMin.BackColor = Color.Transparent; } //最小化按钮的单击事件 private void pnlMin_Click(object sender, EventArgs e) { if (WindowState != FormWindowState.Minimized) { WindowState = FormWindowState.Minimized; } } #endregion #region 最大化 private void pnlMax_MouseMove(object sender, EventArgs e) { tip.SetToolTip(this.pnlMax, "最大化"); if (WindowState == FormWindowState.Maximized) { tip.SetToolTip(this.pnlMax, "恢复"); } this.pnlMax.BackColor = Color.FromArgb(30, 0, 0, 0); } //最小化按钮的鼠标离开改变样式事件 private void pnlMax_MouseLeave(object sender, EventArgs e) { this.pnlMax.BackColor = Color.Transparent; } //最小化按钮的单击事件 private void pnlMax_Click(object sender, EventArgs e) { int h = Screen.PrimaryScreen.WorkingArea.Height; int w = Screen.PrimaryScreen.WorkingArea.Width; if (WindowState == FormWindowState.Normal) { //this.MaximumSize = new Size(w, h);//设置窗体最大化为满屏 this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); this.pnlMax.BackgroundImage = global::Cnwin.Apps.Properties.Resources.max; //this.tbCnt.Dock = DockStyle.Right; //this.pnlCnt.Height = h - 10; //this.pnlCnt.Width = w - 2; //this.sptCnt.Width = 260; this.pnlMax.Invalidate(); WindowState = FormWindowState.Maximized; //PnlRslt.Left = (w / 2); //sptCnt.SplitterDistance = 80; //dgvRslt.Width = w - 100; } else { this.FormBorderStyle = FormBorderStyle.None; this.pnlMax.BackgroundImage = global::Cnwin.Apps.Properties.Resources.max16; //this.pnlCnt.Height = 629; //this.pnlCnt.Width = 964; this.MaximumSize = new Size(966, 630); this.pnlMax.Invalidate(); WindowState = FormWindowState.Normal; this.Height = 656; //PnlRslt.Left = 420; //sptCnt.SplitterDistance = 80; //dgvRslt.Width = 840; //dgvRslt.Height = 452; //pnlPage.Top = 508; } //int width = 0; int clmSum = this.dgvRslt.Columns.Count; ////对于DataGridView的每一个列都调整 //for (int i = 0; i < clmSum; i++) //{ // //将每一列都调整为自动适应模式 // this.dgvRslt.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells); // //记录整个DataGridView的宽度 // width += this.dgvRslt.Columns[i].Width; //} ////判断调整后的宽度与原来设定的宽度的关系,如果是调整后的宽度大于原来设定的宽度, ////则将DataGridView的列自动调整模式设置为显示的列即可, ////如果是小于原来设定的宽度,将模式改为填充。 //if (width > this.dgvRslt.Size.Width) //{ // this.dgvRslt.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; //} //else //{ // //this.dgvRslt.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //} ////this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea; } #endregion #region 关闭按钮 //鼠标经过关闭按钮时触发样式改变 private void pnlClose_MouseMove(object sender, EventArgs e) { tip.SetToolTip(this.pnlClose, "关闭"); this.pnlClose.BackColor = Color.Red; } //鼠标离开关闭按钮触发样式改变 private void pnlClose_MouseLeave(object sender, EventArgs e) { this.pnlClose.BackColor = Color.Transparent; } protected void pnlClose_Click(object sender, EventArgs e) { FrmMonitor lg = new FrmMonitor(); lg.Dispose(); Application.Exit(); } #endregion #region 鼠标移动窗体移动 //窗体拖动 bool isMouseDown = false; Point cwpt = new Point(); //当前窗体位置 Point cmpt = new Point(); //当前鼠标的按下位置 private void sysSet_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = true; cwpt = this.Location; cmpt = System.Windows.Forms.Control.MousePosition; } } private void sysSet_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; } private void sysSet_MouseMove(object sender, MouseEventArgs e) { int rangeX = 0, rangeY = 0; //计算当前鼠标光标的位移,让窗体进行相同大小的位移 if (isMouseDown) { Point pt = System.Windows.Forms.Control.MousePosition; rangeX = cmpt.X - pt.X; rangeY = cmpt.Y - pt.Y; this.Location = new Point(cwpt.X - rangeX, cwpt.Y - rangeY); } } #endregion #endregion
六 阴影效果鼠标左键移动窗口
1)引用API
//窗体阴影API private const int CS_DropSHADOW = 0x20000;//窗体边框阴影 private const int WM_MOVING = 0x0216;//窗体移动限制不超过屏幕 private const int GCL_STYLE = (-26); [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); [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)] public static extern int GetWindowLong(HandleRef hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)] public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息 private const int SC_MOVE = 0xF010;//移动信息 private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息 private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息 private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息 private const int SC_MAXIMIZE = 0xF030;//最大化信息 private const int SC_MINIMIZE = 0xF020;//最小化信息
2)定义鼠标事件
//窗体拖动 bool isMouseDown = false; //Point cwpt = new Point(); //当前窗体位置 //Point cmpt = new Point(); //当前鼠标的按下位置 private void sysSet_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = true; //cwpt = this.Location; //cmpt = System.Windows.Forms.Control.MousePosition; } } private void sysSet_MouseUp(object sender, MouseEventArgs e) { //if (WindowState == FormWindowState.Normal) //{ // this.Location = cwpt; // this.pnlMax.BackgroundImage = global::Cnwin.Apps.Properties.Resources.max12; //} //else //{ // this.pnlMax.BackgroundImage = global::Cnwin.Apps.Properties.Resources.min16w; //} isMouseDown = false; } private void sysSet_MouseMove(object sender, MouseEventArgs e) { //int rangeX = 0, rangeY = 0; //计算当前鼠标光标的位移,让窗体进行相同大小的位移 //if (isMouseDown) //{ // Point pt = System.Windows.Forms.Control.MousePosition; // rangeX = cmpt.X - pt.X; // rangeY = cmpt.Y - pt.Y; // this.Location = new Point(cwpt.X - rangeX, cwpt.Y - rangeY); //} //常量 int WM_SYSCOMMAND = 0x0112; //窗体移动 int SC_MOVE = 0xF010; int HTCAPTION = 0x0002; ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); if (WindowState == FormWindowState.Normal && isMouseDown) { this.pnlMax.BackgroundImage = global::Cnwin.Apps.Properties.Resources.max16; } else if (isMouseDown) { this.pnlMax.BackgroundImage = global::Cnwin.Apps.Properties.Resources.max12; } }
以上是关于Winform 最小化 最大化 关闭 点击任务栏隐藏显示 点击鼠标左键移动窗体的主要内容,如果未能解决你的问题,请参考以下文章
Winform 去掉 最大化 最小化 关闭按钮(不是关闭按钮变灰)终极解决办法
C# Winform窗体实现传统右上角按钮功能——最小化最大化正常化关闭