2013-04-10 83 views

回答

3

默認toolstrip渲染器會忽略BackColor屬性並使用硬編碼的顏色。

你可以參考下面的鏈接來使用你自己的渲染器以你想要的方式繪製分隔符。

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6cceab5b-7e06-40cf-82da-56cdcc57eb5d

+0

我看到這篇文章,但我正在尋找另一種解決方案,我不認爲這是最好的解決方案 – Siwar 2013-04-10 13:07:32

+0

我也研究過它,但沒有發現其他可供選擇的替代方案,這是更好的替代方案。 – Freelancer 2013-04-10 13:08:39

+0

我把這個代碼放在Menu.designer.cs中this.fileToolStripMenuItem.BackColor = System.Drawing.SystemColors.ButtonFace;但它不起作用 – Siwar 2013-04-10 13:09:11

1

我看到問題在2年前被問到,但我仍然無法在網絡上找到一個簡單明瞭的解決方案。所以...

我剛剛面對這個問題,發現解決它非常簡單。

有同樣的情況:

enter image description here

解決方案:

創建繼承了ToolStripSeparator類的類和方法添加到PaintEventHandler繪製分隔符:

public class ExtendedToolStripSeparator : ToolStripSeparator 
{ 
    public ExtendedToolStripSeparator() 
    { 
     this.Paint += ExtendedToolStripSeparator_Paint; 
    } 

    private void ExtendedToolStripSeparator_Paint(object sender, PaintEventArgs e) 
    { 
     // Get the separator's width and height. 
     ToolStripSeparator toolStripSeparator = (ToolStripSeparator)sender; 
     int width = toolStripSeparator.Width; 
     int height = toolStripSeparator.Height; 

     // Choose the colors for drawing. 
     // I've used Color.White as the foreColor. 
     Color foreColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardForeColorName); 
     // Color.Teal as the backColor. 
     Color backColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardBackColorName); 

     // Fill the background. 
     e.Graphics.FillRectangle(new SolidBrush(backColor), 0, 0, width, height); 

     // Draw the line. 
     e.Graphics.DrawLine(new Pen(foreColor), 4, height/2, width - 4, height/2); 
    } 
} 

然後添加分隔符:

ToolStripSeparator toolStripSeparator = new ExtendedToolStripSeparator(); 

this.DropDownItems.Add(newGameToolStripMenuItem); 
this.DropDownItems.Add(addPlayerToolStripMenuItem); 
this.DropDownItems.Add(viewResultsToolStripMenuItem); 
// Add the separator here. 
this.DropDownItems.Add(toolStripSeparator); 
this.DropDownItems.Add(exitToolStripMenuItem); 

結果:

enter image description here

1

我只是指出我的分隔符Paint事件這一習俗PROC:

private void mnuToolStripSeparator_Custom_Paint (Object sender, PaintEventArgs e) 
    { 
     ToolStripSeparator sep = (ToolStripSeparator)sender; 

     e.Graphics.FillRectangle(new SolidBrush(CUSTOM_COLOR_BACKGROUND), 0, 0, sep.Width, sep.Height); 

     e.Graphics.DrawLine(new Pen(CUSTOM_COLOR_FOREGROUND), 30, sep.Height/2, sep.Width - 4, sep.Height/2); 

    } 

凡CUSTOM_COLOR_FOREGROUND是固體/命名爲Color,例如Color.White。