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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/73162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:12:12  来源:igfitidea点击:

How to make the taskbar blink my application like Messenger does when a new message arrive?

windowswinapi

提问by tronda

Is there an API call in .NET or a native DLL that I can use to create similar behaviour as Windows Live Messenger when a response comes from someone I chat with?

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

回答by dummy

FlashWindowEx is the way to go. See here for MSDN documentation

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; 

Calling the Function:

调用函数:

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);

This was shamelessly plugged from Pinvoke.net

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

回答by nathaniel

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);

Stolen from experts-exchange member gtokas.

从专家交换成员 gtokas 处窃取。

FlashWindowEx.

FlashWindowEx

回答by Ian Boyd

From a Raymond Chen blog entry:

来自 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

如何手动刷新窗口标题和任务栏按钮?

如何手动刷新窗口标题和任务栏按钮?评论者 Jonathan Scheepers 想知道那些程序会无限期地闪烁其任务栏按钮,覆盖由 SysteParametersInfo(SPI_SETFOREGROUNDFLASHCOUNT) 设置的默认闪烁计数。

FlashWindowEx 函数及其更简单的前身 FlashWindow 允许程序手动闪烁其窗口标题和任务栏按钮。如果程序在没有获得前景的权限时调用 SetForegroundWindow,则窗口管理器会自动闪烁标题(并且资源管理器通过闪烁任务栏按钮跟随标题),而 SPI_SETFOREGROUNDFLASHCOUNT 设置控制的正是这种自动闪烁。

出于说明目的,我将演示手动闪烁标题。一般来说不推荐这样做,但既然你问了,我会告诉你如何。然后保证你不会这样做。

从scratch程序开始,做这个简单的改变:

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);
  }
}

编译并运行这个程序,然后最小化它。当你这样做时,它的任务栏按钮会无限期地闪烁,直到你点击它。程序通过调用 FlashWindowEx 函数来响应最小化,要求所有可能的东西(当前是标题和任务栏按钮)闪烁,直到窗口出现在前台。

FLASHWINFO 结构的其他成员让您可以进一步自定义闪烁行为,例如控制闪烁频率和闪烁次数。如果您真的想要控制,您可以使用 FLASHW_ALL 和 FLASHW_STOP 以您想要的方式打开和关闭您的标题和任务栏按钮。(谁知道呢,也许您想用摩尔斯电码发送消息。)

由 oldnewthing 于 2008 年 5 月 12 日星期一上午 7:00 发表 归档依据:代码

回答by Abe Heidebrecht

The FlashWindowEx Win32 API is the call used to do this. The documentation for it is at: http://msdn.microsoft.com/en-us/library/ms679347(VS.85).aspx

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

回答by Skizz

I believe you're looking for SetForegroundWindow.

我相信你正在寻找SetForegroundWindow