zoukankan      html  css  js  c++  java
  • C# winForm 开机自动启动 并启动后最小化到任务栏 右侧通知栏并交互操作

    //设置自动启动
    string path = Application.StartupPath;
    SettingHel.SetAutoRun(path +@"MostImpressive.DSCJ.DcbInterface.exe", true);

    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(@"SOFTWAREMicrosoftWindowsCurrentVersionRun", true);
                    if (reg == null)
                    {
                        reg = Registry.LocalMachine.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
                    }
                    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程序自动启动

    C# 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属性改为:退出、隐藏和显示。


    三。主要代码:
    1、双击IconForm,即添加Load事件然后

    //一 右击窗体,选择属性,转到事件页面,双击 Load,SizeChanged事件,给窗体添加代码
    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;
    }

    }

    //二 双击窗体上的菜单项,添加相关代码
    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();

    }

    //三 转到窗体设计模式,右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码
    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();
    }

    }

    四。完整的代码如下:


    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 双击可以隐藏和显示切换

    //一 右击窗体,选择属性,转到事件页面,双击 Load,SizeChanged事件,给窗体添加代码
    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;
    }

    }

    //二 双击窗体上的菜单项,添加相关代码
    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();

    }

    //三 转到窗体设计模式,右击notifyIcon1 ,选择属性,双击其中DoubleClick,添加相关代码
    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();
    }

    }
    }
    }

  • 相关阅读:
    kobject.c 添加注释
    DNS之四---实现DNS的转发功能
    DNS之三-----实现DNS的TCP/UDP功能及子域委派
    DNS之二---实现DNS主从复制
    第十二周----chrony时间同步与Cobbler+PXE自动化安装
    网络时间同步服务和chrony
    第十一周----黑客加入黑名单及将普通用户授权root权限
    第十周--IP监控任务脚本实现
    ELK之十三----kibana dashboard(仪表盘)使用
    vmware虚拟机下linux centos6.6只有lo,没有eth0网卡、随机分配ip地址,固定ip地址等问题
  • 原文地址:https://www.cnblogs.com/weixing/p/3384060.html
Copyright © 2011-2022 走看看