3

I am trying to get the height of windows taskbar from a WPF application. I got this How do I get the taskbar's position and size? which shows how to find taskbar position but not the height. I got an answer from how can i get the height of the windows Taskbar? which says

Use the Screen class. The taskbar is the difference between its Bounds and WorkingArea properties.

but no code example. If that is correct this should be the height of taskbar. Am I doing it right?

private int WindowsTaskBarHeight => Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;
Mahbubur Rahman
  • 4,961
  • 2
  • 39
  • 46
  • Why do you want to get the Taskbar height? It sounds like you actually want to achive something different – Denis Schaf Feb 05 '19 at 08:02
  • 2
    Are you sure the taskbar is not set to auto hide? – preciousbetine Feb 05 '19 at 08:06
  • @preciousbetine not sure about that. – Mahbubur Rahman Feb 05 '19 at 08:12
  • @DenisSchaf I need this for a UI related issue actually. – Mahbubur Rahman Feb 05 '19 at 08:16
  • 1
    This will only work for PrimaryScreen though. If you want the size of any screen, then: https://stackoverflow.com/a/2118993/891715 If your only concern is PrimaryScreen though, you may use System.Windows.SystemParameters.PrimaryScreenWidth (so you don't have to add reference to System.Windows.Forms). Also, user can have the taskbar on the side... – Arie Feb 05 '19 at 08:17

2 Answers2

4
var toolbarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.FullPrimaryScreenHeight - SystemParameters.WindowCaptionHeight;

This code worked right for me. I test it in windows 10.

Y. Parsa
  • 41
  • 2
3

You should be able to use the native SHAppBarMessage function to get the size of the taskbar:

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));
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88