C# Windows 7任务栏开发之图标闪动(Flash)

      使用QQ聊天时,如果对方发出了信息QQ 图标会闪动提示,虽然Windows API 没有直接控制闪动效果的方法,但该效果在开发中可能会经常使用,下面代码为一个闪动效果类:

/// <summary>
/// Win32 API
/// </summary>
internal static class Win32
{
    /// <summary>
    /// 窗口闪动
    /// </summary>
    /// <param name="hwnd">窗口句柄</param>
    /// <param name="bInvert">是否为闪</param>
    /// <returns>成功返回0</returns>
    [DllImport("user32.dll")]
    public static extern bool FlashWindow(IntPtr hwnd, bool bInvert);
}
/// <summary>
/// 窗口闪动的辅助类
/// </summary>
public class FlashWindowHelper
{
    Timer   _timer;
    int     _count      = 0;
    int     _maxTimes   = 0;
    IntPtr  _window;

    public void Flash(int times, double millliseconds, IntPtr window)
    {
        _maxTimes   = times;
        _window     = window;

        _timer = new Timer();
        _timer.Interval = millliseconds;
        _timer.Elapsed += _timer_Elapsed;
        _timer.Start();
    }

    void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (++_count < _maxTimes)
        {
            Win32.FlashWindow(_window, (_count % 2) == 0);
        }
        else
        {
            _timer.Stop();
        }
    }
}
      通过FlashWindowHelper 类可以轻松的使任务栏图标闪动起来:

/// <summary>
/// 通过FlashWindowHelper 类可以轻松的使任务栏图标闪动起来:
/// </summary>
private void _btnFlash_Click(object sender, EventArgs e)
{
    FlashWindowHelper helper = new FlashWindowHelper();
    helper.Flash(10, 300, this.Handle);
}



你可能感兴趣的:(windows,7,任务栏)