Autostart application and open it in taskbar with tray icon - c#

I have an application in .net where I want it to always open without any manual interaction.
In this application I have used NotifyIcon so it always start in Taskbar tray but the notification icon will only show if I manually open that .exe.
so what I did is just added it in Autostart application registry entry with the help of below:
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
So this works fine and on reboot it successfully open it in system taskbar Process list but not as a taskbar tray icon.
Can anyone help me.?

I'm working with NotifyIcon too, and there are some issues with that. First, you need to set an Icon for the NotifyIcon and be sure you didn't set it's Visibility to anything but Visibility.Visible.
Then, NotifyIcon is just a wrapper around the NotifyIcon Windows API, and there's a known issue that it cannot always be created. Therefore, when you initialize the NotifyIcon it might throw an Exception because of an error in Windows (the WinApi returns false if it couldn't be created, and in the source code they throw an Exception there). When it does that, you can just recreate the NotifyIcon in a loop until it can be created.
I also saw an issue sometime when the NotifyIcon was not created in the app.xaml as a XAML object but in code, since then I always create it in XAML instead of in code. Also now I imported the whole NotifyIcon project from CodeProject to be able to debug it's interior. So now I'm creating it in 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 to your needs or you try to recreate the notifyicon when there's an Exception.
I guess this will be the problem because it was the same for us, as sometimes after startup Windows is not yet ready to create the icon. Should there be another issue for you, please post the code you use to create the notifyicon and the system (XP? 64bit?) on which the problem arises.

There was an issue with the way I was using the icon.
There might be an issue with the "icon" file we use in NotifyIcon so I just fixed that issue just by replacing the way
// 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 am able to see the Red icon even after rebooting my computer.

Related

C# Winforms change taskbar icon

I have an old legacy system that uses Winforms and is published with the built in One click,
I look after 3 different installs and each one has its own database it connects to, so its been set up that when a different system is click, it check isCompany1 and will set the Favicon accordingly
so when depoyment is done in the application window the Icon and Manifest is set to the correct ICO, when installed the .exe icon will be what is set there,
However i am trying to bring these all inline as when we debug a drop asks which database we would like to connect to, and depending on that it will set things up differently.
When running in VS the icons in the taskbar will change accoring to the dynamic Favicon however when its depolyed with this new selection it wont change, (it will change once on first load, then wont again)
The issue is some users need access to the different instances so would like different icons at the bottom, but its not changing the task menu, but everything else such as ALT-TAB and the control panel icon is changing,
all forms link into a baseform and call this :
if (App.IsCompany1)
{
this.Icon = new Icon("Resources\\Company1.ico");
}
else if (App.Company2)
{
this.Icon = new Icon("Resources\\Company2.ico");
}
else if (App.Company3)
{
this.Icon = new Icon("Resources\\Company3.ico");
}
AS i said this will change everything but not the Taskbar, but will from VS debugging,
I have made sure the .ico has all sizes, by writing their sizes on each one, and they display correctly the Taskbar and ALT-TAB both use 32x32
If it's any consolation, I couldn't reproduce your complaint.
I put 3 icons in resources, a single button on a form, this code:
private int iconum = 0;
private Icon[] icons = new[] { Properties.Resources.icon1, Properties.Resources.icon2, Properties.Resources.icon3 };
private void button1_Click(object sender, EventArgs e)
{
this.Icon = icons[iconum++ % icons.Length];
}
And it cycled through the icons in the main form title bar and the windows task bar over and over on every button click (made sure to run a release built exe too, not a debug start):
Note: the thing in the top right is my taskbar

Why is my child form not showing a custom icon in the taskbar?

