[英]Click event on MenuItem of a ContextMenu associated to NotifyIcon on a Windows form application not firing but requires one more click on icon to work
[英]Click event delayed in ContextMenu attached to NotifyIcon
我正在为应用程序开发一个插件(使用System.ComponentModel.Composition
),以将图标放置在Windows UI的通知区域中。
trayMenu.MenuItems.Clear();
// Create context menu items
foreach( IJob job in jobs ) {
MenuItem menuItem = new MenuItem( job.Name ) {Tag = job};
menuItem.Click += MenuItemClick;
trayMenu.MenuItems.Add( menuItem );
}
private void MenuItemClick( object sender, EventArgs e ) {
// ...
}
现在,当我单击该图标的上下文菜单中的某个项目时,不会调用Click
处理程序。
但是,有趣的是,当我再次右键单击图标(单击菜单项后)时,将调用先前单击的MenuItem
的Click
处理程序。 左键单击或将鼠标悬停在图标上不会触发此步骤。
到底是怎么回事?
更新 :我很感到我的问题与这个问题有关 。 但我仍在尝试弄清楚如何将其应用于我的插件/应用程序。
据我了解,问题在于没有为NotifyIcon处理任何窗口消息(或者至少没有我喜欢/需要的消息数量)。
我通过从Form
继承并为插件运行另一个消息泵解决了该问题。
using System;
using ...
namespace JobTracker.Tray {
[Export( typeof( IJobTrackerPlugin ) )]
public class TrayPlugin : Form, IJobTrackerPlugin {
#region Plugin Interface
[Import( typeof( IJobTracker ) )]
#pragma warning disable 649
private IJobTracker _host;
#pragma warning restore 649
private IJobTracker Host {
get { return _host; }
}
public void Initialize() {
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add( "Exit", OnExit );
trayIcon = new NotifyIcon();
trayIcon.Icon = new Icon( SystemIcons.Application, 32, 32 );
trayIcon.ContextMenu = trayMenu;
// Show the proxy form to pump messages
Load += TrayPluginLoad;
Thread t = new Thread(
() => {
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.None;
trayIcon.Visible = true;
ShowDialog();
} );
t.Start();
}
private void TrayPluginLoad( object sender, EventArgs e ) {
// Hide the form
Size = new Size( 0, 0 );
}
#endregion
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
private void OnExit( object sender, EventArgs e ) {
Application.Exit();
}
#region Implementation of IDisposable
// ...
private void DisposeObject( bool disposing ) {
if( _disposed ) {
return;
}
if( disposing ) {
// Dispose managed resources.
if( InvokeRequired ) {
EndInvoke( BeginInvoke( new MethodInvoker( Close ) ) );
} else {
Close();
}
trayIcon.Dispose();
trayMenu.Dispose();
}
// Dispose unmanaged resources.
_disposed = true;
}
#endregion
}
}
似乎工作很棒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.