2016-04-21 115 views
0

我正在編寫備份工具。在我的工具之上,我有一個菜單欄,其中包含兩個工具欄菜單項。我改變了一些顏色到我的期望。沒有集中的菜單看起來不錯:MenuItem的顏色變化

Unclicked Menu Item

當我現在在菜單項「文件」點擊打開上下文菜單,顏色變爲白色,我無法再閱讀全文:

Clicked Menu Item

誰能告訴我哪裏可以改變這種行爲?我使用Visual Studio 2013 Ultimate,Windows窗體應用程序,代碼位於C#中。

下面是代碼:

// // initializing menuStrip1 // this.menuStrip1.BackColor = System.Drawing.Color.MediumBlue; this.menuStrip1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.menuStrip1.Font = new System.Drawing.Font("Segoe UI Semilight", 15.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.helpToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.MinimumSize = new System.Drawing.Size(0, 40); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(1056, 40); this.menuStrip1.TabIndex = 77; this.menuStrip1.Text = "menuStrip1"; // // initializing fileToolStripMenuItem and adding to menuStrip1 // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.saveToolStripMenuItem, this.saveAsToolStripMenuItem, this.loadToolStripMenuItem}); this.fileToolStripMenuItem.Font = new System.Drawing.Font("Calibri Light", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.fileToolStripMenuItem.ForeColor = System.Drawing.SystemColors.ControlLightLight; this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 36); this.fileToolStripMenuItem.Text = "File"; this.fileToolStripMenuItem.Click += new System.EventHandler (this.fileToolStripMenuItem_Click); // // initializing saveToolStripMenuItem and adding to fileToolStripMenuItem // this.saveToolStripMenuItem.BackColor = System.Drawing.Color.MediumBlue; this.saveToolStripMenuItem.ForeColor = System.Drawing.SystemColors.ControlLightLight; this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Size = new System.Drawing.Size(166, 30); this.saveToolStripMenuItem.Text = "Save"; this.saveToolStripMenuItem.Click += new System.EventHandler (this.saveToolStripMenuItem_Click);
//

+1

您可以發佈您的顏色自定義代碼? –

+0

添加了初始化代碼:) –

回答

0

默認情況下此功能是不可用的開箱。您需要爲您創建自定義Renderer工具條以實現此目的。

創建從ToolStripProfessionalRenderer繼承的類 -

private class BlueRenderer : ToolStripProfessionalRenderer 
    { 
     protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e) 
     { 
      Rectangle rc = new Rectangle(Point.Empty, e.Item.Size); 
      Color c = Color.MediumBlue; 
      using (SolidBrush brush = new SolidBrush(c)) 
       e.Graphics.FillRectangle(brush, rc); 
     } 
    } 

而且此渲染連接到您的菜單條在窗體構造 -

public Form1() 
    { 
     InitializeComponent(); 
     menuStrip1.Renderer = new BlueRenderer(); 
    } 
+0

哈哈,工作正常,非常感謝你!有趣的是,你可以改變所有的顏色默認,但不是特定的一個...微軟的邏輯?! –

+0

還有一個問題。現在所有狀態的顏色都是MediumBlue。當我單擊menuStrip1中的項目時是否可以使用LightBlue? –

+1

@ schmelzer-daniel - 只需像這樣定義你的clocr:'Color c = e.Item.Selected? Color.LightBlue:Color.MediumBlue; ' – Yogi

1

您可以創建自己的ProfessionalColorTable並覆蓋它的屬性:

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      menuStrip1.Renderer = new ToolStripProfessionalRenderer(new MyColorTable()); 
     } 
    } 

public class MyColorTable : ProfessionalColorTable 
{ 
    public override Color ToolStripDropDownBackground 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color ImageMarginGradientBegin 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color ImageMarginGradientMiddle 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color ImageMarginGradientEnd 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color MenuBorder 
    { 
     get 
     { 
      return Color.Black; 
     } 
    } 

    public override Color MenuItemBorder 
    { 
     get 
     { 
      return Color.Black; 
     } 
    } 

    public override Color MenuItemSelected 
    { 
     get 
     { 
      return Color.Navy; 
     } 
    } 

    public override Color MenuStripGradientBegin 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color MenuStripGradientEnd 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color MenuItemSelectedGradientBegin 
    { 
     get 
     { 
      return Color.Navy; 
     } 
    } 

    public override Color MenuItemSelectedGradientEnd 
    { 
     get 
     { 
      return Color.Navy; 
     } 
    } 

    public override Color MenuItemPressedGradientBegin 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 

    public override Color MenuItemPressedGradientEnd 
    { 
     get 
     { 
      return Color.Blue; 
     } 
    } 
} 
} 

這是鱈魚的結果上述E:

Custom menu