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;
}
andySF
  • 556
  • 7
  • 30

5 Answers5

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;
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    I'm not sure this code is complete. [MSDN has the following in the documentation for `ABM_GETTASKBARPOS`](http://msdn.microsoft.com/en-us/library/windows/desktop/bb787949%28v=vs.85%29.aspx): *You must specify the cbSize **and hWnd** when sending this message;* [This](http://www.pinvoke.net/default.aspx/shell32.shappbarmessage) and [this](http://edn.embarcadero.com/article/26977) both look for a window called `Shell_TrayWnd` to use as the `hWnd`. – ta.speot.is Nov 30 '13 at 10:22
  • @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](http://stackoverflow.com/questions/20298740/issue-with-shappbarmessage-for-taskbar-position-on-windows-server-2008r2/20298931#20298931) with very similar code. Which is how I came across this answer. – ta.speot.is Nov 30 '13 at 11:58
  • Has taskbar looks strange, 2008R2 should have the round Start button orb. The exception message might be appropriate. – Hans Passant Nov 30 '13 at 12:08
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.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
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.

James
  • 80,725
  • 18
  • 167
  • 237
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 );
}
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
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);
}
tresf
  • 7,103
  • 6
  • 40
  • 101