【原创】使用timer、imagelist与notifyicon控件实现图标闪烁-----并避免了GDI泄漏(重点)
2011-09-07 08:11
525 查看
一、未考虑GDI泄漏的代码:后果是直接导致程序的GDI数一直增加,直到程序crash(崩溃)为止
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap btm1 = new Bitmap(this.imageList1.Images[0]);
Icon icon1 = Icon.FromHandle(btm1.GetHicon());
Bitmap btm2 = new Bitmap(this.imageList1.Images[1]);
Icon icon2 = Icon.FromHandle(btm2.GetHicon());
if (i < 1)
{
this.notifyIcon1.Icon = icon1;
i = 1;
}
else
{
this.notifyIcon1.Icon = icon2;
i = 0;
}
}
二、修改之后的代码:
int i = 0;
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap btm1 = new Bitmap(this.imageList1.Images[0]);
Icon icon1 = Icon.FromHandle(btm1.GetHicon());
Bitmap btm2 = new Bitmap(this.imageList1.Images[1]);
Icon icon2 = Icon.FromHandle(btm2.GetHicon());
if (i < 1)
{
this.notifyIcon1.Icon = icon1;
i = 1;
}
else
{
this.notifyIcon1.Icon = icon2;
i = 0;
}
DestroyIcon(icon2.Handle);
DestroyIcon(icon1.Handle);
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap btm1 = new Bitmap(this.imageList1.Images[0]);
Icon icon1 = Icon.FromHandle(btm1.GetHicon());
Bitmap btm2 = new Bitmap(this.imageList1.Images[1]);
Icon icon2 = Icon.FromHandle(btm2.GetHicon());
if (i < 1)
{
this.notifyIcon1.Icon = icon1;
i = 1;
}
else
{
this.notifyIcon1.Icon = icon2;
i = 0;
}
}
二、修改之后的代码:
int i = 0;
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
private void timer1_Tick(object sender, EventArgs e)
{
Bitmap btm1 = new Bitmap(this.imageList1.Images[0]);
Icon icon1 = Icon.FromHandle(btm1.GetHicon());
Bitmap btm2 = new Bitmap(this.imageList1.Images[1]);
Icon icon2 = Icon.FromHandle(btm2.GetHicon());
if (i < 1)
{
this.notifyIcon1.Icon = icon1;
i = 1;
}
else
{
this.notifyIcon1.Icon = icon2;
i = 0;
}
DestroyIcon(icon2.Handle);
DestroyIcon(icon1.Handle);
}
相关文章推荐
- C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示 很多程序是只需要后台运行的,甚至不需要自己的应用界面。NotifyIcon提供了程序在任务栏的显示功能 程序下载链接如下
- 【原创】使用GridView实现绑定List并排序
- Windows界面绘图使用缓冲区避免闪烁:MFC版本 和 GDI版本
- 使用SmartImageView控件实现网络图片的浏览
- 使用ImageView、TextView实现底部导航栏,图标不能居中
- c#使用系统ImageList显示文件图标
- 使用DevExpress的TreeList控件的效果和实现代码
- 如何在Delphi TImageList 中使用 透明 png 图标
- C#中使用repeater控件实现List的clear,add,update,delete功能之二数据库
- 使用定时器timer实现按钮闪烁的效果
- 使用双重缓冲,避免窗体中控件位置移动的时候闪烁
- ODBC API 使用SQLPutData实现SQL数据库BLOB(TEXT、IMAGE类型字段)数据提交[原创](一)
- MFC中使用文本控件实现图标和位图的显示
- [原创]使用html控件中file field实现WEB上传
- ODBC API 使用SQLPutData实现SQL数据库BLOB(TEXT、IMAGE类型字段)数据提交[原创](二)
- 使用ASP.NET AJAX Control Toolkit中的ReorderList控件实现用鼠标拖动改变条目顺序
- Visual Studio 2010——C#的TreeView和ImageList控件的使用
- ImageList控件的使用方法
- [Android初级]关于使用universal-image-loader加载imagelist出现图片闪烁的问题
- 如何在Delphi TImageList 中使用 透明 png 图标