最小化时,窗口应用程序在任务栏上闪烁橙色

时间:2012-07-03 11:20:09

标签: c# asp.net winforms winapi

我有一个窗口应用程序。当我最小化任务栏上的窗口应用程序以在另一个应用程序上工作。我们有一个设施,比如从一个窗口应用程序发送消息到另一个窗口应用程序

所以我的第一个获胜应用程序是最小化,现在我打开我的另一个获胜应用程序然后向第一个应用程序发送消息,但第一个应用程序在任务栏上。 所以我想要的功能就像任何消息捕获我的第一个应用程序然后它会像skype或任何信使一样闪烁。

我曾尝试过User32.dll的“FlashWindowEx”方法。但没有运气。我试过选择“连续闪光直到窗口到达前景”。但没有运气。

请用示例

帮助解决此问题

由于

4 个答案:

答案 0 :(得分:29)

我这样做,如下所示,确保添加所需的引用,如图所示

using System.Runtime.InteropServices;
using Microsoft.Win32;

// To support flashing.
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

//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 window comes to the foreground. 
public const UInt32 FLASHW_TIMERNOFG = 12;

[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
    public UInt32 cbSize;
    public IntPtr hwnd;
    public UInt32 dwFlags;
    public UInt32 uCount;
    public UInt32 dwTimeout;
}

// Do the flashing - this does not involve a raincoat.
public static bool FlashWindowEx(Form form)
{
    IntPtr hWnd = form.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);
}

这应包含您需要的一切。

我希望这会有所帮助。

答案 1 :(得分:26)

C#: Flash Window in Taskbar via Win32 FlashWindowEx它对我有用。

  

Windows API(Win32)在User32中具有FlashWindowEx方法   图书馆;这个方法允许你(开发人员)Flash一个窗口,   向用户表示发生了一些重大事件   需要他们注意的应用程序。最常见的用途   是闪烁窗口,直到用户将焦点返回到   应用。但是,您也可以将窗口闪烁指定的数字   时间,或者只是一直闪烁,直到你决定何时停止。

     

然而,FlashWindowEx方法的使用并未内置于.NET中   任何框架。要访问它,您需要使用平台   调用.NET的(PInvoke)功能以“下载”到Windows API   (Win32)并直接调用它。此外,与许多其他功能一样   在Windows API中(不是由.NET直接公开)   如果不是,FlashWindowEx方法可能有点棘手   熟悉从.NET中使用Windows API。

     

现在而不是深入了解PInvoke或Win32的细节   FlashWindowEx方法,下面是C#中允许的简单静态类   你很容易使用这种方法。实际上有很多   解释如何使用PInvoke来使用的信息   Windows API(Win32),所以我将在以后的文章中介绍它。

     

以下是此静态类的一些示例用法:

     
// One this to note with this example usage code, is the "this" keyword is referring to
// the current System.Windows.Forms.Form.

// Flash window until it recieves focus
FlashWindow.Flash(this);

// Flash window 5 times
FlashWindow.Flash(this, 5);

// Start Flashing "Indefinately"
FlashWindow.Start(this);

// Stop the "Indefinate" Flashing
FlashWindow.Stop(this);
     

有关FlashWindowEx方法需要注意的一点是   (仅适用于Windows 2000或更高版本。

     

以下是C#中静态类的代码:

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

答案 2 :(得分:2)

我知道这个问题很老了,但根据MoonKnight的回答,我做了一些改进,有些可能会觉得有用。 我将其转换为表格扩展名。

public static class ExtensionMethods
{
    // To support flashing.
    [DllImport("user32.dll", CallingConvention = CallingConvention.Cdecl)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

    //Flash both the window caption and taskbar button.
    //This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 
    private const uint FLASHW_ALL = 3;

    // Flash continuously until the window comes to the foreground. 
    private const uint FLASHW_TIMERNOFG = 12;

    [StructLayout(LayoutKind.Sequential)]
    private struct FLASHWINFO
    {
        public uint cbSize;
        public IntPtr hwnd;
        public uint dwFlags;
        public uint uCount;
        public uint dwTimeout;
    }

    /// <summary>
    /// Send form taskbar notification, the Window will flash until get's focus
    /// <remarks>
    /// This method allows to Flash a Window, signifying to the user that some major event occurred within the application that requires their attention. 
    /// </remarks>
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    public static bool FlashNotification(this Form form)
    {
        IntPtr hWnd = form.Handle;
        FLASHWINFO fInfo = new FLASHWINFO();

        fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
        fInfo.hwnd = hWnd;
        fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
        fInfo.uCount = uint.MaxValue;
        fInfo.dwTimeout = 0;

        return FlashWindowEx(ref fInfo);
    }
}

要在表单上使用它,您只需要调用

this.FlashNotification();

要更改闪烁的方式,请查看this table

答案 3 :(得分:-1)

伙计们我找到了一种更简单的方法!如果是这种情况,你就是用它。

在.NET 4.0,C#中,如果你只是使用

this.WindowState = FormWindowState.Normal;
this.Activate();

第一行确保它没有最小化,第二行使它成为焦点。我不确定为什么(其中一个不这样做),但是如果你从这个表单中显示一个MessageBox,你的程序会在任务栏中闪烁橙色!

相关问题