Apply changes to mainforms Form.Icon at runtime
I have a System.Windows.Forms.Form and want to change the Form.Icon at runtime to display a status. I've managed to load the icon from the projects ressources:
Type type = this.GetType();
System.Resources.ResourceManager resources =
new System.Resources.ResourceManager(type.Namespace + ".Properties.Resources", this.GetType().Assembly);
this.Icon = (System.Drawing.Icon)resources.GetObject(
type.Namespace + ".Icons." + statusText + ".ico");
But the displayed icon stays the same (design time i开发者_开发技巧con) all the time. Do I have to call a method to tell the Form to apply changes? Something wrong about my usage of Form.Icon?
It is unclear to me why you are doing this the hard way. Just add the icon to your resources. Project + Properties, Resource tab, arrow on Add Resource button, Add Existing File. Then you'd use it like this at runtime:
private void button1_Click(object sender, EventArgs e) {
this.Icon = Properties.Resources.Mumble;
}
Where Mumble is the name of the icon.
If you are 100% sure that GetObject() doesn't return null then try setting the Icon property in the designer. If it still doesn't show up then there's something wrong with the icon format. Make sure that it doesn't have too many colors, 256 works on XP.
Ok, Siva and Hans where right: GetObject returned null, because the name of the ressource wasn't right. With the following change it works:
Type type = this.GetType();
System.Resources.ResourceManager resources =
new System.Resources.ResourceManager(type.Namespace + ".Properties.Resources", this.GetType().Assembly);
// here it comes, call GetObject just with the resource name, no namespace and no extension
this.Icon = (System.Drawing.Icon)resources.GetObject(statusText);
Thanks for all your help.
I Guess, first of all, your GetObject(...) is returning null. Thats the reason the type casting is ending up silently neither throwing error nor change the icon.
Instead,if possible, use
this.Icon = new System.Drawing.Icon(...)
overloads and then give a try.
Yes, you have to change the icon whenever the status of your application changes.
I tested this out with a simple WinForm application:
private void button1_Click(object sender, EventArgs e)
{
this.Icon = Properties.Resources.Gear;
}
private void button2_Click(object sender, EventArgs e)
{
this.Icon = Properties.Resources.UAC_shield;
}
When the program is running, clicking on each button will change the form icon (and, of course, the icon in the taskbar) to the specified icon. I just picked some icons from the set that ships with Visual Studio and added them to the project's Resource file.
You should be able to add a simple method that you can call anywhere in your code which sets the icon (and you can call it from Form_Load as well):
private void ChangeIconStatus(string statusText)
{
Type type = this.GetType();
System.Resources.ResourceManager resources =
new System.Resources.ResourceManager(type.Namespace + ".Properties.Resources", this.GetType().Assembly);
this.Icon = (System.Drawing.Icon)resources.GetObject(type.Namespace + ".Icons." + statusText + ".ico");
}
"this.Icon" resolves general icon...
But if you want to change the minimized icon of your desktop application, do as follows. This could also work if you want to cover every possibility.
Drag a control "NotifyIcon" (TOOLBOX->COMMON CONTROLS) in your WinForm application. It is transparent, so put it in some corner ... it doesn't matter.
Click the Form and override RESIZE event.
Copy the following code:
/// <summary>
/// GET - if the form is minimized, hide it from the task bar and show the system tray icon
/// (represented by the NotifyIcon control)
/// </summary>
private void Form1_Resize(object sender, EventArgs e)
{
// CTRL -
if (this.WindowState == FormWindowState.Minimized)
{
// RESET - hide this from taskbar
Hide();
// SET - show system tray icon
notifyIcon1.Visible = true;
// INIT - tray icon
// change this as you wish - from the Exe PATH, you have to have a
// "Resources" directory with inside your icon. Just to not put
// every icon inside the exe directory...
Icon tempIcon = Icon.ExtractAssociatedIcon("Resources/myIcon.ico");
notifyIcon1.Icon = tempIcon;
}
// RET
return;
}
The result is that if your program starts MINIMIZED or will be MINIMIZED, the icon will be set by the code.
This solution is in ADDITION to this code that must be put in the "main"
/// <summary>
/// MAIN - with optional arguments
/// </summary>
public Form1(string[] arguments)
{
// INIT
InitializeComponent();
// INIT - icon of the desktop application
Icon tempIcon = Icon.ExtractAssociatedIcon("Resources/myIcon.ico");
this.Icon = tempIcon;
// RET
return;
}
bye
精彩评论