【问题标题】:How to make Taskbar Flash on Lost Focus如何使任务栏在失去焦点时闪烁
【发布时间】:2009-08-21 15:03:48
【问题描述】:

我偶然发现了下面的这段代码,并尝试在我的 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);
        }
    }

【问题讨论】:

  • 你为什么要对从你的应用程序点击到他们的邮件客户端的可怜的灵魂释放尖叫闪烁的死亡......或者也许是病毒扫描程序? :D
  • 实际上,我需要它来帮助他们看到它已经打开,否则我会让他们尝试运行 DataEntry 应用程序的多个实例...
  • 那么你真正想做的是确保应用程序的单个实例,这是另一种问题。而不是通过失去焦点通知来滥用(并真正搞砸)Windows UI 指南。请不要按照你的计划去做。
  • 我正在阻止多个实例,但我仍然需要某种方式将它们引导到现有实例。正如我所说,我有一个非常不精通的内部用户群。靠靠; MSN Messenger* 有时不会在任务栏中闪烁吗?我绝对不提倡它,但它似乎满足了我的需求。你们中的任何一个都有其他建议吗?谢谢
  • 我有类似的情况:我正在使用互斥锁来查看实例是否唯一,如果不是,我使用 IPC 向现有客户端发送消息以获取它激活并显示自己。

标签: c# winforms interop dllimport taskbar


【解决方案1】:

没关系,我通过以下链接帮助解决了问题 --> http://pietschsoft.com/post/2009/01/26/CSharp-Flash-Window-in-Taskbar-via-Win32-FlashWindowEx.aspx

感谢一位 SO 威斯康星人Chris Pietschmann!!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    相关资源
    最近更新 更多