I have a Windows Forms application that uses a custom form to display a progress bar during a long operation. This form is currently set up to display on the taskbar along with the other windows in my program. However, while the other windows of my application show the appropriate application icon, this custom form displays only the default icon:
I set the icon in the designer to the same icon file all of my other windows use, but only this particular form has this problem. The icon shows up correctly in the form's caption as well. Manually setting the icon with form.Icon = Resources.Sample_Icon; in the either the child form's constructor or in the using loop seems to have no effect.
Here's a code sample that reproduces the problem:
// Inside Form1.cs on a button click
using (var form = new SampleForm())
{
form.Show();
for (int i = 0; i < numberOfIterations; ++i)
{
// Do work here in a real program
// This function just calls progressbar.PerformStep():
form.UpdateProgress();
}
}
Changing the Show() to ShowDialog() does show the correct icon, but a DialogResult doesn't really have meaning in this situation.
I suspect that there is some event or message which sets the Windows taskbar icon which never gets processed, but when I tossed in a form.Refresh() and an Application.DoEvents() (just to see if it made a difference), I still had the default icon.
What am I missing? And, why does ShowDialog work properly?
I'm using C# 4.0.
Forms don't inherit their icon from the parent form, even if Show or ShowDialog is called. You should probably try changing the other form's icon explicitly. Either do it in the current code:
form.Icon = <your icon>;
Or, do it in the loading code of your other form, just refer to the form itself.

Hardcodet.Wpf.TaskbarNotification Blank Duplicate system tray icon

When loading my systray icon view xaml which uses Hardcodet.Wpf.TaskbarNotification proxy, a duplicate systray icon renders with a blank icon alongside the correct functional systray icon.
Are there any known bugs with Hardcodet.Wpf.TaskbarNotification that would cause this?
yes sure there is, what actually happend is when you open your app there is icon created but when you close it your app don't remove the icon when he close so...
you just need to open the TaskbarNotification code and make the dispose method public and call it in the Application.Current.Exit event
Application.Current.Exit += new ExitEventHandler(Current_Exit);
void Current_Exit(object sender, ExitEventArgs e)
{
notifyIcon.Dispose();
}

Adding a border (Fixed3D) to an existing borderless application

I have an .NET application that always starts itself with its border style to 'None', as it is supposed to be an full-screen application with a fixed resolution.
However, i would like to add the window border (Fixed3D) to this window when the application starts so that I am able to move it around my desktop.
My first idea was to have a tray app running, monitoring event messages, and somehow change the window style when this specific application starts. However, I am not sure if this will work and how to do this.
Anyone that can point me in the right direction?
You should probably look into using the SetWindowLongPtr function of user32.dll.
You'll need to get the window handle associated with the application's main window. This could potentially be done using:
Process[] processes = Process.GetProcesses();
foreach (Process process in processes) {
if (process.MainModule.FileName == #"C:\Program Files\App\app.exe") {
IntPtr handle = process.MainWindowHandle;
// Call method to change window style here.
break;
}
}
You can then set the appropriate styles using the GWL_STYLE constant (value -16 in decimal) and SetWindowLongPtr.
Changing the Style of the Main Window should help you find the styles that you need.
You can inherit a class from a main class in the source code, and change its properties in the appropriate events. hat would be no modification of the source code, though it would be a different application.

Hide the taskbar using C#

I am running Windows XP 64 bit. I want to hide the taskbar when I run my application.
I tried codes by searching the web. In all those, it hides the task bar. But the problem is, when i open a notepad and maximize it, it is not actually into full screen. Because the space where task bar was there is still blocked with empty space. I want it fit really into full screen mode.
If you like to replace the windows shell (taskbar) you'll have to change a registry key.
Changing the default shell (all users):
open regedit (start menu > run, and type in regedit)
go to: HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon.
Change Shell from explorer.exe to your program path and name e.g. c:\myKioskApp\Kiosk.exe
Changing the default shell (only current user):
open regedit (start menu > run, and type in regedit).
go to: HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon.
add a new string value (Edit > New > String Value) called shell. and set the value to the path of the new shell e.g. c:\myKioskApp\Kiosk.exe
log out and log back in.
I've done this by making the application borderless, maximized, and setting it to be Topmost. Here's a perfect example from CodeProject.
As one of the commenters has said, replacing disabling Explorer and running your application might be the best way, security-wise.
You Can Hide your task bar by setting Following Properties of your C# Form.
WindowState : Maximized
FormBorderStyle : FixedDialog
on window 7 (or maybe higher) using FormWindowState.Maximized is wrong because the maximum size will be subtracted by Taskbar height but you can do this
this.WindowState = FormWindowState.Normal; // or default
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
// do it here
this.Location = new Point(0,0);
var fullscreenSize = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.Size = fullscreenSize;

Categories

Resources