Linux 任务栏位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:23:50  来源:igfitidea点击:

Taskbar location

c#

提问by andySF

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

采纳答案by Hans Passant

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

回答by Hans Olsson

SHAppBarMessage(ABM_GETTASKBARPOS)

See the SHAppBarMessageFunction and the ABM_GETTASKBARPOSMessage for more info and the pinvoke page for SHAppBarMessagehas a VB.Net sample that shouldn't be too difficult to translate.

SHAppBarMessage功能和ABM_GETTASKBARPOS消息更多的信息和PInvoke的页面SHAppBarMessage具有不应该太难翻译了VB.Net样品。

回答by James

The SHAppBarMessagefunction will return you information about the taskbar if you pass in the ABM_GETTASKBARPOSmessage. It has an out parameter which is a pointer to APPBARDATAthat contains the screen cooridinates of the task bar. You can use to work out where on screen it is.

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

回答by Dirk Vollmar

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

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

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

回答by tresf

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

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