【问题标题】:How do I get windows taskbar height in WPF application?如何在 WPF 应用程序中获取 Windows 任务栏高度?
【发布时间】:2019-02-05 07:57:32
【问题描述】:

我正在尝试从 WPF 应用程序获取 Windows 任务栏的高度。我得到了这个How do I get the taskbar's position and size?,它显示了如何找到任务栏位置而不是高度。我得到了how can i get the height of the windows Taskbar? 的答复,上面写着

使用 Screen 类。任务栏是其 Bounds 和 WorkingArea 属性的区别。

但没有代码示例。如果这是正确的,这应该是任务栏的高度。我做得对吗?

private int WindowsTaskBarHeight => Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;

【问题讨论】:

  • 为什么要获取任务栏高度?听起来您实际上想要实现不同的目标
  • 您确定任务栏没有设置为自动隐藏吗?
  • @preciousbetine 不确定。
  • @DenisSchaf 实际上我需要这个来解决与 UI 相关的问题。
  • 这仅适用于 PrimaryScreen。如果您想要任何屏幕的大小,那么:stackoverflow.com/a/2118993/891715 如果您唯一关心的是 PrimaryScreen,您可以使用 System.Windows.SystemParameters.PrimaryScreenWidth(因此您不必添加对 System.Windows.Forms 的引用)。此外,用户可以将任务栏放在一边...

标签: c# .net wpf


【解决方案1】:

你应该可以使用原生的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));
    }
}

【讨论】:

  • 是的。我试过了。结果和我的代码一样。
  • 结果是?
  • 结果正确。但始终从主监视器获取高度(如果是多个监视器)。
【解决方案2】:
var toolbarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.FullPrimaryScreenHeight - SystemParameters.WindowCaptionHeight;

这段代码适合我。我在 Windows 10 中对其进行了测试。

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 2021-03-28
    • 2012-01-27
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多