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

Har*_*dik 41 c# asp.net winapi winforms

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

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

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

请举例帮助解决这个问题

谢谢

Moo*_*ght 36

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

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);
}
Run Code Online (Sandbox Code Playgroud)

这应该包含您需要的一切.

我希望这有帮助.

  • 我试过这种方式,但它不起作用,我有窗口7 os. (3认同)

Arc*_*rci 29

C#:任务栏中的Flash窗口通过Win32 FlashWindowEx它适用于我.

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

但是,FlashWindowEx方法的使用并没有内置在.NET Framework中.要访问它,您需要使用.NET的平台调用(PInvoke)功能"删除"到Windows API(Win32)并直接调用它.此外,与Windows API中的许多其他功能(不直接由.NET公开)一样,如果您不熟悉在.NET中使用Windows API,则使用FlashWindowEx方法会有点棘手.

现在,不要过于深入了解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);
Run Code Online (Sandbox Code Playgroud)

关于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; }
    }
}
Run Code Online (Sandbox Code Playgroud)


mig*_*mpn 6

我知道这个问题已经很老了,但是根据 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

this.FlashNotification();
Run Code Online (Sandbox Code Playgroud)

要改变它的闪烁方式只需查看此表

  • 在删除 DLLImport 属性中的 CallingConvention 后,效果很好。在此之前,它抱怨签名不匹配。 (3认同)