andySF
andySF

Reputation: 604

Taskbar location

How can i detect where the taskbar is located? I need to know for displaying my notification in the right corner. Thanks

Edit: Thank you Hans Passant. I used that with this to get location. I hope is ok.

GetTaskbarLocation(TaskbarPosition.GetTaskbarPosition());

private void GetTaskbarLocation(Rectangle rc)
{
    if (rc.X == rc.Y)
    {
        if (rc.Right < rc.Bottom)
            taskbarLocation = TaskbarLocation.Left;
        if (rc.Right > rc.Bottom)
            taskbarLocation = TaskbarLocation.Top;
    }
    if (rc.X > rc.Y)
        taskbarLocation = TaskbarLocation.Right;
    if (rc.X < rc.Y)
        taskbarLocation = TaskbarLocation.Bottom;
}

Upvotes: 6

Views: 2971

Answers (5)

tresf
tresf

Reputation: 7922

In Java, using JNA (adapted from other C# solutions above)

public static Rectangle getTaskbarPosition() throws Exception {
    APPBARDATA data = new APPBARDATA();
    data.cbSize = new WinDef.DWORD(data.size());
    WinDef.UINT_PTR retval = Shell32.INSTANCE.SHAppBarMessage(ABM_GETTASKBARPOS, data);
    if (retval == null) {
        throw new Exception("Please re-install Windows");
    }

    return new Rectangle(data.rc.left, data.rc.top, data.rc.right - data.rc.left, data.rc.bottom - data.rc.top);
}

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942267

    public static Rectangle GetTaskbarPosition() {
        var data = new APPBARDATA();
        data.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(data);
        IntPtr retval = SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
        if (retval == IntPtr.Zero) throw new Win32Exception("Please re-install Windows");
        return new Rectangle(data.rc.left, data.rc.top,
            data.rc.right - data.rc.left, data.rc.bottom - data.rc.top);

    }

    // P/Invoke goo:
    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;
    }

Upvotes: 6

Dirk Vollmar
Dirk Vollmar

Reputation: 176259

It's probably best to use the available API: NotifyIcon.ShowBalloonTip:

void Form1_DoubleClick(object sender, EventArgs e)
{
    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(20000, "Information", "This is the text",
        ToolTipIcon.Info );
}

Upvotes: 1

James
James

Reputation: 82136

The SHAppBarMessage function will return you information about the taskbar if you pass in the ABM_GETTASKBARPOS message. It has an out parameter which is a pointer to APPBARDATA that contains the screen cooridinates of the task bar. You can use to work out where on screen it is.

Upvotes: 1

Hans Olsson
Hans Olsson

Reputation: 55049

SHAppBarMessage(ABM_GETTASKBARPOS)

See the SHAppBarMessage Function and the ABM_GETTASKBARPOS Message for more info and the pinvoke page for SHAppBarMessage has a VB.Net sample that shouldn't be too difficult to translate.

Upvotes: 1

Related Questions