web-dev-qa-db-fra.com

L'application de la fenêtre clignote comme l'orange sur la barre des tâches lors de la réduction

j'ai une application de fenêtre. quand je minimise l'application de fenêtre sur la barre des tâches pour travailler sur une autre application. nous avons une facilité comme envoyer un message d'une application de fenêtre à une autre application de fenêtre

donc ma première application win est minimisée et maintenant j'ouvre une autre application win et envoie ensuite un message à la première application mais la première application est sur la barre des tâches. donc je veux des fonctionnalités comme quand n'importe quel message capture ma première application alors il clignotera comme skype ou n'importe quel messager.

j'avais essayé la méthode "FlashWindowEx" de User32.dll. mais pas de chance. j'avais essayé avec l'option "Flash continu jusqu'à ce que la fenêtre apparaisse au premier plan." mais pas de chance.

Veuillez aider à résoudre ce problème avec un exemple

Merci

39
Hardik

Je le fais comme indiqué ci-dessous, en étant sûr d'ajouter les références requises comme indiqué

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

Cela devrait contenir tout ce dont vous avez besoin.

J'espère que ça aide.

33
MoonKnight

C #: Fenêtre Flash dans la barre des tâches via Win32 FlashWindowEx cela fonctionne pour moi.

L'API Windows (Win32) possède la méthode FlashWindowEx dans la bibliothèque User32; cette méthode vous permet (au développeur) de flasher une fenêtre, signifiant à l'utilisateur qu'un événement majeur s'est produit dans l'application qui requiert son attention. L'utilisation la plus courante consiste à flasher la fenêtre jusqu'à ce que l'utilisateur revienne au focus sur l'application. Cependant, vous pouvez également flasher la fenêtre un nombre spécifié de fois, ou simplement continuer à la flasher jusqu'à ce que vous décidiez quand vous arrêter.

Cependant, l'utilisation de la méthode FlashWindowEx n'est intégrée dans le .NET Framework nulle part. Pour y accéder, vous devez utiliser les fonctionnalités Platform Invoke (PInvoke) de .NET pour "descendre" vers l'API Windows (Win32) et l'appeler directement. En outre, comme pour de nombreuses autres fonctionnalités de l'API Windows (qui ne sont pas directement exposées par .NET), la méthode FlashWindowEx peut être un peu délicate à utiliser si vous n'êtes pas familier avec l'utilisation de l'API Windows à partir de .NET.

Maintenant, plutôt que d'aller trop loin dans les spécificités de PInvoke ou de la méthode Win32 FlashWindowEx, voici une simple classe statique en C # qui vous permet d'utiliser facilement cette méthode. Il y a en fait beaucoup d'informations nécessaires pour expliquer comment utiliser PInvoke pour utiliser l'API Windows (Win32), alors je vais peut-être en parler dans un prochain article.

Voici un exemple d'utilisation de cette classe statique:

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

Une chose à noter sur la méthode FlashWindowEx est qu'elle nécessite (et ne fonctionnera que sur) Windows 2000 ou version ultérieure.

Voici le code de la classe statique en 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; }
    }
}
28
Arci

Je sais que cette question est assez ancienne, mais sur la base de la réponse de MoonKnight, j'ai apporté une amélioration, que certains pourraient trouver utile. Je l'ai converti en une extension de formulaire.

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

Pour l'utiliser sur un formulaire, il vous suffit d'appeler

this.FlashNotification();

Pour changer la façon dont il clignote, regardez juste ce tablea

2
miguelmpn