How to make a WPF Window to blink on the taskbar?(如何使 WPF 窗口在任务栏上闪烁?)
问题描述
在某个特定时刻,我的 WPF 应用程序需要用户关注.我知道可以使 Windows 7 任务栏图标以黄色闪烁.
A given moment my WPF app needs user attention. I know it is possible to make the Windows 7 taskbar icon to flash with a yellow color.
到目前为止我试过了:
Window.Activate
尝试将窗口置于前台并激活它.Window.Focus
尝试将此元素设置为焦点.
Window.Activate
Attempts to bring the window to the foreground and activates it.Window.Focus
Attempts to set focus to this element.
有什么建议吗?
推荐答案
这是一种可能的解决方案:http://www.jarloo.com/flashing-a-wpf-window/
Here's one possible solution: http://www.jarloo.com/flashing-a-wpf-window/
在代码示例中,为 Window 类创建了两个扩展方法:FlashWindow 和 StopFlashingWindow:
In the code sample, two extensions methods are created for the Window class: FlashWindow and StopFlashingWindow:
private const UInt32 FLASHW_STOP = 0; //Stop flashing. The system restores the window to its original state. private const UInt32 FLASHW_CAPTION = 1; //Flash the window caption.
private const UInt32 FLASHW_TRAY = 2; //Flash the taskbar button.
private const UInt32 FLASHW_ALL = 3; //Flash both the window caption and taskbar button.
private const UInt32 FLASHW_TIMER = 4; //Flash continuously, until the FLASHW_STOP flag is set.
private const UInt32 FLASHW_TIMERNOFG = 12; //Flash continuously until the window comes to the foreground.
[StructLayout(LayoutKind.Sequential)]
private struct FLASHWINFO
{
public UInt32 cbSize; //The size of the structure in bytes.
public IntPtr hwnd; //A Handle to the Window to be Flashed. The window can be either opened or minimized.
public UInt32 dwFlags; //The Flash Status.
public UInt32 uCount; // number of times to flash the window
public UInt32 dwTimeout; //The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
public static void FlashWindow(this Window win, UInt32 count = UInt32.MaxValue)
{
//Don't flash if the window is active
if (win.IsActive) return;
WindowInteropHelper h = new WindowInteropHelper(win);
FLASHWINFO info = new FLASHWINFO
{
hwnd = h.Handle,
dwFlags = FLASHW_ALL | FLASHW_TIMER,
uCount = count,
dwTimeout = 0
};
info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));
FlashWindowEx(ref info);
}
public static void StopFlashingWindow(this Window win)
{
WindowInteropHelper h = new WindowInteropHelper(win);
FLASHWINFO info = new FLASHWINFO();
info.hwnd = h.Handle;
info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));
info.dwFlags = FLASHW_STOP;
info.uCount = UInt32.MaxValue;
info.dwTimeout = 0;
FlashWindowEx(ref info);
}
访问 http://www.jarloo.com/flashing-a-wpf-window/ 获取完整源代码.
Visit http://www.jarloo.com/flashing-a-wpf-window/ for the complete source.
相当有趣的场景.我原以为这会很简单.如果我不得不做类似的事情,我会为这个问题添加书签:)
Pretty interesting scenario. I would have thought it would be something simple. I'll have bookmark this question in the event I ever have to do something similar :)
这篇关于如何使 WPF 窗口在任务栏上闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使 WPF 窗口在任务栏上闪烁?
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30