托盘图标上下文菜单在WPF应用程序中的定位



我有一个C#WPF.NET 4应用程序,它在系统托盘中有一个图标。我目前正在使用备受讨论的WPF NotifyIcon,但我遇到的问题并不依赖于此控件。问题是,.NET4根本不允许(在大多数情况下)WPF ContextMenu对象出现在Windows7任务栏的顶部。这个例子很好地说明了这个问题。

XAML:

<Window x:Class="TrayIconTesting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="400">
    <Window.Resources>
        <ContextMenu x:Key="TrayContextMenu" Placement="MousePoint">
            <MenuItem Header="First Menu Item" />
            <MenuItem Header="Second Menu Item" />
        </ContextMenu>
        <Popup x:Key="TrayPopup" Placement="MousePoint">
            <Border Width="100" Height="100" Background="White" BorderBrush="Orange" BorderThickness="4">
                <Button Content="Close" Click="ButtonClick"></Button>
            </Border>
        </Popup>
    </Window.Resources>
    <StackPanel Orientation="Horizontal">
        <Label Target="{Binding ElementName=UseWinFormsMenu}" VerticalAlignment="Center">
            <AccessText>Use WinForms context menu for tray menu:</AccessText>
        </Label>
        <CheckBox Name="UseWinFormsMenu" IsChecked="False" Click="UseWinFormsMenuClicked" VerticalAlignment="Center" />
    </StackPanel>
</Window>

代码:

using System.Drawing;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using ContextMenu = System.Windows.Controls.ContextMenu;
namespace TrayIconTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ContextMenuStrip winFormsContextMenu;
        public MainWindow()
        {
            InitializeComponent();
            this.TrayIcon = new NotifyIcon
            {
                Icon = new Icon("Bulb.ico"),
                Visible = true
            };
            this.TrayIcon.MouseClick += (sender, args) =>
                                        {
                                            switch (args.Button)
                                            {
                                                case MouseButtons.Left:
                                                    this.TrayPopup.IsOpen = true;
                                                    break;
                                                case MouseButtons.Right:
                                                    if (!this.UseWinFormsMenu.IsChecked.GetValueOrDefault())
                                                    {
                                                        this.TrayContextMenu.IsOpen = true;
                                                    }
                                                    break;
                                            }
                                        };
        }
        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            this.TrayPopup.IsOpen = false;
        }
        private void UseWinFormsMenuClicked(object sender, RoutedEventArgs e)
        {
            this.TrayIcon.ContextMenuStrip = this.UseWinFormsMenu.IsChecked.GetValueOrDefault() ? this.WinFormsContextMenu : null;
        }
        private ContextMenu TrayContextMenu
        {
            get
            {
                return (ContextMenu)this.FindResource("TrayContextMenu");
            }
        }
        private Popup TrayPopup
        {
            get
            {
                return (Popup)this.FindResource("TrayPopup");
            }
        }
        private NotifyIcon TrayIcon
        {
            get;
            set;
        }
        private ContextMenuStrip WinFormsContextMenu
        {
            get
            {
                if (this.winFormsContextMenu ==  null)
                {
                    this.winFormsContextMenu = new ContextMenuStrip();
                    this.winFormsContextMenu.Items.AddRange(new[] { new ToolStripMenuItem("Item 1"), new ToolStripMenuItem("Item 2") });
                }
                return this.winFormsContextMenu;
            }
        }
    }
}

要查看问题,请确保托盘图标始终可见,而不是Win7托盘图标弹出窗口的一部分。右键单击任务栏图标时,上下文菜单将在任务栏上方打开。现在,右键单击旁边的一个标准Windows任务栏图标,查看区别。

现在,左键点击图标,注意到它确实允许自定义弹出窗口在鼠标光标所在的右侧打开

选中"使用WinForms…"复选框将切换应用程序以使用Windows.Forms程序集中的旧ContextMenuStrip上下文菜单。这显然会在正确的位置打开菜单,但它的外观与默认的Windows7菜单不匹配。具体来说,行高亮显示是不同的。

我已经玩过Horizontal和VerticalOffset属性,使用"right"值,你可以在屏幕右下角弹出上下文菜单,但这同样糟糕。它仍然不会打开你的光标所在的位置。

真正令人振奋的是,如果您针对.NET3.5构建相同的示例,它会像预期的那样工作。不幸的是,我的实际应用程序使用了许多.NET4功能,因此不能选择恢复。

有人知道如何在光标所在的位置打开上下文菜单吗?

经过一番搜索,我偶然发现了这个问题&答复我从未想过在NotifyIcon上尝试ContextMenu属性!虽然并不理想,但在WPF解决系统托盘是应用程序的有用部分这一事实之前,它将足够好地工作。不过,如果失去WPF上下文菜单提供的所有绑定和命令路由功能,那将是一件非常遗憾的事情。

不过,接受我自己的答案感觉不对,所以我要再把它放几天。

我很高兴没有将其标记为已回答,因为我找到了一个稍微好一点的选项。我找到了这篇文章,详细介绍了如何将图标添加到System.Windows.Forms.MenuItem对象。现在只需要一点代码,我就有了一个与系统上下文菜单完美匹配的菜单!

最新更新