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

一、开机自动启动:

拖一个CheckBox

1、软件启动时给CheckBox重置状态:

C#代码

  RegistryKey r_local = Registry.LocalMachine;
            RegistryKey r_run = r_local.CreateSubKey((@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run"));
            if (r_run.GetValue("CHINAMOBILE") == null)
            {
                cbWindowStart.Checked = false;
            }
            else
            {
                cbWindowStart.Checked = true;
            }
            r_run.Close();
            r_local.Close();

2、CheckChanged事件:

C#代码
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string R_startPath = Application.ExecutablePath;
            if (checkBox1.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;
                }
              
            }
        }
二、最小化到系统托盘

拖一个NotifyIcon

设置form的showInTastbar属性为false

C#代码

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }
        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible = false;
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 C# WinForm 开机自动启动并运行代码界面,需要进行以下步骤: 1. 在你的 WinForm 项目中,添加一个启动器类,用于在开机启动你的应用程序。 2. 在启动器类中,使用 Microsoft.Win32 命名空间下的 Registry 类来注册开机启动项。 3. 在注册开机启动项时,需要设置项的名称、路径和参数,其中路径应该是你的应用程序的可执行文件路径。 4. 在注册完开机启动项后,可以通过 Process 类来启动你的应用程序,以确保它能够在开机自动运行并显示界面。 下面是一个简单的示例代码: ```csharp using Microsoft.Win32; using System.Diagnostics; using System.Windows.Forms; namespace MyWinFormApp { public static class Startup { public static void RegisterStartup() { RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (registryKey.GetValue("MyWinFormApp") == null) { registryKey.SetValue("MyWinFormApp", Application.ExecutablePath); } Process.Start(Application.ExecutablePath); } } } ``` 你可以在程序的 Main 函数中调用这个 RegisterStartup 方法,来注册开机启动项并启动你的应用程序。例如: ```csharp static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Startup.RegisterStartup(); Application.Run(new MainForm()); } ``` 这样,当用户登录到 Windows 系统后,你的应用程序就会自动启动并显示界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值