c# 窗体相关操作(最大化/最小化/关闭/标题栏)
/// <summary> /// 窗体跟随鼠标移动的标记 /// </summary> private bool normalmoving = false; /// <summary> /// 最大化窗体跟随鼠标Normal和移动的标记 /// </summary> private bool maxmoving = true; /// <summary> /// 临时存储上一次鼠标位置 /// </summary> private Point oldMousePosition; /// <summary> /// 右侧最大化/最小化/关闭按钮的总宽度 /// </summary> int ritghtbuttonslength = 180;
关闭按钮相关操作
#region 页面关闭的相关操作 private void picBoxMainClose_Click(object sender, EventArgs e) { this.Close(); } private void picBoxMainClose_MouseEnter(object sender, EventArgs e) { this.picBoxMainClose.Image =global::LocDevelopDesClient.Properties.Resources.close_mainMouse; } private void picBoxMainClose_MouseLeave(object sender, EventArgs e) { this.picBoxMainClose.Image = global::LocDevelopDesClient.Properties.Resources.close_main; } #endregion
窗体最大化相关操作
#region 窗体最大化的相关操作 //包括标题栏双击窗口的变化及页面最大化图表点击 private void picBoxMaxSize_Click(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } else if (this.WindowState == FormWindowState.Normal) { this.MaximumSize = new Size(Screen.FromControl(this).WorkingArea.Width, Screen.FromControl(this).WorkingArea.Height); this.WindowState = FormWindowState.Maximized; } } private void picBoxMaxSize_MouseEnter(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.picBoxMaxSize.Image = global::LocDevelopDesClient.Properties.Resources.maxSize_mouse; } else if (this.WindowState == FormWindowState.Maximized) { this.picBoxMaxSize.Image = global::LocDevelopDesClient.Properties.Resources.normal_mouse; } } private void picBoxMaxSize_MouseLeave(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { this.picBoxMaxSize.Image = global::LocDevelopDesClient.Properties.Resources.maxSize; } else if (this.WindowState == FormWindowState.Maximized) { this.picBoxMaxSize.Image = global::LocDevelopDesClient.Properties.Resources.normal; } } #endregion
窗体最小化相关操作
#region 窗体最小化的相关操作 private void picBoxMinSize_Click(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Minimized; } } private void picBoxMinSize_MouseEnter(object sender, EventArgs e) { this.picBoxMinSize.Image = global::LocDevelopDesClient.Properties.Resources.minsize_mouse; } private void picBoxMinSize_MouseLeave(object sender, EventArgs e) { this.picBoxMinSize.Image = global::LocDevelopDesClient.Properties.Resources.minsize1; } #endregion
标题栏相关操作
#region 主页面标题栏的相关操作 private void palTitle_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { oldMousePosition = new Point(-e.X, -e.Y); normalmoving = true; } } private void palTitle_MouseUp(object sender, MouseEventArgs e) { maxmoving = true; //pnlFirstMenu.Cursor = Cursors.Default; if (normalmoving) { normalmoving = false; } }
private void palTitle_MouseMove(object sender, MouseEventArgs e) { //如果leftlag为true则进行移动 if (!normalmoving) return; if (this.WindowState == FormWindowState.Maximized && e.Button == MouseButtons.Left && maxmoving) { this.WindowState = FormWindowState.Normal; //当窗体最大时,鼠标点击的位置比例 double bili = e.X * 1.0 / Screen.PrimaryScreen.Bounds.Width; //如果点击位置太靠右,则窗体normal后,鼠标会在右侧按钮上方 //计算缩小后数据离右侧的距离 double rightlength = (1.0 - bili) * this.Width; int x = 0; if (rightlength < ritghtbuttonslength) { x = (int)(e.X - (bili * this.Width) + ritghtbuttonslength - rightlength); //更改oldMousePosition的位置 oldMousePosition.X += (int)(e.X - (bili * this.Width) + ritghtbuttonslength - rightlength); } else { x = (int)(e.X - (bili * this.Width)); //更改oldMousePosition的位置 oldMousePosition.X += (int)(e.X - (bili * this.Width)); } this.Location = new Point(x, 0); } else if (e.Button == MouseButtons.Left) { { Point mouseSet = MousePosition; mouseSet.Offset(oldMousePosition.X, oldMousePosition.Y); //设置移动后的位置 Location = mouseSet; if (MousePosition.Y <= 0) { this.MaximumSize = new Size(Screen.FromControl(this).WorkingArea.Width, Screen.FromControl(this).WorkingArea.Height); this.WindowState = FormWindowState.Maximized; maxmoving = false; } } } } #endregion