FlashWindowEx实现窗口在任务栏闪烁/变化颜色

时间:2024-09-02 21:36:38

原文:FlashWindowEx实现窗口在任务栏闪烁/变化颜色

效果类似QQ收到新的会话消息任务栏颜色变化

附2小段代码:

[System.Runtime.InteropServices.DllImport("user32.dll")]     
  static   extern   int   FlashWindow   (int   hwnd,   int   bInvert);   
  private   void   button1_Click(object   sender,   System.EventArgs   e)   
  {   
    FlashWindow(this.Handle.ToInt32(),1); // 参数为1时启动闪烁, 参数为0时停止闪烁 
  }
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool FlashWindow(
              IntPtr hWnd,           //   handle   to   window  
              bool bInvert       //   flash   status  
              );   
private void button1_Click(object sender, EventArgs e)
{
  FlashWindow(this.Handle, true);
}

当后台应用程序完成工作,可以通过闪动窗体来通知用户.

1.利用窗体的Activate方法

this.Activate();

此方法会直接将目标窗体显示在当前工作窗体的前面.并且当目标窗体被最小化时,使用此方法基本无效.

2.利用Windows API-FlashWindowEx

查了MSDN发现与Flash Window相关的API有两个:

一个FlashWindow,函数原型为:

BOOL WINAPI FlashWindow( __in HWND hWnd, __in BOOL bInvert);

此API只闪动窗体一次,显然不是我们想要的结果.

另外一个是FlashWindowEx,函数原型为:

BOOL WINAPI FlashWindowEx( __in PFLASHWINFO pfwi);

参数PFLASHWINFO 声明为:

typedef struct {
UINT cbSize;
HWND hwnd;
DWORD dwFlags;
UINT uCount;
DWORD dwTimeout;
} FLASHWINFO, *PFLASHWINFO;
字段说明:

cbSize结构体FLASHWINFO的大小.

hwnd要闪动的窗体的句柄.窗体可以是打开的也可以是最小化的.

dwFlags

闪动的状态/标志. 此参数可以是以下的值或值的组合(用或操作符"|"组合).

含义

FLASHW_ALL
0x00000003

闪动窗体标题和任务栏按钮,等于将此参数设置为:

FLASHW_CAPTION | FLASHW_TRAY.

FLASHW_CAPTION
0x00000001
闪动窗体的标题.

FLASHW_STOP
0
停止闪动.系统将窗体恢复到初始状态.

FLASHW_TIMER
0x00000004
连续不停的闪动,直到此参数被设置为:FLASHW_STOP

FLASHW_TIMERNOFG
0x0000000C
连续不停的闪动,直到窗体用户被激活.通常用法将参数设置为: FLASHW_ALL | FLASHW_TIMERNOFG

FLASHW_TRAY
0x00000002
闪动任务栏按钮

uCount闪动的次数.

dwTimeout闪动频率,单位毫秒. 如果dwTimeout值为0, 函数使用光标的闪动频率.

在C#中引用此API:

1.因为FlashWindowEx的参数是一个FLASHWINFO结构体类型,所以首先按FLASHWINFO格式声明一个结构体:

public struct FLASHWINFO
{
public UInt32 cbSize;
public IntPtr hwnd;
public UInt32 dwFlags;
public UInt32 uCount;
public UInt32 dwTimeout;
}

2. 导入API原型:

[DllImport("user32.dll")]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

3.应用API:

private void button1_Click(object sender, EventArgs e)
{

//此处按API中参数dwFlags的取值范围声明常量,方便在使用API时调用,实际应用时可以考虑放到一个专门的文件中

public const UInt32 FLASHW_STOP = 0;
public const UInt32 FLASHW_CAPTION = 1;
public const UInt32 FLASHW_TRAY = 2;
public const UInt32 FLASHW_ALL = 3;
public const UInt32 FLASHW_TIMER = 4;
public const UInt32 FLASHW_TIMERNOFG = 12;

this.WindowState = FormWindowState.Minimized;

//准备参数

FLASHWINFO fInfo = new FLASHWINFO();

fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = this.Handle;
fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;//这里是闪动窗标题和任务栏按钮,直到用户激活窗体
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;

//调用API

FlashWindowEx(ref fInfo);
}

至此,闪动窗体通知用户的功能实现.

完整代码为:

using System;

 using System.Collections.Generic;

 using System.ComponentModel;

 using System.Data;

 using System.Drawing;

 using System.Linq;

 using System.Text;

 using System.Windows.Forms;

 using System.Runtime.InteropServices;

 

 namespace FlashWindowEX

 {

     public struct FLASHWINFO

     {

         public UInt32 cbSize;

         public IntPtr hwnd;

         public UInt32 dwFlags;

         public UInt32 uCount;

         public UInt32 dwTimeout;

     } 

 

     public partial class Form1 : Form 

     {

         public const UInt32 FLASHW_STOP = 0;

         public const UInt32 FLASHW_CAPTION = 1;

         public const UInt32 FLASHW_TRAY = 2;

         public const UInt32 FLASHW_ALL = 3;

         public const UInt32 FLASHW_TIMER = 4;

         public const UInt32 FLASHW_TIMERNOFG = 12; 

 

         public Form1() 

         { 

             InitializeComponent(); 

         } 

 

         [DllImport("user32.dll")] 

         static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

 

         [DllImport("user32.dll")]

         static extern bool FlashWindow(IntPtr handle, bool invert); 

 

 

         private void button1_Click(object sender, EventArgs e) 

         {

             this.WindowState = FormWindowState.Minimized; 

 

             FLASHWINFO fInfo = new FLASHWINFO();

 

             fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));

             fInfo.hwnd = this.Handle;

             fInfo.dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG;//这里是闪动窗标题和任务栏按钮,直到用户激活窗体

             fInfo.uCount = UInt32.MaxValue;

             fInfo.dwTimeout = 0; 

 

             FlashWindowEx(ref fInfo);

         }

     }

 }