日期:2014-05-18  浏览次数:21013 次

在C# 中使用NotifyIcon控件,如何让左键点击和右键点击是一样的事件呢?
private void Icon_Main_MouseDown(object sender, MouseEventArgs e)
  {
  if (e.Button == MouseButtons.Left)
  {
  contextList.Show();
  }
   
  }

我写的数遍点击事件,但是左键显示的位置和右键显示的位置不一样,怎么让他跟右键显示的位置一样呢?都是在右下角

contextList 是NotifyIcon控件

------解决方案--------------------
Show方法有重载的,你可以设定弹出的Point,获取屏幕鼠标位置
int i = MousePosition.X;
int j = MousePosition.Y; 
下面是NotifyIcon的show菜单的源码
C# code

private void ShowContextMenu()
{
    if ((this.contextMenu != null) || (this.contextMenuStrip != null))
    {
        NativeMethods.POINT pt = new NativeMethods.POINT();
        UnsafeNativeMethods.GetCursorPos(pt);
        UnsafeNativeMethods.SetForegroundWindow(new HandleRef(this.window, this.window.Handle));
        if (this.contextMenu != null)
        {
            this.contextMenu.OnPopup(EventArgs.Empty);
            SafeNativeMethods.TrackPopupMenuEx(new HandleRef(this.contextMenu, this.contextMenu.Handle), 0x48, pt.x, pt.y, new HandleRef(this.window, this.window.Handle), null);
            UnsafeNativeMethods.PostMessage(new HandleRef(this.window, this.window.Handle), 0, IntPtr.Zero, IntPtr.Zero);
        }
        else if (this.contextMenuStrip != null)
        {
            this.contextMenuStrip.ShowInTaskbar(pt.x, pt.y);
        }
    }
}