【问题标题】:Taskbar location任务栏位置
【发布时间】:2010-09-09 13:51:01
【问题描述】:

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

编辑: 谢谢汉斯·帕桑特。我用它来获取位置。希望没事。

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

【问题讨论】:

    标签: c#


    【解决方案1】:
        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;
        }
    

    【讨论】:

    【解决方案2】:
    SHAppBarMessage(ABM_GETTASKBARPOS)
    

    请参阅SHAppBarMessage 函数和ABM_GETTASKBARPOS 消息了解更多信息,SHAppBarMessage 的 pinvoke 页面有一个 VB.Net 示例,翻译起来应该不会太难。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 通知我正在使用表格:) 我应该提到这一点。谢谢
        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-21
          相关资源
          最近更新 更多