WINFROM 去除自带的最大最小关闭按钮 窗体移动 最小化托盘

//创建NotifyIcon对象 
        NotifyIcon notifyicon = new NotifyIcon();
        //创建托盘图标对象 
        Icon ico;
        //创建托盘菜单对象 
        ContextMenu notifyContextMenu = new ContextMenu();
        #region 最小化方法
        private void MinWinform()
        {
            //直接最小化
            this.WindowState = FormWindowState.Minimized;
            //托盘显示
            notifyIcon_ico.Icon = ico;
            //隐藏任务栏区图标 
            this.ShowInTaskbar = false;
            //图标显示在托盘区 
            notifyicon.Visible = true;
        }
        #endregion

    #region 关闭窗口方法
    public virtual void CloseWinform()
    {
        DialogResult result = MessageBox.Show("是否退出?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
        {
            this.Dispose();
            Application.Exit();
        }
    }

    #endregion

    #region 托盘双击事件
    private void icoDoubleClick()
    {
        //判断是否已经最小化于托盘 
        if (WindowState == FormWindowState.Minimized)
        {
            //还原窗体显示 
            WindowState = FormWindowState.Normal;
            //激活窗体并给予它焦点 
            this.Activate();
            //任务栏区显示图标 
            this.ShowInTaskbar = true;
            //托盘区图标隐藏 
            notifyicon.Visible = false;
        }
    }
    #endregion

    #region 最大化窗体
    private void MaxWinform()
    {
        if (this.WindowState == FormWindowState.Maximized)
        {
            this.WindowState = FormWindowState.Normal;
        }
        else
        {
            this.WindowState = FormWindowState.Maximized;
        }
    }
    #endregion

    #region 移动窗体
    //定义无边框窗体Form
    [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 gPanelTitleBack_MouseDown(object sender, MouseEventArgs e)
    {
        ReleaseCapture();
        SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数
    }
	#region 上边框单击移动 双击最大化
    private void conter_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Clicks == 1)//单击
        {
            gPanelTitleBack_MouseDown(sender, e);
        }
        else if (e.Clicks == 2)//双击
        {
            MaxWinform();
        }
	  #endregion
    }

    #endregion

    #region 操作按钮 

    #region 最小化按钮
    private void min_MouseClick(object sender, MouseEventArgs e)
    {
        MinWinform();
    }
    #endregion

    #region 最大化按钮
    private void max_MouseClick(object sender, MouseEventArgs e)
    {
        MaxWinform();
    }
    #endregion

    #region 关闭按钮
    private void close_MouseClick(object sender, MouseEventArgs e)
    {
        CloseWinform();
    }
    #endregion

    #region 托盘双击事件
    private void notifyIcon_ico_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        icoDoubleClick();
    }
    #endregion


    #endregion

    private void fm_BaseForm_Load(object sender, EventArgs e)
    {
        if (!DesignMode)
        {
            ico = new Icon(“ico路径”);
            this.Icon = ico;
        }
    }

隐藏系统自带最大化最小化关闭 三个按钮 并重画三个钮
实现了,最小话进托盘 、双击托盘闪现窗体 、无边框移动、子页可继承等功能

上一篇:C# 模板类、接口


下一篇:C# 实现 Winform 切换用户功能