|
|
Comments and Discussions
|
|
 |

|
It s so helpfull in creating icons
|
|
|
|

|
how to get this icon for all users to let a program run when you start under admin and managing it could and normal user?
|
|
|
|

|
i am working on a application which has timers set up in it as
System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, autoEvent, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 0, 0, -1));
now when i am trying to support this application in task bar, using notifyIcons --- whenever i try to close the app, the icon doesn't go away and the application keeps on running in task manager, untill i again click on icon (just clicking on icon serves, no need to select any option)
Actually the timers are running as below, cause i want them to run continuosly with doing some calculation every 12 hours
private void Form1_Load(object sender, EventArgs e)
{
while (true)
{
if (x == 0)
{
System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, autoEvent, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 0, 0, -1));
autoEvent.WaitOne();
x++;
}
else if (x == 1)
{
System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, autoEvent, new TimeSpan(12, 0, 0), new TimeSpan(0, 0, 0, 0, -1));
autoEvent.WaitOne();
}
}
}
|
|
|
|

|
Hi , thanks for nice article
I have a problem, when i follow step by step that article in vs2008 and build my project and finally i run Bin\Release\MyProect.exe i've recieved a windows problem "MyProect has encountered a problem and needs to close. We are sorry for the inconvenience."
Here is My Code :
namespace PostIt
{
public partial class frmPostIT : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon m_notifyicon;
private System.Windows.Forms.ContextMenu m_menu;
public frmPostIT()
{
InitializeComponent();
m_notifyicon = new System.Windows.Forms.NotifyIcon();
m_notifyicon.Text = "Post IT";
m_notifyicon.Visible = true;
m_notifyicon.Icon = new System.Drawing.Icon(GetType(), "NotifyPostIT");
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Add New PostIt", new System.EventHandler(Add_Click)));
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Save Current PostIt", new System.EventHandler(Save_Click)));
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Load Last PostIt", new System.EventHandler(load_Click)));
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Exit PostIt", new System.EventHandler(Exit_Click)));
}
protected void Add_Click(System.Object sender, System.EventArgs e)
{
}
protected void Save_Click(System.Object sender, System.EventArgs e)
{
}
protected void Load_Click(System.Object sender, System.EventArgs e)
{
}
protected void Exit_Click(System.Object sender, System.EventArgs e)
{
Close();
}
}
}
|
|
|
|

|
Hi, thanks for your article
i have a problem , when i follow your solution step by step in VS2008 and build my project ,finally I run bin\Release\Myproject.exe and recieve a windows message "Myproject.ex has encountered a problem and needs to close. We are sorry for the inconvenience."
this is my code
namespace PostIt
{
public partial class frmPostIT : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon m_notifyicon;
private System.Windows.Forms.ContextMenu m_menu;
public frmPostIT()
{
InitializeComponent();
m_notifyicon = new System.Windows.Forms.NotifyIcon();
m_notifyicon.Text = "Post IT";
m_notifyicon.Visible = true;
m_notifyicon.Icon = new System.Drawing.Icon(GetType(), "NotifyPostIT");
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Add New PostIt", new System.EventHandler(Add_Click)));
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Save Current PostIt", new System.EventHandler(Save_Click)));
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Load Last PostIt", new System.EventHandler(load_Click)));
m_menu.MenuItems.Add(0, new System.Windows.Forms.MenuItem("Exit PostIt", new System.EventHandler(Exit_Click)));
}
protected void Add_Click(System.Object sender, System.EventArgs e)
{
}
protected void Save_Click(System.Object sender, System.EventArgs e)
{
}
protected void Load_Click(System.Object sender, System.EventArgs e)
{
}
protected void Exit_Click(System.Object sender, System.EventArgs e)
{
Close();
}
}
}
---------------
Honey Variani
---------------
|
|
|
|

|
where is the code that i can download
|
|
|
|

|
why is there a limitation of 64 characters?
m_notifyicon.Text = "Right click for context menu .................. more than 64 characters. breaks everything off..... breaksss ......................";
in an old project of mine in C++ the tooltips have a limitation of 128 characters.
|
|
|
|

|
It helps me in my job but i wanna idea is there any way how to display context menu on mouse over like as tooltip
finally thanks for the article
evil
|
|
|
|

|
The ressource 'Icon1.ico' could not be found in class 'TrayTest.Form1'
error on Line m_notifyicon.Icon = new Icon(GetType(),"Icon1.ico")
s
|
|
|
|

|
Did you make sure to change it to embedded? Is that the proper name of the object?
Jeff
|
|
|
|

