Mahbubur Rahman Manik
Mahbubur Rahman Manik

Reputation: 5161

How do I get windows taskbar height in WPF application?

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;

Upvotes: 4

Views: 3990

Answers (2)

Y. Parsa
Y. Parsa

Reputation: 61

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

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

Upvotes: 6

mm8
mm8

Reputation: 169420

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));
    }
}

Upvotes: 4

Related Questions