zoukankan      html  css  js  c++  java
  • 【原创】使用timer、imagelist与notifyicon控件实现图标闪烁并避免了GDI泄漏(重点)

          一、未考虑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);

    }

  • 相关阅读:
    hash和history的区别帮助向我一样迷的人弄明白,history和hash
    调用谷歌浏览器的打印所遇到的困难,回流重绘
    webpack简单搭建基础感悟
    linux
    介绍一下call,apply,bind方法实现,源于MDN中的bind
    二进制流转base64加快速度
    手写一个instanceof
    青蛙跳台阶问题
    Django中vue的使用
    pip install 出现 timeout 时的两个临时解决办法
  • 原文地址:https://www.cnblogs.com/rainuu/p/2169394.html
Copyright © 2011-2022 走看看