编程语言
首页 > 编程语言> > c# – 在单击的通知图标上方定位表单

c# – 在单击的通知图标上方定位表单

作者:互联网

有没有办法在Windows 7和Windows Vista中单击通知图标上方定位表单?

解决方法:

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

查看以下文章,其中包含一个类,该类公开了检索托盘Rectangle Structure的方法:[c#] NotifyIcon – Detect MouseOut

使用此类,您可以检索托盘的Rectangle Structure,如下所示:

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

希望这可以帮助.

标签:c,winforms,position,notifyicon
来源: https://codeday.me/bug/20190530/1186042.html