C# 实现winform软件最小化到系统托盘,开机自启动

本文介绍了如何使用C#编程语言将WinForm应用程序设置为最小化到系统托盘,并详细讲解了notifyIcon组件的配置,包括双击弹出软件的处理以及contextMenuStrip的设置。此外,还涵盖了如何让软件实现开机自启动的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:https://www.cnblogs.com/GT_Andy/archive/2010/03/16/1921836.html

1.最小化到系统托盘

添加notifyIcon和contextMenuStrip
在这里插入图片描述

        private void Form1_Load(object sender, EventArgs e)
        { 
            WindowState = FormWindowState.Minimized; //最小化到系统托盘
        }      
        //最小化
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized) //最小化到系统托盘
            {
                Visible = false;
                notifyIcon1.Visible = true;
            }
        }

1.1.notifyIcon 设置:

在这里插入图片描述
在这里插入图片描述

1.2.NotifyIcon 双击弹出软件

        //NotifyIcon 双击弹出软件 
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetF();          //获得本窗体的句柄
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetF(IntPtr hWnd); //设置此窗体为活动窗体
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Visible = true;
            WindowState = FormWindowState.Normal;
            Show(); 
            if (this.Handle != GetF()) //如果本窗口没有获得焦点
                SetF(this.Handle); //设置本窗口获得焦点
        }

1.3.contextMenuStrip 设置:

在这里插入图片描述

       //右键小图标菜单 - 退出
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

2.开机自启动

//开机启动
        private void checkBoxReboot_CheckedChanged(object sender, EventArgs e)
        {
            string R_startPath = Application.ExecutablePath;
            if (checkBoxReboot.Checked == true)
            {
                RegistryKey R_local = Registry.LocalMachine;
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.SetValue("BirthdayTipF", R_startPath);
                R_run.Close();
                R_local.Close();
            }
            else
            {
                try
                {
                    RegistryKey R_local = Registry.LocalMachine;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue("BirthdayTipF", false);
                    R_run.Close();
                    R_local.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("您需要管理员权限修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw;
                }

            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值