gpt4 book ai didi

c# - 在 C#/WPF 中发生特定事件时强制窗口闪烁

转载 作者:可可西里 更新时间:2023-11-01 03:13:33 34 4
gpt4 key购买 nike

我正在使用 C#/WPF 制作应用程序。在该应用程序中,如果发生特定事件,我想使窗口闪烁,以便该应用程序的用户知道发生了什么事。如何在我的 C# WPF 应用程序中获取它。

就像在 Yahoo Messenger 中一样,如果您收到一条消息,消息窗口会闪烁以吸引您的注意力,我想在我的应用程序中使用这种效果。

最佳答案

可以使用以下代码在 WPF 中以类似于 IM 通知的方式闪烁窗口和任务栏。它使用 PlatformInvoke 使用 WPF Application.Current.MainWindow

的 Win32 句柄调用 WinAPI 函数 FlashWindowEx

代码

public class FlashWindowHelper
{
private IntPtr mainWindowHWnd;
private Application theApp;

public FlashWindowHelper(Application app)
{
this.theApp = app;
}

public void FlashApplicationWindow()
{
InitializeHandle();
Flash(this.mainWindowHWnd, 5);
}

public void StopFlashing()
{
InitializeHandle();

if (Win2000OrLater)
{
FLASHWINFO fi = CreateFlashInfoStruct(this.mainWindowHWnd, FLASHW_STOP, uint.MaxValue, 0);
FlashWindowEx(ref fi);
}
}

private void InitializeHandle()
{
if (this.mainWindowHWnd == IntPtr.Zero)
{
// Delayed creation of Main Window IntPtr as Application.Current passed in to ctor does not have the MainWindow set at that time
var mainWindow = this.theApp.MainWindow;
this.mainWindowHWnd = new System.Windows.Interop.WindowInteropHelper(mainWindow).Handle;
}
}

[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="hwnd"></param>
/// <returns></returns>
public static bool Flash(IntPtr hwnd)
{
// Make sure we're running under Windows 2000 or later
if (Win2000OrLater)
{
FLASHWINFO fi = CreateFlashInfoStruct(hwnd, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0);

return FlashWindowEx(ref fi);
}
return false;
}

private static FLASHWINFO CreateFlashInfoStruct(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="hwnd">The handle of the Window to Flash.</param>
/// <param name="count">The number of times to Flash.</param>
/// <returns></returns>
public static bool Flash(IntPtr hwnd, uint count)
{
if (Win2000OrLater)
{
FLASHWINFO fi = CreateFlashInfoStruct(hwnd, FLASHW_ALL | FLASHW_TIMERNOFG, count, 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 Environment.OSVersion.Version.Major >= 5; }
}
}

用法

var helper = new FlashWindowHelper(Application.Current);

// Flashes the window and taskbar 5 times and stays solid
// colored until user focuses the main window
helper.FlashApplicationWindow();

// Cancels the flash at any time
helper.StopFlashing();

关于c# - 在 C#/WPF 中发生特定事件时强制窗口闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924556/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com