C# winForm 开机自动启动 并启动后最小化到任务栏 右侧通知栏并交互操作

  1. 开机自启
///开机自启动
 class Program
    {
        static void Main(string[] args)
        {
            string path = Application.StartupPath;
            SettingHel.SetAutoRun(path +@"\IISCheck.exe", true);

        }catch (WebException ex)
        {
           Console.WriteLine(ex.ToString());
        }
   }

public static class SettingHel
    {
        /// <summary>
        /// 设置应用程序开机自动运行
        /// </summary>
        /// <param name="fileName">应用程序的文件名</param>
        /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>
        /// <exception cref="system.Exception">设置不成功时抛出异常</exception>
        /// <returns>返回1成功,非1不成功</returns>
        public static String SetAutoRun(string fileName, bool isAutoRun)
        {
            string reSet = string.Empty;
            RegistryKey reg = null;
            try
            {
                if (!System.IO.File.Exists(fileName))
                {
                    reSet = "该文件不存在!";
                }
                string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                {
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                }
                if (isAutoRun)
                {
                    reg.SetValue(name, fileName);
                    reSet = "1";
                }
                else
                {
                    reg.SetValue(name, false);
                }

            }
            catch (Exception ex)
            {
                  //“记录异常”
            }
            finally
            {
                if (reg!=null)
                {
                    reg.Close();
                }
            }
            return reSet;
        }

    }

设置.exe程序自动启动

  1. winForm启动最小化到任务栏右侧通知栏并交互操作
    一、主要功能:
    (1)、程序启动自动隐藏到任务栏右侧通知栏显示。(与系统托盘同义)
    (2)、双击系统托盘图标显示、隐藏窗口;
    (3)、右击系统托盘图标提供三个菜单选项,“退出”、“隐藏”、“显示”;

二、相关控件:
1、建一个WinForm程序—IconForm,将Form属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示。
2、将Form属性WindowState选择为 Minimized,以便起来自动最小化隐藏
3、在工具栏中的“公共控件”里,拖入NotifyIcon控件—notifyIcon1,这个是程序运行任务栏右侧通知区域图标显示控件,为控件notifyIcon的属性Icon添加一个icon图标,或从代码中加入。
4、在工具栏中的“菜单和工具栏”里,拖入ContextMenuStrip—contextMenuStrip1,这个控件是右击时关联菜单。
5、右键notifyIcon1选择属性,将其属性ContextMenuStrip改加为contextMenuStrip1,这个时候notifyIcon1和contextMenuStrip1两个控件就关联了。
6、右键contextMenuStrip1,选择属性,进入Items,然后点击“添加”,这里添加三个菜单选项:exitMenuItem、hideMenuItem、showMenuItem,同时分别将其Text属性改为:退出、隐藏和显示。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IconForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //说明,程序运行后自动隐藏到任务栏右侧的通知栏里,
        //1 右击选择退出,隐藏,显示
        //2 双击可以隐藏和显示切换

        /// <summary>
        /// 一 右击窗体,选择属性,转到事件页面,双击 Load,SizeChanged事件,给窗体添加代码
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            //1.将Form属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示。
            //2.将Form属性WindowState选择为 Minimized,以便起来自动最小化隐藏。
            string startup = Application.ExecutablePath; //取得程序路径
            int pp = startup.LastIndexOf("\\");
            startup = startup.Substring(0, pp);
            string icon = startup + "\\testIcon.ico";
            //3.一定为notifyIcon1其设置图标,否则无法显示在通知栏。或者在其属性中设置
            notifyIcon1.Icon = new Icon(icon);

        }

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

        }

        /// <summary>
        /// 二 双击窗体上的菜单项,添加相关代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }

        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();

        }
        /// <summary>
        /// 三 转到窗体设计模式,右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码
        /// </summary>
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }

        }
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值