时间:2019-05-11 标签:c#toolstripmenuitem更改背景winforms [英] c# winforms toolstripmenuitem change background
问题描述
好的,请告诉我为什么这不起作用.
OK, someone please tell me why this is not working.
我在 winforms 应用程序 (c#) 中有一个简单的 MenuStrip.它有 ToolStripMenuItems.
I have a simple MenuStrip in winforms app (c#). It has ToolStripMenuItems.
在设计器的属性窗口中,我选择 BackColor = White.在 Desginer.cs 文件中我可以看到它.
In the properties window of the designer, I select BackColor = White. In Desginer.cs file I can see it.
运行应用,背景色为Control(灰色).
Running the app, the background color is Control (grey).
这是怎么回事?为什么背景色不是白色?
What is going? Why is the backcolor not white?
谢谢
编辑
这是来自 Designer.cs 的代码:
This is the code from the Designer.cs:
this.menuRefresh.BackColor = System.Drawing.Color.White;
编辑 2:
在代码中,加载表单后(在构造函数和 Form_Load 事件中,我已经放置了这个:
In the code, after loading the form (in the constructor and also in Form_Load event I've placed this:
menuRefresh.BackColor = Color.White;
也没有帮助.
推荐答案
您需要实现一个简单的渲染器类来实现这一点.下面是一个例子:
You need to implement a simple renderer class to achieve this. Here is an example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
menuStrip1.Renderer = new MyRenderer();
}
private class MyRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
Color c = e.Item.Selected ? Color.Azure : Color.Beige;
using (SolidBrush brush = new SolidBrush(c))
e.Graphics.FillRectangle(brush, rc);
}
}
}
这篇关于时间:2019-05-11 标签:c#toolstripmenuitem更改背景winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!