首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

在C# 中使用NotifyIcon控件,怎么让左键点击和右键点击是一样的事件呢

2012-05-11 
在C# 中使用NotifyIcon控件,如何让左键点击和右键点击是一样的事件呢?private void Icon_Main_MouseDown(o

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

热点排行