|
The GetType() (or typeof(somethinginthisassembly)) doesn't seme to work for class libraries.
The flollowing works, though:
m_icon = new Icon(
System.Reflection.Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("project.iconname.ico"));
Where project is usually the class library project name, and iconname.ico is the embedded resource name. You can also see the "full" resource name using Reflector.
|
|
|
|

|
Even that doesn't really work for me.
I'm just trying this out to learn how to do it,
but despite it's beauty and simplicity - it just doesn't run.
I added the resource myIcon.ico (in Visual C# Express, newest version)
and I am getting a folder "Resources" where the icon seems to live in.
After that I edit the icon's properties.
Build Action: Embedded Resource
m_notifyicon.Icon = new Icon(System.Reflection.Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("TrayTest.Form1.myIcon.ico"));
Yields: "Value of 'null' is not valid for 'stream'." on runtime.
This:
m_notifyicon.Icon = new Icon(System.Reflection.Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("TrayTest.Resources.myIcon.ico"));
Yields: Oooops, it doesn't yield any error, but an icon in the tray!
Don't seem to have tried that before...
Well then, no problem left, just solved it.
May this post help other people as dumb as me who can't get their paths right.
Btw, the world is a great place, actually. Must see it!
|
|
|
|

|
I guess that's what I meant - If you open the binary with the embedded icon in Lutz Röders Reflector[^], you will see the correct path.
|
|
|
|

|
Actually i have created one context menu and assigned it for the system tray icon's context menu. I wanted to change the particular context menu's text dynamically. Using the property contextmenuname.text i have changed the context menu's text. But it didn't get refreshed in the tray icon.
How can we do this?
|
|
|
|

|
Can this work for a windows service? From my recent trials, it doesn't seem to.
Got a coding problem? Hand it to the CodeDevil!
|
|
|
|

|
Very easy and clear !! As someone also posted a question. How can we minimize windows to system tray when we click on minimize button?
Let me know if you have solution for this.
Thanks,
Tejpal
|
|
|
|

|
Hello… Thanks for the article! Straight forward except for one thing…
When I tried it in VS 2005 Team Developer, it complained that Form1 already had a dispose. After scratching my head for a while, I realized - There is a dispose in the "other part" of the partial class (Form1.Designer.cs) - in which it went:
if (disposing && (components != null))
{
components.Dispose();
}
Which I changed to:
if (disposing)
{
if (components != null)
{
components.Dispose();
}
this.m_notifyicon.Dispose(); }
Then it worked… Thanks!
|
|
|
|

|
Hi,
What about canceling the context menu ? Today, we are used to click anywhere else than on context menu to cancel it (or to hide it). How can I detect a click outside the context menu region ?!?
Thanks.
_____________________________
Fylom
|
|
|
|

|
Many programs minimize to the system tray when you minimize them and rise up out of the system tray as you open the form again.
Is this possible in vb.net?
Mike
|
|
|
|

|
Very Nice article. To the point, clear and Concise.
<html>
Without education, you're not going anywhere in this world.
Malcolm X, speech at Militant Labor Forum, NY, 29 May 1964
</html>
|
|
|
|

|
Simple and easy to follow. I like that!
ak
|
|
|
|

|
I've know that Windows Mobile 5.0 supporting tray icon on today screen, I've have the Windows Mobile 5.0 SDK and there is a sample creating tray icon application but the code in C/C++. Now i want to change the code into C#, but i have problem create NotifyIconData object on cbSize variable, how to use "sizeof" function in C#, because creating tray icon in Windows Mobile and Windows 32 is same nothing difference. Ok my problem is when i want to take size of NotifyIconData object it always make exception of NotSupportedException, i have use "sizeof" and "Marshal.SizeOf" but still have same problems. My code is :
* The NOTIFYICONDATA Structure
[StructLayout(LayoutKind.Sequential)]
public struct NOTIFYICONDATA
{
public int cbSize;
public int hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public int hIcon;
public char[] szTip;
}
* Get the structure size on cbSize
...
this.tryData.cbSize = Marshal.SizeOf(tryData);
...
I have use alternate for char[] into string but still same exception.
Sorry long message, i hope you can help. Thanks a lot.
|
|
|
|

|
NOTIFYICONDATA notdata = new NOTIFYICONDATA();
notdata.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(notdata);
notdata.hIcon = hIcon;
notdata.hWnd = hwnd;
notdata.uCallbackMessage = WM_NOTIFY_TRAY;
notdata.uFlags = NIF_MESSAGE | NIF_ICON;
notdata.uID = uID;
I think u can use like this, it gives no exception.
yuvraj
|
|
|
|

|
Thanks for the article.. Its very well written.
Can you help out on how to attach a Context Menu to the NotifyIcon in the .Net Compact Framework.
I have succesfully created a NotifyIcon to the application using the article at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/messagewindow.asp[^]. But I need help on attaching a Context Menu to the NotifyIcon..
Please help... really in need.
Thanks in advance.
We are alone, absolutely alone on this chance planet: and, amid all the forms of life that surround us, not one, excepting the dog, has made an alliance with us. - Maurice Maeterlinck
|
|
|
|

|
Since you're using the NotifyIcon class from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/messagewindow.asp, just add the following to your application (to your Form class).
Create a context menu, add some menu items to it, add some click event handlers for the menu items.
Add these declarations:
[DllImport("coredll.dll")]
internal static extern IntPtr LoadIcon(IntPtr hInst, string IconName);
[DllImport("coredll.dll")]
internal static extern IntPtr GetModuleHandle(String lpModuleName);
private NotifyIcon notifyIcon;
Set up your notifyicon (probably in the constructor for your Form class):
notifyIcon = new NotifyIcon();
notifyIcon.Click += new EventHandler(notifyIcon_Click);
AddIcon();
Add these methods:
private void AddIcon()
{
IntPtr hIcon = LoadIcon(GetModuleHandle(null), "#32512");
notifyIcon.Add(hIcon);
}
private void RemoveIcon()
{
notifyIcon.Remove();
}
private void notifyIcon_Click(object sender, EventArgs e)
{
contextMenu1.Show(this, new System.Drawing.Point(20, 20));
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
RemoveIcon();
}
|
|
|
|

|
I have a thought to alter the behavior of standard clock provided in system tray. Do you have any idea how to access it. Thanks
|
|
|
|

|
I didn't realize it was this simple.
Jon Sagara
As you may presently yourself be fully made aware of, my grammar sucks.
Sagara.org | Blog | My Articles
|
|
|
|

|
Hi, I am writing a c# code that opens a file and searches for some condition and according to the condition that occuring tray icon changes.This should be done every 10 minutes.
I have coded this part well but I can not exit my application from a context menu item(exit) which never appears.
If I erase the line where my while loop appear, context menu appears and every thing goes well.
Do you have any suggestion where I might have made a mistake? I am a c# newbie and I cannot find the solution...
thanks for your help very much..
Here is the code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
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;
private System.Boolean check;
public Form1()
{
this.check=true;
//this.HideApp();
//this.ShowInTaskbar = false;
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);
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 (Situation controller)";
notifyIcon1.Visible = true;
notifyIcon1.Icon = new Icon("Icon2.ico");
while(this.check==true)
{
FileStream file = new FileStream("path.txt", FileMode.Open, FileAccess.Read,FileShare.Read);
StreamReader sr = new StreamReader(file);
string s;
bool control=false;
while((s = sr.ReadLine())!= null)
{
if(s.IndexOf("down")!=-1)
{
control=true;
}
}
if(control==true)
{
notifyIcon1.Icon = new Icon("Icon2.ico");
}
else
{
notifyIcon1.Icon = new Icon("Icon1.ico");
}
sr.Close();
file.Close();
//bu kısmı degistirdim
Thread.Sleep(1000*10*1);//1000*60*10
//this.Close();
//Application.Exit();
}
this.notifyIcon1.Dispose();
}
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)
{
this.check=false;
notifyIcon1.Visible = false;
Application.Exit();
this.Close();
}
private void InitializeComponent()
{
//
// Form1
//
//this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
//this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.ShowInTaskbar = false;
this.Load += new System.EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
public void HideApp()
{
this.WindowState = FormWindowState.Minimized;
Hide();
}
public void ShowApp()
{
Show();
this.WindowState = FormWindowState.Normal;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
|
|
|
|

|
It was completely helpful.I was going mad with c++ and WIN32 stuff.thank you very much...
|
|
|
|

|
Hi,
Lovely article, and very useful. Well done.
Here is a question people .. I am implementing a desktop switch to lock down a machine until it receives an unlocking command on a TCP port. I am in the process of doing this through a tray icon application, and most of the mechanism works.
Problem: when I'm in desktop 1, say, and the tray app is running, listening to the port, and it gets an unlock command, I want to switch to desktop 2, with full explorer access, but I want to take the tray app with me from desktop 1 so that I could lock the desktop again. This is for a cyber caf control application.
Any ideas ?
Thanks so much.
Ahmed.
|
|
|
|

|
Hi,
Nice article Nish ! but... there's one basic thing that is not pointed in this article... How can I show the window when the user presses double click over the notification icon ? Is that not so easy pc ?
|
|
|
|

|
Ok it was easy
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
MessageBox.Show("Icon Notify Double Clicked");
}
|
|
|
|

|
How would you show the balloon above the icon in the tray?
|
|
|
|

|
Now all you need is to set the notifyicon's visible property to false in the constructor and when restored, and to true when minimized, and you'll have the icon appear only when you need it.
|
|
|
|

|
It was nice. A breath of frest air. ah.
/\ |_ E X E GG
|
|
|
|

|
Does anyone have an example of doing the same thing, but for a Console Application? (command prompt running service)
Thanks,
-Matt
|
|
|
|
|

|
I haven't tried your code, but I have used the NotifyIcon control in C#. It is MUCH easier than it used to be - it is intuitive, and should have always been this easy.
I just want to add one thing I figured out along the way. You can't do owner draw MenuItems with NotifyIcon. When I was debugging I found that the OnDrawItem and OnMeasureItem events were never firing on ContextMenu when it was called by the NotifyIcon, but when I moved the same code to a context menu of another control, it worked fine.
I searched and, luckily, found this: http://support.microsoft.com/?id=827043 Microsoft KnowledgeBase article 827043 - BUG: The ContextMenu Control That Is Associated with the NotifyIcon Component Is Not Displayed When You Set the OwnerDraw Property of Menu Items in the ContextMenu Control to True.
The article doesn't have a fix for the problem, but at least I won't be wasting more time trying to troubleshoot it.
Dale
|
|
|
|

|
is there a vb.net sample for this?
|
|
|
|

|
This code worked great for me, but I'm wondering how can I disable a menu
item until I want it used?
|
|
|
|

|
Hello,
I very much enjoyed this article, it was quite helpful - thank you.
However, I've found a situation where the tray icon doesn't properly dissapear when the application exits.
If the application form is currently hidden (this.Visible = false), and I call Application.Exit(), I get a ghost icon in the tray.
Apart from setting this.Visible = true before closing the app (kind of annoying since the app flashes into view and then closes again), is there any other way I can stop this from happening?
What happens to the form during this.Visible = false that would prevent the tray icon from properly disposing?
Thanks for any help or advice you can lend me!
oblius
http://www.oblius.com/
|
|
|
|

|
You should just be able to put a line in the Dispose method of the form:
this.NotifyIcon.Dispose();
Doing that should remove the icon when the form is disposed
|
|
|
|

|
I found an even simpler solution. Before calling this.Close(), just call this.NotifyIcon.Visible = false. Doing that should remove the icon when the form is closed.
|
|
|
|
|

|
I have set the notifyicon with the same icon that appears in the upperleft corner of my Form. But the icon in the system tray is an older version (not transparent and ugly) of the icon that the form is using. But there are no older versions of this icon on my computer. Somewhere this older version of the icon is hid somewheren. Anyone got a clue? I've deleted all references to this older version (I think) and there are no non-transparent versions of this icon inside the icon file......
Please help me.
erald
|
|
|
|

|
You'll probably find it's displaying the new version of your icon but colour-quantised to 4-bit (16 colours) and 1-bit alpha. Your best bet is probably to create a composite .ico file that contains both the new version of your icon and a carefully dithered 4-bit version of your new icon. If I'm right, this will cause the system notification area to display the carefully dithered 4-bit version of your icon rather than programmatically creating a 4-bit icon from your 8- or 24-bit icon.
Annoying as this limitation of the system notification area is, I don't know of any work arounds.
|
|
|
|

|
I had written a similar program recently.
However, I wanted to hide the main form window when the app starts up (i.e. only show the tray icon). I tried putting a Hide() call in the constructor but to no effect. I also tried putting Visible = false. If I do it later in the execution stream (say off of the context menu) it works fine.
Do you know of a way to tell a form application to not display it's main window by default?
(Update)
I forgot to say that the only thing that seemed to work was adding
WindowState = FormWindowState.Minimized;
ShowInTaskbar = false;
to the constructor. But this seems a bit kludgy to me. Is there a better way to do this?
|
|
|
|

|
The problem is that when you call Application.Run(MyForm), this in turn calls ThreadContext::RunMessageLoop(). It is this method which makes your form visible. So anything you do before this method is called is irrelevant.
I got as far as overriding Control::SetVisibleCore() before realizing that setting WindowState and ShowInTaskbar was less of a hack!
|
|
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Beginner's Tutorial on adding tray icons and setting context menus
| Type | Article |
| Licence | |
| First Posted | 10 Apr 2002 |
| Views | 420,632 |
| Bookmarked | 166 times |
|
|