using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Windows.Forms; namespace UAS_LabelMachine.CustomControl { public partial class HeadBar : UserControl { //需要关闭前提示窗口的在这里加 private List NeedConfirm = new List() {"Main"}; [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; //定义委托 public delegate void OnHeadBarClick(object sender, EventArgs e); //定义事件 public event OnHeadBarClick HeadBarDoubleClick; public HeadBar() { InitializeComponent(); } /// /// 用于对窗体的关闭,判断主窗体的时候退出程序,非主窗体时关闭该窗体,同时回收内存 /// /// /// public void CloseWindow_Click(object sender, EventArgs e) { if (this.ParentForm.WindowState == FormWindowState.Normal || this.ParentForm.WindowState == FormWindowState.Maximized) { this.ParentForm.WindowState = FormWindowState.Minimized; } } /// /// 最小化父级窗体 /// /// /// private void MinWindow_Click(object sender, EventArgs e) { //判断是否需要关闭前的提示 if (NeedConfirm.Contains(this.FindForm().Name)) { string logout_confirm = MessageBox.Show(this.ParentForm, "是否关闭", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (logout_confirm == "Yes") { this.FindForm().Close(); } } else { this.FindForm().Close(); } } private void panel1_Click(object sender, EventArgs e) { HeadBarDoubleClick?.Invoke(sender, new EventArgs()); } } }