创建一个从 ToolStripProfessionalRenderer 派生的类
public class customRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
{
// check if the object being rendered is actually a ToolStripButton
if (e.Item is ToolStripButton)
{
ToolStripButton button = e.Item as ToolStripButton;
// only render checked items differently
if (button.Checked || button.Selected)
{
//do work here
}
else
base.OnRenderButtonBackground(e);
}
// if this object is not a ToolStripButton, use the normal render event
else
base.OnRenderButtonBackground(e);
}
protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
{
//do checking here like above
base.OnRenderItemBackground(e);
}
}
设置您在表单构造函数上拥有的控件的渲染 - 适用于所有工具条(toolstrippbackground、toolstripbutton、contexmenustrip 等)我刚刚覆盖了 OnRenderButtonBackground,因为您提到了这一点,您可以覆盖 ToolStripProfessionalRenderer 的其他方法
toolStrip1.Renderer = new customRenderer();//if you have this and want to override any behaviour
menuStrip1.Renderer = new customRenderer();//if you have this and want to override any behaviour
contextMenuStrip1.Renderer = new customRenderer();//if you have this and want to override any behaviour