Home Contact

O(geek)

Running in time proportional to Geek.

News

Archives

Post Categories

Syndication:

MenuStrip & ToolStrip

After watching Joe Stegman's DEV260 overview of Windows Forms “2.0” I was fascinated by the new MenuStrip and ToolStrip controls.  I love the fact that MS is giving us access to controls that will have a very similar look and feel, by default, to the current MS applications.  What's even better is these new strips can use a custom renderer to create your own look and feel.  That means when Office v.next comes out we'll immeidately be able to update our own look and feel to match the new UI. 

I've included a sample of an Xbox theme I hacked together just to get a basic feel for creating a custom renderer.  I'm sure I'm far off best practices, but I'm happy to get this working for a start. 

The following code you'll need in your Form with the ToolStrip and MenuStrip objects. 

            MyRenderer rend = new MyRenderer();
            ToolStripManager.Renderer = rend;
            toolStrip1.Renderer = rend;
            menuStrip1.Renderer = rend;

The important thing to note in this code is the ToolStripManager.Renderer = rend;  line.  This seems to be the key to having the OnRenderRaftingContainerBackground event called in your customer renderer.  Without it you'll end up with a rafting container that is blue or the system colors.  (The rafting container I've picked up is the object under the MenuStrip and ToolStrip that tie things together.  This is pretty much the area no drawn over by the strips.) 

Unfortunately, at least to me, this seems a little unintuitive.  I was expecting to be able to set a Renderer property on the instance of the RaftingContainer object.  Instead there is this static property that gets set.  I'm sure there is something under the hood I don't understand yet that has led to this implementation. 

The next piece is inheriting from ToolStripRenderer to create your own renderer.  I've included below a sample of one of the more interesting events that is fired.  This is the code that implements the drawing of the greenish yellow gradient when the user hovers over a toolbar button with the mouse, and clicks on a button.

        //The drawing that happens on the background of the button.
        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            base.OnRenderButtonBackground(e);

            Graphics g = e.Graphics;
           
            //if the mouse is over the given buttin
            if (e.Item.Selected)
            {
                //if the mouse has been pressed on the button,
                //
we want to make the colors a little
                //darker so the user knows something happened.
                if (e.Item.Pressed)
                {
                    using (LinearGradientBrush b = new LinearGradientBrush(new Rectangle(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height), Color.FromArgb(Color.Yellow.R, Color.Yellow.G, Color.Yellow.B + 50), Color.DarkGreen, 90))
                    {
                        g.FillRectangle(b, new Rectangle(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height));
                    }
                }
                //draw the case when the mouse is just hovering. 
                else
                {
                    using (LinearGradientBrush b = new LinearGradientBrush(new Rectangle(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height), Color.Yellow, Color.Green, 90))
                    {
                        g.FillRectangle(b, new Rectangle(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height));
                    }
                }
            }
        }

Hopefully this will get a few people started writing custom renderers.  I was really surprised at how easy it is to write one.  Please feel free to let me know what you think.  I'm learning as I go, so I'm sure there are some places that I can definitely learn a litle more.

BTW this was built and runs on Visual Studio 2005 CTP May, the version released at TechEd and on MSDN at the same time. 


Feedback

# re: MenuStrip & ToolStrip

That rocks! 6/11/2004 2:11 PM | Jeff Julian

# re: MenuStrip & ToolStrip

Is there source code that we can download and play with? I seem to be missing some important part when I try to do this 6/29/2004 5:11 PM | shim

# re: MenuStrip & ToolStrip

That is so freaking cool. ;) 7/2/2004 1:01 PM | Shawn Burke

# re: MenuStrip & ToolStrip

Did you ever have two different toolstrips and try moving one in front of the other? I have tried it and it seems to not work correctly. I am not even worried about the custom render not working correctly. 7/19/2004 5:44 AM | evega

# re: MenuStrip & ToolStrip

It seems like site where EXE can be downloaded does not work. Can you please, verify it? 8/16/2004 12:32 PM | Anton

# re: MenuStrip & ToolStrip

The link to download the menustrip zip file doesn't work. Can you put the file back up somewhere else to be downloaded.

Thanks... 9/6/2004 5:03 AM | Alex

# re: MenuStrip & ToolStrip

Sorry about that. Stupid me has that domain running internally w/ DNS records to the sites that no longer worked from the outside world. So I didn't see a problem.

Both the image and link should be fixed now. 9/16/2004 9:58 PM | Ryan Cain

# re: MenuStrip & ToolStrip

Some time ago I was develop Rebar ToolStrip Rendrer. Hope this will help like additional example :).

http://www.chaliy.com/Sources/RebarRenderer/ 2/21/2006 1:59 AM | Mike Chalit

# re: MenuStrip & ToolStrip

Sorry Chalit is Chaliy :). Hope author will fix my post. 2/21/2006 2:02 AM | Mike Chaliy

# re: MenuStrip & ToolStrip

This is an easy code to understand. Could we write same type of code in VB.Net. What term i use in place of 'Using(...) in VB.Net. 6/11/2007 8:46 AM | Ashish Gupta

# re: MenuStrip & ToolStrip

Please do it in VB.NET 10/16/2007 5:09 AM | Luci

# re: MenuStrip & ToolStrip

Hi,

I am trying to create ToolStrip.
when i was executing i couldn't able to move the tool strip. Can you help me.

Thanks 2/12/2008 4:26 AM | maniraj

# re: Dropdownlist

how to use dropdownlist tool in vb.net2005 2/25/2008 7:47 AM | gunavignesh

# re: MenuStrip & ToolStrip

How to add new event to a toolstripmenu at running time??

plz help
best regards
7/3/2008 8:14 PM | alex

# re: MenuStrip & ToolStrip

hello
i need a menustrip for vb.net2005
11/9/2008 2:46 AM | efat

# re: MenuStrip & ToolStrip

can any body tell me how to bind menustip to database 3/10/2009 5:49 AM | ravi mantra

# ToolStrip

I added the items in the toolstrip dynamiccally,.. Then I want to find which item is selected currently in the toolstrip. please help to do this

Regards
Jesu 6/8/2009 5:43 AM | Jesu Balan

# re: MenuStrip & ToolStrip

i am new to this web browsing on VB2008 and i couldnt understand some of wat u have said up..can anyone help me in a tutorial or something ? i just need to add menu bar, status bar, favorites, history etc and all to my web browser..i need help but in details plz
thx for any help 9/29/2009 3:16 PM | Adel Daas

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: