Autostart application and open it on the taskbar with a tray icon

I have an application in .net where I want it to always open without any manual interaction.

In this application, I used NotifyIcon , so it always launches in the taskbar tray, but the notification icon will only be displayed if I manually open this .exe .

so I just added it to the registry entry of the Autostart application using below:

 RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); rkApp.SetValue("MyApp", Application.ExecutablePath.ToString()); 

So, this works great, and upon reboot, it successfully opens it in the system taskbar, but not as an icon in the taskbar.

Can someone help me?

+4
source share
2 answers

There was a problem with the way I used the icon.

There may be a problem with the "icon" file that we use in NotifyIcon , so I just fixed this problem by simply replacing the method

 // START: Creating a red graphic instead of image Bitmap b = new Bitmap(16, 16); Graphics g = Graphics.FromImage(b); g.Clear(Color.Transparent); SolidBrush sb = new SolidBrush(Color.Red); g.FillEllipse(sb, 0, 0, 16, 16); // END: Creating a red graphic instead of image m_notifyicon.Visible = true; m_notifyicon.Icon = Icon.FromHandle(b.GetHicon()); 

Now I can see the red icon even after restarting my computer.

0
source

I work with NotifyIcon too, and there are some problems with this. First, you need to install the icon for NotifyIcon and be sure that you have not installed it. Visibility for anything other than Visibility.Visible.

Then, NotifyIcon is just a shell of the NotifyIcon API, and there is a known issue that it cannot always create. Therefore, when you initialize NotifyIcon , it can throw an exception due to an error in Windows (WinApi returns false if it cannot be created, and in the source code they throw an exception there). When this happens, you can simply recreate NotifyIcon in a loop until it is created.

I also saw a problem once when NotifyIcon was not created in app.xaml as an XAML object, but in code, since I always created it in XAML instead of code. Also now I have imported the entire NotifyIcon project from CodeProject , so that I can debug it inside. So now I create it this way:

  <NotifyIcon1:NotifyIcon x:Key="NotifyIcon" x:Name="notifyicon" ToolTipText="" Visibility="Visible" IconSource="/Images/Icons/bulb.ico"/> 

This should throw an exception if the icon cannot be created in this part of the code in the NotifyIcon library:

 /// <summary> /// Creates the taskbar icon. This message is invoked during initialization, /// if the taskbar is restarted, and whenever the icon is displayed. /// </summary> private void CreateTaskbarIcon() { lock (this) { if (!IsTaskbarIconCreated) { const IconDataMembers members = IconDataMembers.Message | IconDataMembers.Icon | IconDataMembers.Tip; //write initial configuration var status = Util.WriteIconData(ref iconData, NotifyCommand.Add, members); if (!status) { throw new Win32Exception("Could not create icon data"); } //set to most recent version SetVersion(); messageSink.Version = (NotifyIconVersion) iconData.VersionOrTimeout; IsTaskbarIconCreated = true; } } } 

Either you directly edit the code according to your needs, or try to recreate notifyicon when there is an Exception.

I suppose this will be a problem because it was the same for us, because sometimes after starting Windows, it’s not yet ready to create an icon. If you have another problem, send the code that you use to create notifyicon and the system (XP? 64bit?) On which the problem occurs.

+3
source

Source: https://habr.com/ru/post/1496149/


All Articles