I want to change a Form's icon at runtime.
here's my sample code:
public partial class Form1 : Form
{
bool changingIcon = false;
Icon blankIcon;
Icon musicIcon;
int iconState = 0;
public Form1()
{
InitializeComponent();
timer1.Interval = 1000;
blankIcon = (Icon)Properties.Resources.blankcd.Clone();
musicIcon = (Icon)Properties.Resources.cdmusic.Clone();
}
private void button1_Click(object sender, EventArgs e)
{
if (changingIcon)
{
changingIcon = false;
timer1.Enabled = false;
}
else
{
changingIcon = true;
timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Icon previousIcon = this.Icon;
IntPtr hIcon = previousIcon.Handle;
previousIcon.Dispose();
if (0 == iconState)
{
this.Icon = (Icon)blankIcon.Clone();
iconState = 1;
}
else
{
this.Icon = (Icon)musicIcon.Clone();
iconState = 0;
}
}
}
But when runnning this program, the privatebytes counter continually
increase.
How can I dispose the resource of changed Form's Icon ?
What is the correct way to change a Form's icon at runtime ?
|