4

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

4

2 回答 2

8

这是一个更简单的方法。

触发 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();        
    }
于 2014-07-06T20:45:51.063 回答
1

关于您的评论:“我怎么知道任务栏的位置?”

查看以下文章,其中包含一个公开检索托盘矩形结构的方法的类: [c#] NotifyIcon - Detect 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));
    }
}

希望这可以帮助。

于 2011-09-03T18:33:28.507 回答