1

使用以下代码,我得到一个带有上下文菜单的系统托盘图标。但是当我在应用程序运行时更改 Windows 主题时,背景颜色保持不变。

private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Controls.ContextMenu contextMenu1;
    private System.Windows.Forms.MenuItem menuItem1;
    private System.ComponentModel.IContainer components;


    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Action action = new Action(ExecuteStartupSequence);
        action.ExecuteProfiled();


        this.components = new System.ComponentModel.Container();
        /*
        this.contextMenu1 = new System.Windows.Forms.ContextMenu();
        this.menuItem1 = new System.Windows.Forms.MenuItem();
         */

        // Initialize contextMenu1
        /*
        this.contextMenu1.MenuItems.AddRange(
                    new System.Windows.Forms.MenuItem[] { this.menuItem1 });
        */

        // Initialize menuItem1
        /*
        this.menuItem1.Index = 0;
        this.menuItem1.Text = "E&xit";
        this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
        */
        this.contextMenu1 = this.FindResource("TrayContextMenu") as System.Windows.Controls.ContextMenu;


        // Create the NotifyIcon.
        //this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(components);

        // The Icon property sets the icon that will appear
        // in the systray for this application.
        notifyIcon1.Icon = new System.Drawing.Icon("Icon1.ico");

        // The ContextMenu property sets the menu that will
        // appear when the systray icon is right clicked.
        // notifyIcon1.ContextMenu = this.contextMenu1;

        // The Text property sets the text that will be displayed,
        // in a tooltip, when the mouse hovers over the systray icon.
        notifyIcon1.Text = "Form1 (NotifyIcon example)";
        notifyIcon1.Visible = true;

        // Handle the DoubleClick event to activate the form.
        notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
        notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_DoubleClick);



        //tb = (TaskbarIcon)FindResource("notifyIcon"); ;
    }

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
    {
        this.contextMenu1 = this.FindResource("TrayContextMenu") as System.Windows.Controls.ContextMenu;
        contextMenu1.IsOpen = true;
    }

    private void menuItem1_Click(object Sender, EventArgs e)
    {
        MessageBox.Show("Open");


    }

这里是 XAML-Stuff。

<ContextMenu x:Key="TrayContextMenu" Placement="MousePoint" Style="{x:Null}">
        <MenuItem Header="First Menu Item" Style="{x:Null}" />
        <MenuItem Header="Second Menu Item" Style="{x:Null}" />
        </ContextMenu>
        <Popup x:Key="TrayPopup" Placement="MousePoint">
            <Border Width="100" Height="100" Background="White" BorderBrush="Orange" BorderThickness="4">
            <Button Content="Close" Click="menuItem1_Click"></Button>
            </Border>
        </Popup>

我无法理解这一点,我已经使用 Style="{x:Null}" 来摆脱所有未配置的东西,但它根本不起作用。我可以避免使用 System.Windows.Controls.Contextmenu,但我应该改用什么?

我很感谢所有的提示。

谢谢

4

2 回答 2

2

如果您想在更改窗口颜色时更改颜色,那么您的控件需要使用系统颜色之一 wpf中的系统颜色

然后使用 dynamicresource 以便在更改资源时重新应用资源

 <Border Width="100" Height="100" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
于 2012-09-25T21:41:05.637 回答
0

我将受影响的 XAML 代码更改为:

<ContextMenu 
        x:Key="TrayContextMenu" 
        Placement="MousePoint" 
        Style="{x:Null}" 
        Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
        <MenuItem 
            Header="First Menu Item" 
            Style="{x:Null}"
            Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>

        <MenuItem 
            Header="Second Menu Item" 
            Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />

        </ContextMenu>
        <Popup x:Key="TrayPopup" Placement="MousePoint">
            <Border Width="100" Height="100" Background="White" BorderBrush="Orange" BorderThickness="4">
            <Button Content="Close" Click="menuItem1_Click"></Button>
            </Border>
        </Popup>

但结果还是一样。

通常这会解决问题,但如果是托盘图标上下文菜单,它会失败。

于 2012-09-26T05:16:49.677 回答