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

时间:2019-02-05 07:57:33

标签: 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?得到了一个答案

  

使用Screen类。任务栏是其“边界”和“工作区域”属性之间的区别。

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

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

2 个答案:

答案 0 :(得分:2)

您应该可以使用本机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));
    }
}

答案 1 :(得分:0)

const PriceWithID = ({match}) =>
    {
        return(
          <Price image={IMGS.filter((i)=>(i.id === parseInt(match.params.pID,10)))[0]}/>
        );
    }

此代码适合我。我在Windows 10中进行了测试。