如何为 MouseDown 事件添加事件处理程序?
How to add event handler for MouseDown event?
当我按照 Microsoft 网页上的文档操作时,添加事件处理程序单击事件非常简单。不幸的是,没有包含 MouseDown 事件的示例。
我已经尝试了很多组合,但我一定是使用了错误的语法或错误的声明。
这很好用:
notifyIcon.Click += new System.EventHandler(NotifyIcon_Click);
System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });
menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;
}
private void NotifyIcon_Click(object Sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.Activate();
}
但这不是:
notifyIcon.MouseDown += new System.EventHandler(NotifyIcon_MouseDown);
System.Windows.Forms.ContextMenu contextMenu = new
System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItemExit = new
System.Windows.Forms.MenuItem();
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{menuItemExit});
menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;
}
private void NotifyIcon_MouseDown(object Sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.Activate();
}
我在这里想要实现的是右键单击打开上下文菜单,左键单击通知图标打开应用程序本身。我希望在 MouseDown 事件中能够检测到鼠标左键或右键是否按下。
我建议您在 page_load 事件中添加事件以触发其 ASP.Net 网络应用程序。
protected void Page_Load(object sender, EventArgs e) { }.
根据您添加的方式,该事件看起来是正确的。
如果这是表单应用程序,那么您需要在构造函数中处理它,语法如下:
this.nitifyIcon.MouseDown += 新 System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseDown);
我看到我已经收到了一些回复。在等待的过程中,我突然发现了解决我一直面临的问题的方法。
来源:Invoke NotifyIcon's Context Menu
private void SetNotifyIconSettings()
{
notifyIcon.Text = "My Application";
notifyIcon.Icon = new System.Drawing.Icon("now-agent-icon.ico");
notifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseDown) ;
System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });
menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;
}
private void NotifyIcon_MouseDown(object Sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Visibility = Visibility.Visible;
this.Activate();
}
if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
MethodInfo mi = typeof(System.Windows.Forms.NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon, null);
}
}
private void menuItemExit_Click(object Sender, EventArgs e)
{
CloseApplication();
}
当我按照 Microsoft 网页上的文档操作时,添加事件处理程序单击事件非常简单。不幸的是,没有包含 MouseDown 事件的示例。
我已经尝试了很多组合,但我一定是使用了错误的语法或错误的声明。
这很好用:
notifyIcon.Click += new System.EventHandler(NotifyIcon_Click);
System.Windows.Forms.ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });
menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;
}
private void NotifyIcon_Click(object Sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.Activate();
}
但这不是:
notifyIcon.MouseDown += new System.EventHandler(NotifyIcon_MouseDown);
System.Windows.Forms.ContextMenu contextMenu = new
System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItemExit = new
System.Windows.Forms.MenuItem();
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{menuItemExit});
menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;
}
private void NotifyIcon_MouseDown(object Sender, EventArgs e)
{
this.Visibility = Visibility.Visible;
this.Activate();
}
我在这里想要实现的是右键单击打开上下文菜单,左键单击通知图标打开应用程序本身。我希望在 MouseDown 事件中能够检测到鼠标左键或右键是否按下。
我建议您在 page_load 事件中添加事件以触发其 ASP.Net 网络应用程序。
protected void Page_Load(object sender, EventArgs e) { }.
根据您添加的方式,该事件看起来是正确的。
如果这是表单应用程序,那么您需要在构造函数中处理它,语法如下:
this.nitifyIcon.MouseDown += 新 System.Windows.Forms.MouseEventHandler(this.notifyIcon_MouseDown);
我看到我已经收到了一些回复。在等待的过程中,我突然发现了解决我一直面临的问题的方法。
来源:Invoke NotifyIcon's Context Menu
private void SetNotifyIconSettings()
{
notifyIcon.Text = "My Application";
notifyIcon.Icon = new System.Drawing.Icon("now-agent-icon.ico");
notifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(NotifyIcon_MouseDown) ;
System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem();
contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItemExit });
menuItemExit.Index = 0;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new System.EventHandler(menuItemExit_Click);
notifyIcon.ContextMenu = contextMenu;
}
private void NotifyIcon_MouseDown(object Sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Visibility = Visibility.Visible;
this.Activate();
}
if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
MethodInfo mi = typeof(System.Windows.Forms.NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon, null);
}
}
private void menuItemExit_Click(object Sender, EventArgs e)
{
CloseApplication();
}