2009-08-21 8 views
5

私は以下のこのコードを見つけて、私のWinFormアプリケーションに実装して、ユーザーの多くが技術的には役に立たないようにしました。タスクバーのフラッシュを紛失した場合

残念ながら、何もしません。エラーや何も生成しません。それは単にそれをフラッシュにしません。

誰でも洞察力を提供できますか? Win 7(x64)& Win XP(x86)で試してみましたが、両方で同じ結果が得られました。

私はそれをそう呼んでいます - >TaskbarFlasher.FlashWindow(this);私のメインフォームから。

[DllImport("user32.dll")] 
    private extern static bool FlashWindow(IntPtr hwnd, bool bInvert); 
    [DllImport("user32.dll")] 
    private extern static IntPtr GetForegroundWindow(); 

    /// <summary> 
    /// Notifies the user that the application requests attention 
    /// by flashing the taskbar if the form is not the current window. 
    /// </summary> 
    /// <param name="myForm">The form in question.</param> 
    public static void FlashWindow(Form myForm) 
    { 
     // if the current foreground window isn't this window, 
     // flash this window in task bar once every 1 second 
     if (GetForegroundWindow() != myForm.Handle) 
     { 
      FlashWindow(myForm.Handle, true); 
     } 
    } 
+0

あなたのアプリをメールクライアントにクリックした貧しい人々に悲鳴を上げる悲鳴を上げる欲求を払うことを望むのはなぜですか? :D –

+0

実際には、すでにオープンしているか、DataEntryアプリの複数のインスタンスを実行しようとしているのを手伝う必要があります。 –

+1

本当にやりたいことは、アプリケーションの1つのインスタンスを確保することですこれは別の種類の質問です。 WindowsのUIガイドラインにフォーカスの喪失通知を悪用する(実際にはねつける)代わりに、してください、あなたが計画していることをしないでください。 –

答えて

14

ネヴァーマインド、私は以下のリンクの助けを借りてそれを考え出した - >http://pietschsoft.com/post/2009/01/26/CSharp-Flash-Window-in-Taskbar-via-Win32-FlashWindowEx.aspx

仲間から感謝Chris Pietschmann SO Wisconsinite !!

public static class FlashWindow 
{ 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 
    [StructLayout(LayoutKind.Sequential)] 
    private struct FLASHWINFO 
    { 
     /// <summary> 
     /// The size of the structure in bytes. 
     /// </summary> 
     public uint cbSize; 
     /// <summary> 
     /// A Handle to the Window to be Flashed. The window can be either opened or minimized. 
     /// </summary> 
     public IntPtr hwnd; 
     /// <summary> 
     /// The Flash Status. 
     /// </summary> 
     public uint dwFlags; 
     /// <summary> 
     /// The number of times to Flash the window. 
     /// </summary> 
     public uint uCount; 
     /// <summary> 
     /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate. 
     /// </summary> 
     public uint dwTimeout; 
    } 
    /// <summary> 
    /// Stop flashing. The system restores the window to its original stae. 
    /// </summary> 
    public const uint FLASHW_STOP = 0; 

    /// <summary> 
    /// Flash the window caption. 
    /// </summary> 
    public const uint FLASHW_CAPTION = 1; 

    /// <summary> 
    /// Flash the taskbar button. 
    /// </summary> 
    public const uint FLASHW_TRAY = 2; 

    /// <summary> 
    /// Flash both the window caption and taskbar button. 
    /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 
    /// </summary> 
    public const uint FLASHW_ALL = 3; 
    /// <summary> 
    /// Flash continuously, until the FLASHW_STOP flag is set. 
    /// </summary> 
    public const uint FLASHW_TIMER = 4; 
    /// <summary> 
    /// Flash continuously until the window comes to the foreground. 
    /// </summary> 
    public const uint FLASHW_TIMERNOFG = 12; 

    /// <summary> 
    /// Flash the spacified Window (Form) until it recieves focus. 
    /// </summary> 
    /// <param name="form">The Form (Window) to Flash.</param> 
    /// <returns></returns> 
    public static bool Flash(System.Windows.Forms.Form form) 
    { 
     // Make sure we're running under Windows 2000 or later 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 
    private static FLASHWINFO Create_FLASHWINFO(IntPtr handle, uint flags, uint count, uint timeout) 
    { 
     FLASHWINFO fi = new FLASHWINFO(); 
     fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi)); 
     fi.hwnd = handle; 
     fi.dwFlags = flags; 
     fi.uCount = count; 
     fi.dwTimeout = timeout; 
     return fi; 
    } 
    /// <summary> 
    /// Flash the specified Window (form) for the specified number of times 
    /// </summary> 
    /// <param name="form">The Form (Window) to Flash.</param> 
    /// <param name="count">The number of times to Flash.</param> 
    /// <returns></returns> 
    public static bool Flash(System.Windows.Forms.Form form, uint count) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL, count, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 
    /// <summary> 
    /// Start Flashing the specified Window (form) 
    /// </summary> 
    /// <param name="form">The Form (Window) to Flash.</param> 
    /// <returns></returns> 
    public static bool Start(System.Windows.Forms.Form form) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL, uint.MaxValue, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 
    /// <summary> 
    /// Stop Flashing the specified Window (form) 
    /// </summary> 
    /// <param name="form"></param> 
    /// <returns></returns> 
    public static bool Stop(System.Windows.Forms.Form form) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_STOP, uint.MaxValue, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 
    /// <summary> 
    /// A boolean value indicating whether the application is running on Windows 2000 or later. 
    /// </summary> 
    private static bool Win2000OrLater 
    { 
     get { return System.Environment.OSVersion.Version.Major >= 5; } 
    } 
} 
関連する問題