2010-05-27 15 views
11

Básicamente hice una aplicación de consola que realiza algunas tareas que dura unos minutos. Me gustaría tenerlo en la barra de tareas para avisarme cuando haya terminado de hacerlo.¿Hay alguna manera de hacer que una ventana de la consola destelle en la barra de tareas programáticamente?

+2

he aquí una solución que pueda ser de alguna ayuda: http://stackoverflow.com/questions/73162/how-to-make-the-taskbar-blink- my-application-like-messenger-does-when-a-new-messa – Zack

Respuesta

16

Al usar answer that @Zack posted y another one to find the handle of a console app se me ocurrió esto y funciona muy bien.

class Program 
{ 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 

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

    public const UInt32 FLASHW_ALL = 3; 

    static void Main(string[] args) 
    { 
     Console.WriteLine("Flashing NOW"); 
     FlashWindow(Process.GetCurrentProcess().MainWindowHandle); 
     Console.WriteLine("Press any key to continue"); 
     Console.ReadKey(); 
    } 

    private static void FlashWindow(IntPtr hWnd) 
    { 
     FLASHWINFO fInfo = new FLASHWINFO(); 

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

     FlashWindowEx(ref fInfo); 
    } 
} 
+0

jajaja, supongo que es más fácil que PInvoking obtener el identificador – Davy8

+0

Parece que hay 3 formas diferentes de obtener el identificador de ventana pero me gusta el mejor ya que no funciona t PInvocar para hacerlo así que aceptándolo. – Davy8

+0

Wow. Usé el método P/Invoke para obtener el identificador de la ventana de la consola, lol. http://msdn.microsoft.com/en-us/library/ms683175(VS.85).aspx – Zack

2

La combinación de la respuesta en la pregunta vinculada en @ comentarios y getting the hwnd of a console window using this de Zack que era capaz de conseguir que funcione. Esta es la clase que he creado:

public static class FlashWindow 
{ 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 
    [DllImport("kernel32.dll")] 
    static extern IntPtr GetConsoleWindow(); 

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

    public const UInt32 FLASHW_ALL = 3; 

    public static void Flash() 
    { 
     FLASHWINFO fInfo = new FLASHWINFO(); 

     fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo)); 
     fInfo.hwnd = GetConsoleWindow(); 
     fInfo.dwFlags = FLASHW_ALL; 
     fInfo.uCount = UInt32.MaxValue; 
     fInfo.dwTimeout = 0; 

     FlashWindowEx(ref fInfo); 
    } 
} 

No nunca deja de parpadear hasta que se ha cerrado, pero que no era importante para mis propósitos.

4

Lo leí wasn't possible to get the window handle of a console window a través de cualquier medio directo, pero parece ser bastante simple en .NET en realidad. Por lo tanto, es más o menos lo mismo que this question:

class Program 
{ 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 

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

    public const UInt32 FLASHW_STOP = 0; 
    public const UInt32 FLASHW_CAPTION = 1; 
    public const UInt32 FLASHW_TRAY = 2; 
    public const UInt32 FLASHW_ALL = 3; 
    public const UInt32 FLASHW_TIMER = 4; 
    public const UInt32 FLASHW_TIMERNOFG = 12; 

    static void Main(string[] args) 
    { 
     // Give you a few seconds to alt-tab away :) 
     Thread.Sleep(2000); 

     // Flash on the task bar, until the window becomes the foreground window. 
     // Constants for other behaviors are defined above. 
     FLASHWINFO fInfo = new FLASHWINFO(); 
     fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo)); 
     fInfo.hwnd = Process.GetCurrentProcess().MainWindowHandle; 
     fInfo.dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG; 
     fInfo.uCount = UInt32.MaxValue; 
     fInfo.dwTimeout = 0; 
     FlashWindowEx(ref fInfo); 

     // Wait for input so the app doesn't finish right away. 
     Console.ReadLine(); 
    } 
} 
Cuestiones relacionadas