I'm trying to set the icon of my application (visible in the taskbar). The icon is correct when I run the .exe itself or run it from visual studio, but this doesn't apply when starting from a shortcut.. The icon inside the application itself, top left corner, is correct.
Code used to set the icon:
var assemblyDirectory = Directory.GetCurrentDirectory();
var iconUri = new Uri(Path.Combine(assemblyDirectory, resourceName), UriKind.Absolute);
Icon= BitmapFrame.Create(iconUri);
I'm trying to add a red circle indicating changes in my application to the users, which is done by changing between 2 icons.
Any idea on how to set the icon of a shortcut at runtime, or about how to show a red circle in the taskbar-icon indicating changes?
Although it is impossible to set the assembly icon during runtime (it is read-only), you can use NotifyIcon to display an icon in system tray and that is fairly easy to change during runtime.
Here is an example of how to implement a NotifyIcon:
https://www.codeproject.com/Tips/627796/Doing-a-NotifyIcon-program-the-right-way
Note: Since you are using WPF, you need to include a reference to Windows.Forms in your application.
Edit: just in case the link goes dead, here some code example for Program.cs, needs an "icon" imported as resource:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyCustomApplicationContext());
}
public class MyCustomApplicationContext : ApplicationContext
{
public static NotifyIcon trayIcon;
public MyCustomApplicationContext()
{
trayIcon = new NotifyIcon()
{
Icon = icon,
ContextMenu = new ContextMenu(new MenuItem[]
{
new MenuItem("Exit", Exit),
new MenuItem("Do something", DoSomething),
}),
Visible = true,
BalloonTipText = "My Application",
BalloonTipTitle = "My Application",
Text = "My Application Text"
};
trayIcon.Click += new System.EventHandler(trayIcon_Click);
}
private void trayIcon_Click(object sender, System.EventArgs e)
{
//Do something
}
void DoSomething(object sender, EventArgs e)
{
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Application.Exit();
}
}
Related
OK I am a total newbie at WPF but I have to develop what's in the title with wpf but not relying on MVVM.
I have followed this:
WPF Application that only has a tray icon
I found the first answer the one with the hardcodet lib but find it too difficult and MVVM biased.
So followed the second one and everything seemed fine except that in the end it doesn't work.
So in my App.xaml.cs I have put:
public partial class App : Application
{
private System.Windows.Forms.NotifyIcon notifyIcon = null;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
UnityCore.Initialize();
}
protected override void OnActivated(EventArgs e)
{
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Click += NotifyIcon_Click;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/Images/ITA.png")).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
notifyIcon.Visible = true;
base.OnActivated(e);
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
Console.Beep();//show main window
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
Console.Beep();//show main window
}
}
The main window at start is transparent of minimized to make it invisible.
At this stage I hoped to see the ITA flag in the tray icon and then at click or double click restore the main window.
But I don't see a damn thing in the tray.
I think that the flag resource is properly set here's my solution
Thanx for any help.
Firstly, I have zero experience programming with .NET so this could be a pretty nooby question...
I'm wondering if it's possible to build an application like xFire - which displays pop-ups in-game to a user.
As the primary application will have focus, how does my application manage to display it's message/popup? Does the primary application have to allow access or something?
Cheers!
Looks like you are looking for NotifyIcon, it displays little pop-ups just as know from Windows.
The example is a little long, but the documentation will surely help you understand.
using System;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public Form1()
{
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.menuItem1});
// Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
// Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Text = "Notify Icon Example";
// Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("appicon.ico");
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (NotifyIcon example)";
notifyIcon1.Visible = true;
// Handle the DoubleClick event to activate the form.
notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
}
protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose();
base.Dispose( disposing );
}
private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon.
// Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal;
// Activate the form.
this.Activate();
}
private void menuItem1_Click(object Sender, EventArgs e) {
// Close the form, which closes the application.
this.Close();
}
}
See MSDN.
"As the primary application will have focus, how does my application manage to display it's message/popup? Does the primary application have to allow access or something?"
An application can set itself as "TopMost" which means it appears in front of other "normal" applications that are not TopMost. A well behaved notification will appear without stealing focus from the current application (this is usually achieved with the ShowWindow() API and the SW_SHOWNA flag). This does not require any permission from the currently active application.
Take a look at TaskbarNotifier, a skinnable MSN Messenger-like popup in C# and now in VB.NET too.
I am trying to show icon on the taskbar, well i did this in this way.
ResourceManager resManager = new ResourceManager("TestAgent.Properties.Resources", GetType().Module.Assembly);
notifyicon.Icon = (Icon)resManager.GetObject("TestAgent");
notifyicon.Visible = true;
notifyicon.Text = "Test Agent";
this.Hide();
this.ShowInTaskbar = false;
this.SetVisibleCore(false);
On other side when try remove icon from the taskbar doing in this way.
notifyicon.Visible = false;
notifyicon = null;
rulehandler = null;
I did this successfully but the problem is when try to remove icon from the taskbar it remove icon successfully from the taskbar but not hide the icon, When hover the mouse on the icon it removes.
Is there anyway to remove icon without mouse hover?
I am doing this in windows form with c#
Simply Dispose it.
In a Windows Form you can subscribe to the global event ApplicationExit ...
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
private void OnApplicationExit(object sender, EventArgs e) {
notifyicon.Dispose();
}
simply write notifyIcon.Visible = false; (capital I in Icon) before closing the window, and you are good to go.
As simple as that.
how to make C# program that will be All time in the taskbar ?
i want to Build a Keyboard program.
i need that when i open the device the program will open and be in the taskbar.
another question is, when i have an external program that has a Textbox, how
to make that when i press any key in my C# keyboard it will be in this external Textbox ?
thank's in advance
It's not implemented in the CF, but the NotifyIcon class is what you're after. The SDF does implement it. It would be used something like this:
m_notifyIcon = new NotifyIcon();
m_notifyIcon.Icon = this.Icon;
m_notifyIcon.Visible = true;
m_notifyIcon.Click += new EventHandler(m_notifyIcon_Click);
m_notifyIcon.DoubleClick += new EventHandler(m_notifyIcon_DoubleClick);
EDIT
If you want to implement this yourself, the place to start is with the Shell_NotifyIcon API. You'll want to pass it the handle to a MessageWindow class and handle the WM_NOTIFY messages.
To Create a system Tray application in Windows-CE, put some code like:
CSystemTray m_TrayIcon; // Member variable of some class
...
// in some member function maybe...
m_TrayIcon.Create(pParentWnd, WM_MY_NOTIFY, "Click here",
hIcon, nTrayIconID);
Eg. For a non-MFC tray icon, do the following:
Collapse
CSystemTray m_TrayIcon; // Member variable of some class
...
// in some member function maybe...
m_TrayIcon.Create(hInstance, NULL, WM_MY_NOTIFY,
"Click here", hIcon, nID);
// Send all menu messages to hMyMainWindow
m_TrayIcon.SetTargetWnd(hMyMainWindow);
As found here:
http://www.codeproject.com/KB/shell/systemtray.aspx
To create a system tray application in Windows XP or Windows 7/Vista, put some code like this in your project:
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
and this to handle the system tray click
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
This and more information found at :
http://www.developer.com/net/net/article.php/3336751/C-Tip-Placing-Your-C-Application-in-the-System-Tray.htm
I mean when the user starts my application(exe). I want it to start directly in system tray, without showing the window. Like antivirus softwares & download managers, which start and run in the system tray silently.
I want the same effect and when user click the "show" button of the notifyIcon's contextmenustrip then only application should show GUI.
I'm using this, but its not working
private void Form_Load(object sender, EventArgs e)
{
this.Hide();
}
May be I need to have Main() function in some other class which has no GUI but has notifyIcon & ContextMenuStrip whose option will instantiate the GUI window class. Right?
The way I usually setup something like this is to modify the Program.cs to look something like the following:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (NotifyIcon icon = new NotifyIcon())
{
icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
icon.ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Show form", (s, e) => {new Form1().Show();}),
new MenuItem("Exit", (s, e) => { Application.Exit(); }),
});
icon.Visible = true;
Application.Run();
icon.Visible = false;
}
}
Using this, you don't need to worry about hiding instead of closing forms and all the rest of the hacks that can lead to... You can make a singleton form too instead of instantiating a new Form every time you click the show form option. This is something to build off of, not the end solution.
You need to set up the notify icon as well.
Either manually or via the toolbar (drag a notifyIcon onto your form) create the notifyIcon:
this.notifyIcon = new System.Windows.Forms.NotifyIcon(components);
Then add this code to Form_Load():
// Notify icon set up
notifyIcon.Visible = true;
notifyIcon.Text = "Tooltip message here";
this.ShowInTaskbar = false;
this.Hide();
Though this will, as has been pointed out, briefly show the form before hiding it.
From the accepted answer of this question, the solution appears to be to change:
Application.Run(new Form1());
to:
Form1 f = new Form1();
Application.Run();
in Main().
Have you created a Windows Application in C#?
You need to drag a NotifyIcon control onto the form, the control will be placed below the form because it has no visual representation on the form itself.
Then you set its properties such as the Icon...
Try this one first...