Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get windows taskbar height in WPF application?

Tags:

c#

.net

wpf

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;
like image 825
Mahbubur Rahman Avatar asked Nov 01 '25 11:11

Mahbubur Rahman


2 Answers

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

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

like image 185
Y. Parsa Avatar answered Nov 03 '25 02:11

Y. Parsa


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));
    }
}
like image 26
mm8 Avatar answered Nov 03 '25 01:11

mm8