有没有办法以编程方式在任务栏中使控制台窗口闪烁

基本上我做了一个控制台应用程序,执行一些需要几分钟的任务。 我想让它在任务栏中闪现,让我知道它什么时候完成它的事情。

使用@Zack发布的答案和另一个找到控制台应用程序的句柄,我想出了这个并且效果很好。

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

我读到通过任何直接方式都无法获得控制台窗口的窗口句柄 ,但实际上它在.NET中似乎非常简单。 所以,它与这个问题几乎相同:

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

结合@Zack评论中链接的问题的答案,并使用这个获得控制台窗口的hwnd我能够使它工作。 这是我创建的类:

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

它永远不会停止闪烁直到它关闭但这对我的目的并不重要。