当收到新消息时,如何使任务栏像Messenger一样闪烁我的应用程序?

时间:2020-03-05 18:56:11  来源:igfitidea点击:

当与我聊天的人做出响应时,.NET中是否存在API调用或者本机DLL,我可以使用它来创建与Windows Live Messenger类似的行为?

解决方案

回答

FlashWindowEx Win32 API是用于执行此操作的调用。其文档位于:
http://msdn.microsoft.com/zh-CN/library/ms679347(VS.85).aspx

回答

HWND hHandle = FindWindow(NULL,"YourApplicationName");
FLASHWINFO pf;
pf.cbSize = sizeof(FLASHWINFO);
pf.hwnd = hHandle;
pf.dwFlags = FLASHW_TIMER|FLASHW_TRAY; // (or FLASHW_ALL to flash and if it is not minimized)
pf.uCount = 8;
pf.dwTimeout = 75;

FlashWindowEx(&pf);

被专家交流成员gtokas窃取。

FlashWindowEx。

回答

我相信我们正在寻找SetForegroundWindow

回答

从Raymond Chen博客条目中:

How do I flash my window caption and taskbar button manually?
  
  How do I flash my window caption and
  taskbar button manually? Commenter
  Jonathan Scheepers wonders about those
  programs that flash their taskbar
  button indefinitely, overriding the
  default flash count set by
  SysteParametersInfo(SPI_SETFOREGROUNDFLASHCOUNT).
  
  The FlashWindowEx function and its
  simpler precursor FlashWindow let a
  program flash its window caption and
  taskbar button manually. The window
  manager flashes the caption
  automatically (and Explorer follows
  the caption by flashing the taskbar
  button) if a program calls
  SetForegroundWindow when it doesn't
  have permission to take foreground,
  and it is that automatic flashing that
  the SPI_SETFOREGROUNDFLASHCOUNT
  setting controls.
  
  For illustration purposes, I'll
  demonstrate flashing the caption
  manually. This is generally speaking
  not recommended, but since you asked,
  I'll show you how. And then promise
  you won't do it.
  
  Start with the scratch program and
  make this simple change:

void
OnSize(HWND hwnd, UINT state, int cx, int cy)
{
  if (state == SIZE_MINIMIZED) {
    FLASHWINFO fwi = { sizeof(fwi), hwnd,
                       FLASHW_TIMERNOFG | FLASHW_ALL };
    FlashWindowEx(&fwi);
  }
}

  
  Compile and run this program, then
  minimize it. When you do, its taskbar
  button flashes indefinitely until you
  click on it. The program responds to
  being minimzed by calling the
  FlashWindowEx function asking for
  everything possible (currently the
  caption and taskbar button) to be
  flashed until the window comes to the
  foreground.
  
  Other members of the FLASHWINFO
  structure let you customize the
  flashing behavior further, such as
  controlling the flash frequency and
  the number of flashes. and if you
  really want to take control, you can
  use FLASHW_ALL and FLASHW_STOP to turn
  your caption and taskbar button on and
  off exactly the way you want it. (Who
  knows, maybe you want to send a
  message in Morse code.)
  
  Published Monday, May 12, 2008 7:00 AM
  by oldnewthing Filed under: Code

回答

FlashWindowEx是必经之路。请参阅此处以获取MSDN文档

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
    public UInt32 cbSize;
    public IntPtr hwnd;
    public UInt32 dwFlags;
    public UInt32 uCount;
    public UInt32 dwTimeout;
}

public const UInt32 FLASHW_ALL = 3;

调用函数:

FLASHWINFO fInfo = new FLASHWINFO();

fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
fInfo.hwnd = hWnd;
fInfo.dwFlags = FLASHW_ALL;
fInfo.uCount = UInt32.MaxValue;
fInfo.dwTimeout = 0;

FlashWindowEx(ref fInfo);

这是从Pinvoke.net无耻地插入的