我知道我可以在任何我想使用的地方绘制矩形
using (Graphics G = myControl.CreateGraphics())
{
G.DrawRectangle(new Pen(myColor),myControl.Bounds);
}
但我无法弄清楚如何使用 toolStripMenuItem 执行此操作,以便我可以在它周围绘制一个矩形。
任何帮助表示赞赏。谢谢!
我知道我可以在任何我想使用的地方绘制矩形
using (Graphics G = myControl.CreateGraphics())
{
G.DrawRectangle(new Pen(myColor),myControl.Bounds);
}
但我无法弄清楚如何使用 toolStripMenuItem 执行此操作,以便我可以在它周围绘制一个矩形。
任何帮助表示赞赏。谢谢!
最简单的方法是从控件继承并覆盖该OnPaint
方法,然后将所有实例更改ToolStripMenuItem
为MyToolStripMenuItem
。
class MyToolStripMenuItem : ToolStripMenuItem
{
protected override void OnPaint( PaintEventargs pe )
{
base.OnPaint( pe );
pe.ClipRectangle.Inflate( -1, -1 );
pe.Graphics.DrawRectangle( Pens.Black, pe.ClipRectangle );
}
}
更复杂一点,但从长远来看更好的可维护性,是实现一个自定义ToolStripRenderer,它可以让你改变整个事物的外观,例如让它看起来像 VS2010。
(图片取自VBForums)
您可以尝试使用 Paint 事件(您应该很少使用 CreateGraphics)和 ContentRectangle 属性:
void toolStripButton1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawRectangle(Pens.Red, toolStripButton1.ContentRectangle);
}
虽然ToolStripMenuItem
不是控件,但它的父级是。因此你可以打电话
myToolStripMenuItem.GetCurrentParent().CreateGraphics()