I know that I can draw rectangles just about anywhere I want using
using (Graphics G = myControl.CreateGraphics())
{
G.DrawRectangle(new Pen(myColor),myControl.Bounds);
}
but I'm having trouble figuring out how to do this with a toolStripMenuItem, so that I can draw a rectangle around it.
Any help is appreciated. Thanks!
The easiest way is to inherit from the control and override the OnPaint
method, then change all instances of ToolStripMenuItem
to 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 );
}
}
A bit more complicated, but better in the long run for maintainability, is implementing a custom ToolStripRenderer which would allow you to change the look of the entire thing, for example making it look like VS2010.
(Image taken from VBForums )
You can try using the Paint event (you should rarely ever use CreateGraphics) and the ContentRectangle property:
void toolStripButton1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawRectangle(Pens.Red, toolStripButton1.ContentRectangle);
}
While ToolStripMenuItem
is not a control, its parent is. Therefore you can call
myToolStripMenuItem.GetCurrentParent().CreateGraphics()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.