如何在c#启动时初始化notifyicon [英] How to initialize the notifyicon at startup in c#

查看:110
本文介绍了如何在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]

推荐答案

再次恭喜JParis,



......那么你能做什么? />


现在我们可以谈谈(Windows-)服务等等,但我认为最简单的就是在计算机启动时启动应用程序(自动启动,注册表项)并让它在后台运行(仅通过NotifyIcon对用户可见)。



只需创建一个应用程序 - 让一个部分触发提醒(观看事件)和另一个部分显示提醒(BaloonTips)。



如果您的应用程序最小化,请使用NotifyIcon而不是任务栏。

Windows启动时自动启动应用程序(可以使用AutoStart或为您的应用设置运行注册表项。



也许你想要使用以下(runnable)示例播放arround,显示典型的NotifyIcon抵抗和最小化szenario。我还添加了一些代码来读取和写入注册表Autostart条目 - 您必须有权访问注册表(最好以管理员身份运行)!否则写入注册表将失败!所以剩下的就是为NotifyIcon添加你自己的Icon而不是我用来测试示例项目的那个。



Hi again JParis,

... So what can you do?

Now we can talk about (Windows-)Services and so on, but I think easiest for you is just to start your app when the Computer starts (Autostart, registry entry) and let it run in the background (only visible for the user through the NotifyIcon).

Just create one app - let one "part" trigger the reminders (watch "events") and the other "part" showing the reminders (BaloonTips).

Use the NotifyIcon instead of the taskbar if your app is minimized.
Autostart your app when Windows starts (could use "AutoStart" or setting the "Run" registry key for your app.

Maybe you want to play arround with the following (runnable) example showing a typical NotifyIcon resisizing and minimizing szenario. I also added some code to read and write the registry "Autostart" entry - You must have rights to access registry (best run as admin)! Otherwise writing to the registry will fail! So whats left is to add your own Icon for the NotifyIcon instead of the one I used to test the example project.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