Question

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!

Was it helpful?

Solution 2

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);
}

OTHER TIPS

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.

enter image description here

(Image taken from VBForums)

While ToolStripMenuItem is not a control, its parent is. Therefore you can call

myToolStripMenuItem.GetCurrentParent().CreateGraphics()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top