C# 如何在失去焦点时使任务栏闪烁
我无意中发现了下面的代码,并试图在我的WinForm应用程序中实现它,以帮助我的用户,因为很多人都不是很懂技术 不幸的是,它什么也没做。它不会产生任何错误或任何东西。它只是不能让它闪光 有人能提供一些见解吗?我曾在Win7(x64)和WinXP(x86)上试用过它,在这两个平台上的效果都相同 我这样称呼它-->C# 如何在失去焦点时使任务栏闪烁,c#,winforms,interop,dllimport,taskbar,C#,Winforms,Interop,Dllimport,Taskbar,我无意中发现了下面的代码,并试图在我的WinForm应用程序中实现它,以帮助我的用户,因为很多人都不是很懂技术 不幸的是,它什么也没做。它不会产生任何错误或任何东西。它只是不能让它闪光 有人能提供一些见解吗?我曾在Win7(x64)和WinXP(x86)上试用过它,在这两个平台上的效果都相同 我这样称呼它-->TaskbarFlasher.FlashWindow(这个)来自我的主窗体 [DllImport("user32.dll")] private extern static bool
TaskbarFlasher.FlashWindow(这个)代码>来自我的主窗体
[DllImport("user32.dll")]
private extern static bool FlashWindow(IntPtr hwnd, bool bInvert);
[DllImport("user32.dll")]
private extern static IntPtr GetForegroundWindow();
/// <summary>
/// Notifies the user that the application requests attention
/// by flashing the taskbar if the form is not the current window.
/// </summary>
/// <param name="myForm">The form in question.</param>
public static void FlashWindow(Form myForm)
{
// if the current foreground window isn't this window,
// flash this window in task bar once every 1 second
if (GetForegroundWindow() != myForm.Handle)
{
FlashWindow(myForm.Handle, true);
}
}
[DllImport(“user32.dll”)]
专用外部静态布尔FlashWindow(IntPtr hwnd、布尔bInvert);
[DllImport(“user32.dll”)]
私有外部静态IntPtr GetForegroundWindow();
///
///通知用户应用程序请求注意
///如果窗体不是当前窗口,则闪烁任务栏。
///
///有问题的表格。
公共静态void FlashWindow(表单myForm)
{
//如果当前前景窗口不是此窗口,
//每1秒在任务栏中刷新此窗口一次
if(GetForegroundWindow()!=myForm.Handle)
{
FlashWindow(myForm.Handle,true);
}
}
没关系,我通过下面的帮助链接找到了答案-->
来自威斯康辛州一位如此之的家伙的感谢
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; }
}
}
公共静态类FlashWindow
{
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
专用静态外部布尔FlashWindowEx(参考FLASHWINFO pwfi);
[StructLayout(LayoutKind.Sequential)]
私有结构FLASHWINFO
{
///
///结构的大小(以字节为单位)。
///
公共单位cbSize;
///
///要刷新的窗口的句柄。可以打开或最小化窗口。
///
公共IntPtr hwnd;
///
///闪存状态。
///
公共旗帜;
///
///窗口闪烁的次数。
///
公共单位账户;
///
///窗口闪烁的速率,以毫秒为单位。如果为零,则函数使用默认的光标闪烁速率。
///
公共uint超时;
}
///
///停止闪烁。系统将窗口恢复到其原始状态。
///
公共警察闪光停止=0;
///
///刷新窗口标题。
///
公共警察FLASHW_字幕=1;
///
///闪烁任务栏按钮。
///
公共警用闪蒸盘=2;
///
///闪烁窗口标题和任务栏按钮。
///这相当于设置FLASHW_标题| FLASHW_托盘标志。
///
公共警察闪现全部=3;
///
///持续闪烁,直到设置了FLASHW_停止标志。
///
公共警察闪光灯定时器=4;
///
///持续闪烁,直到窗口出现在前景。
///
公共警察闪光时间=12;
///
///闪烁指定的窗口(窗体),直到收到焦点。
///
///要闪烁的窗体(窗口)。
///
公共静态bool闪存(System.Windows.Forms.Form)
{
//确保我们在Windows 2000或更高版本下运行
如果(Win2000OrLater)
{
FLASHWINFO fi=Create_FLASHWINFO(form.Handle,FLASHW_ALL | FLASHW_TIMERNOFG,uint.MaxValue,0);
返回FlashWindowEx(参考fi);
}
返回false;
}
私有静态FLASHWINFO创建\u FLASHWINFO(IntPtr句柄、uint标志、uint计数、uint超时)
{
FLASHWINFO fi=新FLASHWINFO();
fi.cbSize=Convert.ToUInt32(Marshal.SizeOf(fi));
fi.hwnd=手柄;
fi.dwFlags=标志;
fi.uCount=计数;
fi.dwTimeout=超时;
返回fi;
}
///
///按指定次数闪烁指定窗口(窗体)
///
///要闪烁的窗体(窗口)。
///闪烁的次数。
///
公共静态布尔闪存(System.Windows.Forms.Form表单,uint计数)
{
如果(Win2000OrLater)
{
FLASHWINFO fi=Create_FLASHWINFO(form.Handle,FLASHW_ALL,count,0);
返回FlashWindowEx(参考fi);
}
返回false;
}
///
///开始闪烁指定的窗口(窗体)
///
///要闪烁的窗体(窗口)。
///
公共静态bool启动(System.Windows.Forms.Form)
{
如果(Win2000OrLater)
{
FLASHWINFO fi=Create_FLASHWINFO(form.Handle,FLASHW_ALL,uint.MaxValue,0);
返回FlashWindowEx(参考fi);
}
返回false;
}
///
///停止闪烁指定的窗口(窗体)
///
///
///
公共静态布尔停止(System.Windows.Forms.Form)
{
如果(Win2000OrLater)
{
FLASHWINFO fi=Create_FLASHWINFO(form.Handle,FLASHW_STOP,uint.MaxValue,0);
返回FlashWindowEx(参考fi);
}
返回false;
}
///
///一个布尔值,指示应用程序是否在Windows 2000或更高版本上运行。
///
专用静态布尔Win2000OrLater
{
获取{return System.Environment.OSVersion.Version.Major>=5;}
}
}
你为什么要对从你的应用程序点击到他们的邮件客户端的可怜的灵魂释放尖叫闪烁的死亡。。。或者,病毒扫描器?:实际上,我需要它来帮助他们看到它已经打开了,或者我会让他们尝试运行一个DataEntry应用程序的多个实例……那么你真正想做的是确保你的应用程序只有一个实例,这是另一种问题。而不是滥用(并真正搞砸)WindowsUI准则和焦点丢失通知。请不要做您计划的事情。我正在阻止多个实例,但我仍然需要某种方法将它们引导到现有实例。正如我所说,我有一个非常不精明的内部用户群。由;不是吗