在通知区域附近打开WPF窗口,支持多个监视器
本文关键字:支持 监视器 窗口 WPF 区域 通知 近打 | 更新日期: 2023-09-27 18:19:25
我的wpf应用程序应该在通知区域附近打开一个窗口。当用户点击托盘图标打开一个窗口。所以我需要在通知区域的同一屏幕上的窗口。现在我只是得到任务栏的位置:https://stackoverflow.com/a/8581546/1502011任务栏矩形在屏幕上https://stackoverflow.com/a/3677319/1502011然后我用它来定位我的窗口,像:
//xaml.cs
var taskBarLocation = GetTaskBarLocation();
var taskBarPosition = GetTaskbarPosition();
this.Left = taskBarLocation == Location.Left
? taskBarPosition.Width
: SystemParameters.WorkArea.Width - Width;
this.Top = taskBarLocation == Location.Top
? taskBarPosition.Height
: SystemParameters.WorkArea.Height - Height;
只有当通知区域放在主屏幕上时才有效。正如我所理解的,我应该使用WinApi将我的窗口放在其他监视器上,但我甚至不知道如何找到带有通知区域的屏幕。
Ok, since
1) Wpf窗口位置不关心显示器(它在"控制面板'所有控制面板项'显示'屏幕分辨率"坐标上工作,而不是在当前屏幕坐标上工作)
2) WinApi方法SHAppBarMessage(5, ref data)也适用于全局坐标
3)每个屏幕对象都有Bounds属性,表示屏幕在全局坐标处的位置
解决方法很简单:
public static class TaskBarLocationProvider
{
// P/Invoke goo:
private const int ABM_GETTASKBARPOS = 5;
[DllImport("shell32.dll")]
private static extern IntPtr SHAppBarMessage(int msg, ref AppBarData data);
/// <summary>
/// Where is task bar located (at top of the screen, at bottom (default), or at the one of sides)
/// </summary>
private enum Dock
{
Left = 0,
Top = 1,
Right = 2,
Bottom = 3
}
private struct Rect
{
public int Left, Top, Right, Bottom;
}
private struct AppBarData
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public Dock Dock;
public Rect rc;
public IntPtr lParam;
}
private static Rectangle GetTaskBarCoordinates(Rect rc)
{
return new Rectangle(rc.Left, rc.Top,
rc.Right - rc.Left, rc.Bottom - rc.Top);
}
private static AppBarData GetTaskBarLocation()
{
var data = new AppBarData();
data.cbSize = Marshal.SizeOf(data);
IntPtr retval = SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
if (retval == IntPtr.Zero)
{
throw new Win32Exception("WinAPi Error: does'nt work api method SHAppBarMessage");
}
return data;
}
private static Screen FindScreenWithTaskBar(Rectangle taskBarCoordinates)
{
foreach (var screen in Screen.AllScreens)
{
if (screen.Bounds.Contains(taskBarCoordinates))
{
return screen;
}
}
return Screen.PrimaryScreen;
}
/// <summary>
/// Calculate wpf window position for place it near to taskbar area
/// </summary>
/// <param name="windowWidth">target window height</param>
/// <param name="windowHeight">target window width</param>
/// <param name="left">Result left coordinate <see cref="System.Windows.Window.Left"/></param>
/// <param name="top">Result top coordinate <see cref="System.Windows.Window.Top"/></param>
public static void CalculateWindowPositionByTaskbar(double windowWidth, double windowHeight, out double left, out double top)
{
var taskBarLocation = GetTaskBarLocation();
var taskBarRectangle = GetTaskBarCoordinates(taskBarLocation.rc);
var screen = FindScreenWithTaskBar(taskBarRectangle);
left = taskBarLocation.Dock == Dock.Left
? screen.Bounds.X + taskBarRectangle.Width
: screen.Bounds.X + screen.WorkingArea.Width - windowWidth;
top = taskBarLocation.Dock == Dock.Top
? screen.Bounds.Y + taskBarRectangle.Height
: screen.Bounds.Y + screen.WorkingArea.Height - windowHeight;
}
和在xaml.cs中的用法
private void SetWindowPosition()
{
double left, right;
TaskBarLocationProvider.CalculateWindowPositionByTaskbar(this.Width, this.Height, out left, out right);
Left = left;
Top = right;
}