单击事件在NotifyIcon附带的ContextMenu中延迟 [英] Click event delayed in ContextMenu attached to NotifyIcon
问题描述
我正在使用一个插件(使用 System.ComponentModel.Composition
)将应用程序放置在Windows UI的通知区域中。
I am working on a plugin (using System.ComponentModel.Composition
) for an application to place an icon in the notification area of the 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
处理程序不是
但是有趣的是,当我再次右键单击图标(单击菜单项后)时,先前单击的 Click
处理程序 MenuItem
被调用。左键单击或将鼠标悬停在图标上不会触发此步骤。
Now when I click on an item in the context menu of that icon, the Click
handler is not being invoked.
Interestingly though, when I right-click the icon again (after having clicked a menu item) the Click
handler for the previously clicked MenuItem
is invoked. Left-clicking or hovering over the icon does not trigger this step.
发生了什么事?
更新:我强烈认为我的问题与此问题有关。但我仍在尝试找出如何将其应用于我的插件/应用程序。
Update: I have a strong feeling my problem is related to this question. But I'm still trying to figure out how I could apply that to my plugin/application.
推荐答案
了解我的问题是没有为NotifyIcon处理任何窗口消息(或至少没有我喜欢/需要的消息)。
To my understanding the problem is that no window messages are being processed for the NotifyIcon (or at least not as many messages as I liked/needed).
我通过从<$继承来解决了这个问题c $ c> Form 并为我的插件运行另一个消息泵。
I solved the issue by inheriting from Form
and running another message pump for my plugin.
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
}
}
似乎很棒。
这篇关于单击事件在NotifyIcon附带的ContextMenu中延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!