Taskbar location
Asked Answered
P

5

6

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;
}
Pelias answered 9/9, 2010 at 13:51 Comment(0)
H
6
    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;
    }
Handoff answered 9/9, 2010 at 14:4 Comment(3)
I'm not sure this code is complete. MSDN has the following in the documentation for ABM_GETTASKBARPOS: You must specify the cbSize and hWnd when sending this message; This and this both look for a window called Shell_TrayWnd to use as the hWnd.Maori
@HansPassant I thought it was redundant but on Win8 you can have more than one task bar. And this guy seems to be having some problems with very similar code. Which is how I came across this answer.Maori
Has taskbar looks strange, 2008R2 should have the round Start button orb. The exception message might be appropriate.Handoff
M
1
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.

Mizzen answered 9/9, 2010 at 13:58 Comment(0)
I
1

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.

Ileus answered 9/9, 2010 at 13:59 Comment(0)
B
1

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 );
}
Begrudge answered 9/9, 2010 at 14:2 Comment(1)
for notification I'm using a form :) i should mention that. thanksPelias
I
1

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);
}
Ileanaileane answered 24/3, 2020 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.