C# 窗口程序运行期间,修改图标,模拟闪动的效果
C# Console Windows icon change 。 Blink icon。
C# 窗口程序运行期间,修改图标,模拟出图标闪动的效果。Blink icon!
in your Program class:
[DllImport("user32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( IntPtr hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter int lParam // second message parameter ); [DllImport("Kernel32.dll", EntryPoint = "GetConsoleWindow")] public static extern IntPtr GetConsoleWindow(); private const int STD_OUTPUT_HANDLE = -11; private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); private const int WM_SETICON = 0x0080; static bool gline = true; //Creat blink thread in your program main() //Thread thread = new Thread(new ThreadStart(BlinkConsoleWindow)); & start the thread public static void BlinkConsoleWindow() { string a_char = "|"; while (true) { Console.Title = "My App Running! " + a_char; //显示旋转的竖线 whirl | Thread.Sleep(300); switch (a_char) { case "|": a_char = "/"; break; case "/": a_char = "-"; break; case "-": a_char = @"\"; break; case @"\": a_char = "|"; break; default: a_char = "="; break; } //闪烁Console窗口图标 IntPtr hnd = GetConsoleWindow(); IntPtr icon ; if (gline) { icon = BuildBin.Properties.Resources._1.Handle; //需要创建资源文件,并将图标放到资源文件中 } else { icon = BuildBin.Properties.Resources._2.Handle; //Create Resources file,& put your icon in } gline = !gline; if (hnd != INVALID_HANDLE_VALUE) { SendMessage(hnd, WM_SETICON, 0, (int) icon); //发送更替图标的消息 } } }