WPF 任务栏图标闪烁提醒

摘要:
1使用系统;2使用System.Collections.Generic;3使用System.Linq;4使用System.Runtime.InteropServices;5使用System.Text;6使用System.Windows;7使用System.Windows.Interop;8910公共静态类FlashWindo
WPF 任务栏图标闪烁提醒第1张WPF 任务栏图标闪烁提醒第2张
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Runtime.InteropServices;
  5 using System.Text;
  6 using System.Windows;
  7 using System.Windows.Interop;
  8 
  9 
 10 public static class FlashWindow
 11 {
 12     [DllImport("user32.dll")]
 13     [return: MarshalAs(UnmanagedType.Bool)]
 14     private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
 15 
 16     [StructLayout(LayoutKind.Sequential)]
 17     private struct FLASHWINFO
 18     {
 19         /// <summary>
 20         /// The size of the structure in bytes.
 21         /// </summary>
 22         public uint cbSize;
 23 
 24         /// <summary>
 25         /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
 26         /// </summary>
 27         public IntPtr hwnd;
 28 
 29         /// <summary>
 30         /// The Flash Status.
 31         /// </summary>
 32         public uint dwFlags;
 33 
 34         /// <summary>
 35         /// The number of times to Flash the window.
 36         /// </summary>
 37         public uint uCount;
 38 
 39         /// <summary>
 40         /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
 41         /// </summary>
 42         public uint dwTimeout;
 43     }
 44 
 45     /// <summary>
 46     /// Stop flashing. The system restores the window to its original stae.
 47     /// </summary>
 48     public const uint FLASHW_STOP = 0;
 49 
 50     /// <summary>
 51     /// Flash the window caption.
 52     /// </summary>
 53     public const uint FLASHW_CAPTION = 1;
 54 
 55     /// <summary>
 56     /// Flash the taskbar button.
 57     /// </summary>
 58     public const uint FLASHW_TRAY = 2;
 59 
 60     /// <summary>
 61     /// Flash both the window caption and taskbar button.
 62     /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
 63     /// </summary>
 64     public const uint FLASHW_ALL = 3;
 65 
 66     /// <summary>
 67     /// Flash continuously, until the FLASHW_STOP flag is set.
 68     /// </summary>
 69     public const uint FLASHW_TIMER = 4;
 70 
 71     /// <summary>
 72     /// Flash continuously until the window comes to the foreground.
 73     /// </summary>
 74     public const uint FLASHW_TIMERNOFG = 12;
 75 
 76 
 77     /// <summary>
 78     /// Flash the spacified Window (Form) until it recieves focus.
 79     /// </summary>
 80     /// <param name="form">The Form (Window) to Flash.</param>
 81     /// <returns></returns>
 82     public static bool Flash(Window w)
 83     {
 84         // Make sure we're running under Windows 2000 or later
 85         if (Win2000OrLater)
 86         {
 87             FLASHWINFO fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0);
 88             return FlashWindowEx(ref fi);
 89         }
 90         return false;
 91     }
 92 
 93     private static FLASHWINFO Create_FLASHWINFO(IntPtr handle, uint flags, uint count, uint timeout)
 94     {
 95         FLASHWINFO fi = new FLASHWINFO();
 96         fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi));
 97         fi.hwnd = handle;
 98         fi.dwFlags = flags;
 99         fi.uCount = count;
100         fi.dwTimeout = timeout;
101         return fi;
102     }
103 
104     /// <summary>
105     /// Flash the specified Window (form) for the specified number of times
106     /// </summary>
107     /// <param name="form">The Form (Window) to Flash.</param>
108     /// <param name="count">The number of times to Flash.</param>
109     /// <returns></returns>
110     public static bool Flash(Window w, uint count)
111     {
112         if (Win2000OrLater)
113         {
114             FLASHWINFO fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_ALL, count, 0);
115             return FlashWindowEx(ref fi);
116         }
117         return false;
118     }
119 
120     /// <summary>
121     /// Start Flashing the specified Window (form)
122     /// </summary>
123     /// <param name="form">The Form (Window) to Flash.</param>
124     /// <returns></returns>
125     public static bool Start(Window w)
126     {
127         if (Win2000OrLater)
128         {
129             var fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_ALL, uint.MaxValue, 0);
130             return FlashWindowEx(ref fi);
131         }
132         return false;
133     }
134 
135     /// <summary>
136     /// Stop Flashing the specified Window (form)
137     /// </summary>
138     /// <param name="form"></param>
139     /// <returns></returns>
140     public static bool Stop(Window w)
141     {
142         if (Win2000OrLater)
143         {
144             var fi = Create_FLASHWINFO(new WindowInteropHelper(w).Handle, FLASHW_STOP, uint.MaxValue, 0);
145             return FlashWindowEx(ref fi);
146         }
147         return false;
148     }
149 
150     /// <summary>
151     /// A boolean value indicating whether the application is running on Windows 2000 or later.
152     /// </summary>
153     private static bool Win2000OrLater
154     {
155         get { return System.Environment.OSVersion.Version.Major >= 5; }
156     }
157 }
FlashWindow


启动:FlashWindow.Start(Window w);

停止:FlashWindow.Stop(Window w);

.NET技术交流群 199281001 .欢迎加入。

免责声明:文章转载自《WPF 任务栏图标闪烁提醒》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇kudu 监控梯度下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框、文本框

本文主要介绍PyQt5界面最基本使用的单选按钮、复选框、下拉框三种控件的使用方法进行介绍。 1、RadioButton单选按钮/CheckBox复选框。需要知道如何判断单选按钮是否被选中。 2、ComboBox下拉框。需要知道如何对下拉框中的取值进行设置以及代码实现中如何获取用户选中的值。 带着这些问题下面开始介绍这RadioButton单选按钮、Chec...

WPF DataGrid多选功能开发

<DataTemplate x:Key="CheckBoxDataTemplate"> <Grid x:Name="Grid" HorizontalAlignment="Center" VerticalAlignment="Center"> <CheckBox...

以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转)

以前编写的inno setup脚本,涵盖了自定义安装界面,调用dll等等应用 (转) ; Script generated by the Inno Setup 脚本向导.   ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!      ; 为1的时候表示定义成...

学习WPF——了解WPF中的XAML

XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Button对象 XAML可以在一个元素中嵌套另一个元素,例如Grid元素可以嵌套Button元素   了解XAML VisualStudio创建一个窗口,默认...

WPF中RadioButton绑定数据的正确方法

RadioButton一般用于单选的时候,也就是从一组值中选择一个值。 比如性别有“男”和“女”两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女。 这种时候一般就会用到RadioButton。 RadioButton有一个IsChecked属性用于表示是否选中,IsChecked属性的值类型是bool,只能直接绑定bool类型的值。 然而对...

WPF 导出EXCEL 方法

是用WPF将数据导出成EXCEL其实和其他.NET应用是相通的,ASP.NET也好WINFORM也好,都是用相同的方法实现,唯一不同的是ASP.NET中可能会存在用户权限的问题,毕竟ASP.NET的执行用户是IIS指定的用户而不是默认的系统用户。 具体实现方法如下,代码中使用完整的名称空间,便于理解 第一步,不许引用Excel的程序集,不同于网上其他文章,...