当一个项目未被保存时,我希望我的ToolStrip背景能够改变。
为了渲染ToolStrip的背景,我使用自己的渲染器:
class ToolStripRenderer : ToolStripProfessionalRenderer
{
private MenuBarForm parent;
public ToolStripRenderer(MenuBarForm Parent)
{
parent = Parent;
}
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
if (parent.controlItems.Last().Unsaved)
e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, Color.Red, 90, true), e.AffectedBounds);
else
e.Graphics.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, SystemColors.ControlDark, 90, true), e.AffectedBounds);
}
}
工具栏第一次呈现时,它的设计是灰色到深灰色的,呈现正确:
![CorrectToolstrip](https://istack.dev59.com/MNBqF.webp)
![IncorrectToolstrip](https://istack.dev59.com/QQscY.webp)
e.AffectedBounds
更改为e.ToolStrip.Bounds
,但没有成功。