c# - 如何在 WPF 应用程序中获取 Windows 任务栏高度?

标签 c# .net wpf

我正在尝试从 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/

相关文章:

c# - 是否可以重载窗体的 ShowDialog 方法并返回不同的结果?

c# - 在 .Net Core 中使用 app.config

c# - .NET反射的 "cost"是什么?

c# - 使用 MVVM 模式正确实现 WPF 自定义 MessageBox

wpf - WPF 中的 .NET Framework 4 不显示位图效果

javascript - 从 asp.net 和 Ajax 的列表中删除自动完成选定的项目

c# - 如何将自定义 header 添加到 System.Net.Mail SMTP 类?

c# - 保持控制始终位于首位

.net - 使 Infragistics UltraGrid 列具有 "Spring"行为

c# - 属性更改时在ViewModel中执行方法