2009-08-21 1 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

그런 다음 정말로 원하는 것은 응용 프로그램의 단일 인스턴스를 보장하는 것입니다 이것은 다른 종류의 질문입니다. 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; } 
    } 
}