将表单放置在单击的通知图标上方

发布于 2024-12-02 13:52:40 字数 58 浏览 2 评论 0原文

有没有办法将窗体放置在 Windows 7 和 Windows Vista 中单击的“通知”图标上方?

Is there a way to position a form just above the clicked Notify Icon in windows 7 and windows Vista?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别挽留 2024-12-09 13:52:40

这是一个更简单的方法。

当 OnClick 事件触发时,您可以获取鼠标的 X,Y 位置。
您还可以通过从这些对象 Screen.PrimaryScreen.BoundsScreen.PrimaryScreen.WorkingArea 进行一些检查来获取任务栏位置。

    private void OnTrayClick(object sender, EventArgs e)
    {
        _frmMain.Left = Cursor.Position.X;
        _frmMain.Top = Screen.PrimaryScreen.WorkingArea.Bottom -_frmMain.Height;
        _frmMain.Show();        
    }

Here is an easier way.

You can get X,Y position of the mouse when the OnClick event is fired.
You can also get the taskbar position with somes checks from these objects Screen.PrimaryScreen.Bounds, Screen.PrimaryScreen.WorkingArea.

    private void OnTrayClick(object sender, EventArgs e)
    {
        _frmMain.Left = Cursor.Position.X;
        _frmMain.Top = Screen.PrimaryScreen.WorkingArea.Bottom -_frmMain.Height;
        _frmMain.Show();        
    }
青春有你 2024-12-09 13:52:40

关于您的评论:“我怎么知道任务栏是如何定位的?”

查看以下文章,其中包含一个类,该类公开了用于检索 托盘的矩形结构[c#] NotifyIcon - 检测 MouseOut

使用此类,您可以检索 托盘的矩形结构,如下所示:

Rectangle trayRectangle = WinAPI.GetTrayRectangle();

它将为您提供托盘的顶部、左侧、右侧和底部坐标及其宽度和高度。

我已将以下课程包括在内:

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.ComponentModel;

public class WinAPI
{
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public override string ToString()
        {
            return "(" + left + ", " + top + ") --> (" + right + ", " + bottom + ")";
        }
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);


    public static IntPtr GetTrayHandle()
    {
        IntPtr taskBarHandle = WinAPI.FindWindow("Shell_TrayWnd", null);
        if (!taskBarHandle.Equals(IntPtr.Zero))
        {
            return WinAPI.FindWindowEx(taskBarHandle, IntPtr.Zero, "TrayNotifyWnd", IntPtr.Zero);
        }
        return IntPtr.Zero;
    }

    public static Rectangle GetTrayRectangle()
    {
        WinAPI.RECT rect;
        WinAPI.GetWindowRect(WinAPI.GetTrayHandle(), out rect);
        return new Rectangle(new Point(rect.left, rect.top), new Size((rect.right - rect.left) + 1, (rect.bottom - rect.top) + 1));
    }
}

希望这会有所帮助。

Regarding your comment: "how could i know how is the taskbar positioned?"

Check out the following article which contains a class which exposes a method for retrieving a Rectangle Structure for the tray: [c#] NotifyIcon - Detect MouseOut

Using this class you can retrieve the Rectangle Structure for the tray like so:

Rectangle trayRectangle = WinAPI.GetTrayRectangle();

Which will provide you with the Top, Left, Right and Bottom coordinates for the tray along with its width and height.

I have included the class below:

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.ComponentModel;

public class WinAPI
{
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;

        public override string ToString()
        {
            return "(" + left + ", " + top + ") --> (" + right + ", " + bottom + ")";
        }
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr FindWindow(string strClassName, string strWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);


    public static IntPtr GetTrayHandle()
    {
        IntPtr taskBarHandle = WinAPI.FindWindow("Shell_TrayWnd", null);
        if (!taskBarHandle.Equals(IntPtr.Zero))
        {
            return WinAPI.FindWindowEx(taskBarHandle, IntPtr.Zero, "TrayNotifyWnd", IntPtr.Zero);
        }
        return IntPtr.Zero;
    }

    public static Rectangle GetTrayRectangle()
    {
        WinAPI.RECT rect;
        WinAPI.GetWindowRect(WinAPI.GetTrayHandle(), out rect);
        return new Rectangle(new Point(rect.left, rect.top), new Size((rect.right - rect.left) + 1, (rect.bottom - rect.top) + 1));
    }
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文