【问题标题】:Background Color doesn't change while changing windows system colors更改 Windows 系统颜色时背景颜色不会更改
【发布时间】:2012-09-17 10:49:22
【问题描述】:

使用以下代码,我得到一个带有上下文菜单的系统托盘图标。但是当我在应用程序运行时更改 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,但我应该改用什么?

感谢所有提示。

谢谢

【问题讨论】:

    标签: c# wpf contextmenu background-color system-tray


    【解决方案1】:

    如果您想在更改窗口颜色时更改颜色,则您的控件需要使用系统颜色之一System colors in wpf

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

     <Border Width="100" Height="100" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
    

    【讨论】:

    • 不幸的是,这没有帮助。
    • 我发现,如果上下文菜单可见,Windows 系统颜色将改变时,它可以工作。可能是什么原因?
    【解决方案2】:

    我将受影响的 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>
    

    但结果还是一样。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多