Apply changes to mainforms Form.Icon at runtime

后端 未结 4 992
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 01:57

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:



        
4条回答
  •  感情败类
    2020-12-07 02:10

    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");
    }
    

提交回复
热议问题