任务栏位置

时间:2010-09-09 13:51:01

标签: c#

如何检测任务栏的位置?我需要知道在右上角显示我的通知。感谢

编辑: 谢谢Hans Passant。我用它来获取位置。我希望没问题。

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

5 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:1)

SHAppBarMessage(ABM_GETTASKBARPOS)

请参阅SHAppBarMessage函数和ABM_GETTASKBARPOS消息以获取更多信息,SHAppBarMessage的pinvoke页面包含一个不易翻译的VB.Net示例。

答案 2 :(得分:1)

如果您传递SHAppBarMessage消息,ABM_GETTASKBARPOS函数将返回有关任务栏的信息。它有一个out参数,它是一个指向APPBARDATA的指针,它包含任务栏的屏幕协调。你可以用来计算它在屏幕上的位置。

答案 3 :(得分:1)

最好使用可用的API:NotifyIcon.ShowBalloonTip

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

答案 4 :(得分:0)

在Java中,使用JNA(改编自上述其他C#解决方案)

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