简体   繁体   English

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

[英]Window application flash like orange on taskbar when minimize

I have a window application.我有一个窗口应用程序。 When I minimize the window application on taskbar to work on another application.当我最小化任务栏上的窗口应用程序以在另一个应用程序上工作时。 We hava a facility to send messages from one window application to another window application.我们有一种工具可以将消息从一个窗口应用程序发送到另一个窗口应用程序。

So my first win application is minimized and now I open my other win application and then send a message to the first application, but the first application is on the taskbar.所以我的第一个 win 应用程序被最小化,现在我打开另一个 win 应用程序,然后向第一个应用程序发送消息,但第一个应用程序在任务栏上。 So I want functionality like when any message capture my first application then it will blink like skype or any messenger.所以我想要这样的功能,比如当任何消息捕获我的第一个应用程序时,它会像 Skype 或任何信使一样闪烁。

I have tried FlashWindowEx method of User32.dll , but no luck.我尝试过User32.dll FlashWindowEx方法,但没有成功。 I have tried it with the option "Flash continuously until the window comes to the foreground."我已经尝试过使用“连续闪烁直到窗口出现在前台”选项。 but no luck.但没有运气。

Please help me solve this problem with an example.请用一个例子帮我解决这个问题。

I do it as shown below, being sure to add the required references as shown我按如下所示进行操作,确保添加所需的参考,如图所示

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

This should contain everything you need.这应该包含您需要的一切。

I hope this helps.我希望这有帮助。

C#: Flash Window in Taskbar via Win32 FlashWindowEx it works for me. C#:任务栏中的 Flash Window 通过 Win32 FlashWindowEx它对我有用

The Windows API (Win32) has the FlashWindowEx method within the User32 library; Windows API (Win32) 在 User32 库中有 FlashWindowEx 方法; this method allows you (the developer) to Flash a Window, signifying to the user that some major event occurred within the application that requires their attention.此方法允许您(开发人员)闪现一个窗口,向用户表明应用程序中发生了一些需要他们注意的重大事件。 The most common use of this is to flash the window until the user returns focus to the application.最常见的用途是使窗口闪烁,直到用户将焦点返回到应用程序。 However, you can also flash the window a specified number of times, or just keep flashing it until you decide when to stop.但是,您也可以使窗口闪烁指定的次数,或者一直闪烁直到您决定何时停止。

The use of the FlashWindowEx method however isn't built into the .NET Framework anywhere.但是,FlashWindowEx 方法的使用并未内置到 .NET Framework 的任何地方。 In order to access it you need to use the Platform Invoke (PInvoke) features of .NET to "drop" down to the Windows API (Win32) and call it directly.为了访问它,您需要使用 .NET 的平台调用 (PInvoke) 功能“下拉”到 Windows API (Win32) 并直接调用它。 Also, as with many other functionalities in the Windows API (that aren't directly exposed by .NET) the FlashWindowEx method can be a little tricky to use if you aren't familiar with working with the Windows API from within .NET.此外,与 Windows API 中的许多其他功能(不直接由 .NET 公开)一样,如果您不熟悉在 .NET 中使用 Windows API,则 FlashWindowEx 方法使用起来可能有点棘手。

Now rather than go too deep into the specifics of PInvoke or the Win32 FlashWindowEx method, below is a simple static class in C# that allows you to easily utilize this method.现在,与其深入了解 PInvoke 或 Win32 FlashWindowEx 方法的细节,不如使用 C# 中的一个简单静态类,它允许您轻松使用此方法。 There is actually quite a bit of information needed to explain how to use PInvoke to utilize the Windows API (Win32), so maybe I'll cover that in a future article.实际上需要很多信息来解释如何使用 PInvoke 来利用 Windows API (Win32),所以我可能会在以后的文章中介绍。

Here's some example usage of this static class:下面是这个静态类的一些示例用法:

 // 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);

One thing to note about the FlashWindowEx method is that it requires (and will only work on) Windows 2000 or later.关于 FlashWindowEx 方法需要注意的一件事是它需要(并且只能在)Windows 2000 或更高版本上运行。

Here's the code for the static class in C#:这是 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; } } }

I know this question is quite old, but based on MoonKnight answer I made an improvement, that some might find useful.我知道这个问题已经很老了,但根据 MoonKnight 的回答,我做了改进,有些人可能会觉得有用。 I converted it to a Form extension.我将其转换为表单扩展名。

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

To use it on a form you just need to call要在表单上使用它,您只需要调用

this.FlashNotification();

To change the way it flashes just look at this table要更改它的闪烁方式,请查看此表

Guys I found a much easier way to do it!伙计们,我找到了一种更简单的方法来做到这一点! If this is the case you're using it for.如果是这种情况,您将使用它。

In .NET 4.0, C#, if you just use在 .NET 4.0、C# 中,如果您只是使用

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

The first line makes sure it's not minimized, the second line makes it the focus.第一行确保它没有被最小化,第二行使它成为焦点。 I'm not sure exactly why (either one of them don't do this), but combined, if you show a MessageBox from this form, your program flashes orange in the taskbar!我不确定为什么(其中一个不这样做),但综合起来,如果您从此表单中显示 MessageBox,您的程序会在任务栏中闪烁橙色!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM