我正在尝试从 WPF 应用程序获取 windows 任务栏的高度。我得到这个How do I get the taskbar's position and size?它显示了如何找到任务栏位置而不是高度。我从how can i get the height of the windows Taskbar?得到了答案上面写着
Use the Screen class. The taskbar is the difference between its Bounds and WorkingArea properties.
但没有代码示例。如果这是正确的,这应该是任务栏的高度。我做得对吗?
private int WindowsTaskBarHeight => Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
最佳答案
您应该能够使用 native SHAppBarMessage
函数来获取任务栏的大小:
public partial class MainWindow : Window
{
private const int ABM_GETTASKBARPOS = 5;
[System.Runtime.InteropServices.DllImport("shell32.dll")]
private static extern IntPtr SHAppBarMessage(int msg, ref APPBARDATA data);
private struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;
public RECT rc;
public IntPtr lParam;
}
private struct RECT
{
public int left, top, right, bottom;
}
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
APPBARDATA data = new APPBARDATA();
data.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(data);
SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
MessageBox.Show("Width: " + (data.rc.right - data.rc.left) + ", Height: " + (data.rc.bottom - data.rc.top));
}
}
关于c# - 如何在 WPF 应用程序中获取 Windows 任务栏高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54529896/