如何在c#启动时初始化notifyicon

如何在c#启动时初始化notifyicon

本文介绍了如何在c#启动时初始化notifyicon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在使用C#进行项目,其中我正在创建日历提醒类别。

我有编码我所在的部分作为输入在sql数据库中记住的各种日期。

我想要的是......应用程序应设置警报并且特定事件应该弹出或显示在使用notifyicon的通知托盘的气球中。

Hi Everyone

I am working on a project in C# wherein I am creating a calendar- reminder sort of.
I have coded the part where I am taking in as input the various dates to be remembered in an sql database.
What I want is.. the application should set an alarm and the particular event should pop up or show in a balloon from the notification tray using notifyicon.

notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip();



也请任何人告诉我如何在启动时仅初始化notifyicon(如何将其十六进制代码放在regedit中)在启动时初始化整个项目。



谢谢



已添加代码块[/编辑]


Also please can anyone tell me how to initialize only the notifyicon at startup (How to place its hex codes in regedit)instead of initializing the entire project at startup.

Thank You

Code block added[/Edit]

推荐答案

using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace NotifyIcon_AutoStartSample
{
    static class Program
    {
        class SampleForm : Form
        {
            NotifyIcon m_notifyicon;

            ContextMenuStrip m_ctxmenuNotify;
            ToolStripMenuItem m_miNotifyExit;
            ToolStripMenuItem m_miNotifyRestore;

            CheckBox m_cbShowInTaskbarWhenMinimized; // in your real app you have to persist this setting (ApplicationSettings?).
            CheckBox m_cbAutoRunWithWindows;

            public SampleForm()
            {
                // Create Menu for NotifyIcon
                m_ctxmenuNotify = new ContextMenuStrip();
                m_ctxmenuNotify.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuNotifyOpening);
                m_miNotifyExit = new ToolStripMenuItem("Exit");
                m_miNotifyExit.Click += new EventHandler(MenuNotifyExitOnClick);
                m_miNotifyRestore = new ToolStripMenuItem("Restore");
                m_miNotifyRestore.Click += new EventHandler(MenuNotifyRestoreOnClick);
                m_ctxmenuNotify.Items.Add(m_miNotifyExit);
                m_ctxmenuNotify.Items.Add(m_miNotifyRestore);

                // Create a typical menu for a NotifyIcon
                m_notifyicon = new NotifyIcon();
                m_notifyicon.Icon = NotifyIcon_AutoStartSample.Properties.Resources.Icon1; // Replace with your own icon!
                m_notifyicon.Text = Text;
                m_notifyicon.Visible = true;
                m_notifyicon.Click += new EventHandler(NotifyIconOnClick);
                m_notifyicon.ContextMenuStrip = m_ctxmenuNotify;

                // A CheckBox for selecting if app should be shown in the taskbar
                m_cbShowInTaskbarWhenMinimized = new CheckBox();
                m_cbShowInTaskbarWhenMinimized.Text = "Show in Taskbar when minimized";
                m_cbShowInTaskbarWhenMinimized.Dock = DockStyle.Top;

                 // A CheckBox for auto run
                m_cbAutoRunWithWindows = new CheckBox();
                m_cbAutoRunWithWindows.Text = "AutoStart with windows";
                m_cbAutoRunWithWindows.Dock = DockStyle.Top;
                m_cbAutoRunWithWindows.Checked = IsAutoRunWithWindowsEnabled();
                m_cbAutoRunWithWindows.CheckedChanged += new EventHandler(m_cbAutoRunWithWindows_CheckedChanged);

                Controls.Add(m_cbShowInTaskbarWhenMinimized);
                Controls.Add(m_cbAutoRunWithWindows);
            }

            void ContextMenuNotifyOpening(object obj, System.ComponentModel.CancelEventArgs cea)
            {
                if (WindowState == FormWindowState.Minimized)
                    m_miNotifyRestore.Text = "Restore";
                else
                    m_miNotifyRestore.Text = "Minimize";
            }

            void MenuNotifyRestoreOnClick(object obj, EventArgs ea)
            {
                if (WindowState == FormWindowState.Minimized)
                {
                    Visible = true;
                    WindowState = FormWindowState.Normal;
                    Activate();
                }
                else
                {
                    WindowState = FormWindowState.Minimized;
                }
            }

            void NotifyIconOnClick(object obj, EventArgs ea)
            {
                MouseEventArgs mea = ea as MouseEventArgs;

                if (mea.Button != MouseButtons.Left)
                    return;

                if (WindowState == FormWindowState.Minimized)
                {
                    Visible = true;
                    WindowState = FormWindowState.Normal;
                }
                Activate();
            }

            void MenuNotifyExitOnClick(object obj, EventArgs ea)
            {
                Close();
            }

            protected override void OnResize(EventArgs ea)
            {
                if (!m_cbShowInTaskbarWhenMinimized.Checked)
                {
                    if (WindowState == FormWindowState.Minimized)
                        Visible = false;
                }

                base.OnResize(ea);
            }

            protected override void OnClosed(EventArgs ea)
            {
                base.OnClosed(ea);

                // Dispose NotifyIcon if this Form closes. (not needed if you add your NotifyIcon with the FormsDesigner)
                if (m_notifyicon != null)
                    m_notifyicon.Dispose();
            }

            void m_cbAutoRunWithWindows_CheckedChanged(object sender, EventArgs e)
            {
                SetAutoRunWithWithWindows(m_cbAutoRunWithWindows.Checked, String.Empty);
            }

            static public bool SetAutoRunWithWithWindows(bool bEnable, string strArgs)
            {
                RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (regkey == null)
                    return false;

                string strPath = Application.ExecutablePath + (String.IsNullOrEmpty(strArgs) ? String.Empty : " " + strArgs);
                if (bEnable)
                {
                    regkey.SetValue(Path.GetFileNameWithoutExtension(Application.ExecutablePath), strPath, RegistryValueKind.String);
                }
                else
                {
                    regkey.DeleteValue(Path.GetFileNameWithoutExtension(Application.ExecutablePath), false);
                }

                return true;
            }

            static public bool IsAutoRunWithWindowsEnabled()
            {
                RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (regkey == null)
                    return false;

                if (!String.IsNullOrEmpty((string)regkey.GetValue(Path.GetFileNameWithoutExtension(Application.ExecutablePath))))
                    return true;

                return false;
            }

        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new SampleForm());
        }

    }
}





我希望这可以帮助你决定什么为你的应用程序做的 - 随时问你是否有任何其他问题。



亲切的问候Johannes



I hope this helps you in deciding what to do for your app - feel free to ask if you have any further questions.

Kind regards Johannes


这篇关于如何在c#启动时初始化notifyicon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:30