有没有办法将窗体放在Windows 7和Windows Vista中点击的通知图标上方?
答案 0 :(得分:5)
这是一种更简单的方法。
触发OnClick事件时,您可以获得鼠标的X,Y位置。
您还可以使用这些对象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();
}
答案 1 :(得分:1)
关于你的评论:“我怎么知道任务栏是如何定位的?”
查看以下文章,其中包含一个类,该类公开了检索托盘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));
}
}
希望这有帮助。