zoukankan      html  css  js  c++  java
  • WinForm:最小化到托盘

    ①在MainForm上托2个控件NotifyIcon和ContextMenuStrip。
    ②NotifyIcon:用于显示托盘。
    a)先Choose Icon,作为托盘显示的图标;
    b)设置属性ContextMenuStrip,关联右击后显示的菜单。
    c)追加双击事件:

            private void mainNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                if (this.Visible)
                {
                    this.WindowState = FormWindowState.Minimized;
                    this.mainNotifyIcon.Visible = true;
                    this.Hide();
                }
                else
                {
                    this.Visible = true;
                    this.WindowState = FormWindowState.Normal;
                    this.Activate();
                }
    
            }


    ③ContextMenuStrip:右击托盘显示的菜单。
    a)追加3个Item,最大化,最小化,关闭。
    b)追加最大化,最小化,关闭的处理代码。

            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Minimized;
                this.mainNotifyIcon.Visible = true;
                this.Hide();
            }
    
            private void toolStripMenuItem2_Click(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Maximized;
                this.mainNotifyIcon.Visible = true;
                this.Show();
            }
    
            private void toolStripMenuItem3_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("Do you want to quit?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    this.mainNotifyIcon.Visible = false;
                    this.Close();
                    this.Dispose();
                    System.Environment.Exit(System.Environment.ExitCode);
                }
            }


    ③重写主窗口的FormClosing方法,使Main上点关闭时,最小化到托盘。

            private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    e.Cancel = true;
    
                    this.WindowState = FormWindowState.Minimized;
                    this.mainNotifyIcon.Visible = true;
                    this.Hide();
                    return;
                }
            }
  • 相关阅读:
    学习规划
    一位十年的老司机告诉你什么是编程思想
    React开发
    一个简单的ipfs音乐播放器的实现
    React错误总结(三)
    React错误总结解决方案(二)
    mongoid模糊查询
    Rails accepts_nested_attributes_for表单嵌套的利器
    route_path
    "constantize" and "with_indifferent_access" method
  • 原文地址:https://www.cnblogs.com/wyvern0618/p/9723753.html
Copyright © 2011-2022 走看看