在不窃取焦点的情况下吸引用户注意

我有一个程序,让用户打开几个表单。 一旦给定的事件发生(例如:30秒已经过去),我需要让用户注意触发事件的Form,而不会窃取焦点。 我已经把表格放在首位了

f.TopMost = true; 

但我想实现一些替代方案。 由于改变框架的边框颜色似乎是一项几乎不可能的任务(这个解决方案本来就是最好的),有人知道如何在不偷窃焦点的情况下获得关注吗?

选项A:您需要使用Windows API中的FlashWindowEx。 这在.NET中不可用,因此您需要使用PInvoke。

选项B:使用系统托盘中的气球提示。 它内置于.NET中,但要求您的应用程序使用您可能不需要的通知图标。 更多细节: http : //msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.showballoontip.aspx

以下是如何使用选项A的示例:

pInvoke.net就是最好的例子: http ://pinvoke.net/default.aspx/user32.FlashWindowEx

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

笔记:

 //Stop flashing. The system restores the window to its original state. public const UInt32 FLASHW_STOP = 0; //Flash the window caption. public const UInt32 FLASHW_CAPTION = 1; //Flash the taskbar button. public const UInt32 FLASHW_TRAY = 2; //Flash both the window caption and taskbar button. //This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. public const UInt32 FLASHW_ALL = 3; //Flash continuously, until the FLASHW_STOP flag is set. public const UInt32 FLASHW_TIMER = 4; //Flash continuously until the window comes to the foreground. public const UInt32 FLASHW_TIMERNOFG = 12; 

提示与技巧:

请加一些!

示例代码:

 ///  /// Flashes a window ///  /// The handle to the window to flash /// whether or not the window needed flashing public static bool FlashWindowEx(IntPtr hWnd) { 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; return FlashWindowEx(ref fInfo); } 

 /// Minor adjust to the code above ///  /// Flashes a window until the window comes to the foreground /// Receives the form that will flash ///  /// The handle to the window to flash /// whether or not the window needed flashing public static bool FlashWindowEx(Form frm) { IntPtr hWnd = frm.Handle; FLASHWINFO fInfo = new FLASHWINFO(); fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo)); fInfo.hwnd = hWnd; fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG; fInfo.uCount = UInt32.MaxValue; fInfo.dwTimeout = 0; return FlashWindowEx(ref fInfo); } 

以下是Microsoft的官方示例: http : //msdn.microsoft.com/en-us/library/ms679347(v = vs。85).aspx

  [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FlashWindowEx(ref FLASHWINFO pwfi); [StructLayout(LayoutKind.Sequential)] public struct FLASHWINFO { ///  /// The size of the structure in bytes. ///  public uint cbSize; ///  /// A Handle to the Window to be Flashed. The window can be either opened or minimized. ///  public IntPtr hwnd; ///  /// The Flash Status. ///  public FlashWindowFlags dwFlags; //uint ///  /// The number of times to Flash the window. ///  public uint uCount; ///  /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate. ///  public uint dwTimeout; } public enum FlashWindowFlags : uint { ///  /// Stop flashing. The system restores the window to its original state. ///  FLASHW_STOP = 0, ///  /// Flash the window caption. ///  FLASHW_CAPTION = 1, ///  /// Flash the taskbar button. ///  FLASHW_TRAY = 2, ///  /// Flash both the window caption and taskbar button. /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. ///  FLASHW_ALL = 3, ///  /// Flash continuously, until the FLASHW_STOP flag is set. ///  FLASHW_TIMER = 4, ///  /// Flash continuously until the window comes to the foreground. ///  FLASHW_TIMERNOFG = 12 } public static bool FlashWindow(IntPtr hWnd, FlashWindowFlags fOptions, uint FlashCount, uint FlashRate) { if(IntPtr.Zero != hWnd) { FLASHWINFO fi = new FLASHWINFO(); fi.cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO)); fi.dwFlags = fOptions; fi.uCount = FlashCount; fi.dwTimeout = FlashRate; fi.hwnd = hWnd; return FlashWindowEx(ref fi); } return false; } public static bool StopFlashingWindow(IntPtr hWnd) { if(IntPtr.Zero != hWnd) { FLASHWINFO fi = new FLASHWINFO(); fi.cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO)); fi.dwFlags = (uint)FlashWindowFlags.FLASHW_STOP; fi.hwnd = hWnd; return FlashWindowEx(ref fi); } return false; } 

在Windows 7中,表单上的进度条显示在其任务栏按钮中; 你可以利用它。 还有一种方法可以简单地突出显示任务栏按钮,就像IM程序在收到新消息时所做的那样。