如何使 WPF 窗口在任务栏上闪烁?

How to make a WPF Window to blink on the taskbar?(如何使 WPF 窗口在任务栏上闪烁?)

本文介绍了如何使 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 窗口在任务栏上闪烁?

基础教程推荐