Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF应用程序中获取windows任务栏高度?_C#_.net_Wpf - Fatal编程技术网

C# 如何在WPF应用程序中获取windows任务栏高度?

C# 如何在WPF应用程序中获取windows任务栏高度?,c#,.net,wpf,C#,.net,Wpf,我正在尝试从WPF应用程序获取windows任务栏的高度。我得到了这个,它显示了如何找到任务栏的位置,而不是高度。我得到的答案是 使用Screen类。任务栏是其边界和WorkingArea属性之间的差异 但是没有代码示例。如果这是正确的,这应该是任务栏的高度。我做得对吗 private int WindowsTaskBarHeight => Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Heigh

我正在尝试从WPF应用程序获取windows任务栏的高度。我得到了这个,它显示了如何找到任务栏的位置,而不是高度。我得到的答案是

使用Screen类。任务栏是其边界和WorkingArea属性之间的差异

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

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

您应该能够使用本机的
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));
    }
}

这个代码对我来说很有效。我在windows 10中测试它。

为什么要获得任务栏高度?听起来你真的想获得一些不同的东西。你确定任务栏没有设置为自动隐藏吗?@preciousbetine不确定。@DenisChaf我需要这个来解决一个与UI相关的问题。但这只适用于PrimaryScreen。如果您想要任何屏幕的大小,那么:如果您唯一关心的是PrimaryScreen,那么您可以使用System.Windows.SystemParameters.PrimaryScreenWidth(这样您就不必添加对System.Windows.Forms的引用)。此外,用户可以将任务栏放在旁边…是的。我试过了。结果和我的代码一样。结果是?结果是正确的。但始终从主监视器获取高度(在多个监视器的情况下)。
var toolbarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.FullPrimaryScreenHeight - SystemParameters.WindowCaptionHeight;