托盤程序菜單位置 NotifyIcon menu position
.net framework4.5.2 環境下使用NotifyIcon 托盤 默認狀態下彈出的右鍵菜單是在左邊 別家的都是在右邊。
使用ContextMenuStrip 在其opening事件中
m_NotifyIcon.ContextMenuStrip.Opening += ContextMenuStripOnOpening;
private void ContextMenuStripOnOpening(object sender, CancelEventArgs cancelEventArgs)
{
Point p = CursorPosition.GetCursorPosition();
Point menuPosition = new Point();
menuPosition.X =p.X;
menuPosition.Y = p.Y - 5 - m_NotifyIcon.ContextMenuStrip.Size.Height;
m_NotifyIcon.ContextMenuStrip.Show(menuPosition);
}
鼠標位置的獲取:
public class CursorPosition
{
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public static Point GetCursorPosition()
{
POINT mousestart = new POINT();
GetCursorPos(out mousestart);
Point p = new Point();
p.X = mousestart.X;
p.Y = mousestart.Y;
return p;
}
}
托盤程序菜單位置 NotifyIcon menu position