Geeks With Blogs
O(geek) Ryan Cain, running in time proportional to Geek.

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. 

Posted on Friday, June 11, 2004 10:31 AM Development , .Net 2.0 | Back to top


Comments on this post: MenuStrip & ToolStrip

# re: MenuStrip & ToolStrip
Requesting Gravatar...
That rocks!
Left by Jeff Julian on Jun 11, 2004 2:11 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
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
Left by shim on Jun 29, 2004 5:11 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
That is so freaking cool. ;)
Left by Shawn Burke on Jul 02, 2004 1:01 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
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.
Left by evega on Jul 19, 2004 5:44 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
It seems like site where EXE can be downloaded does not work. Can you please, verify it?
Left by Anton on Aug 16, 2004 12:32 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
The link to download the menustrip zip file doesn't work. Can you put the file back up somewhere else to be downloaded.

Thanks...
Left by Alex on Sep 06, 2004 5:03 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
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.
Left by Ryan Cain on Sep 16, 2004 9:58 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
Some time ago I was develop Rebar ToolStrip Rendrer. Hope this will help like additional example :).

http://www.chaliy.com/Sources/RebarRenderer/
Left by Mike Chalit on Feb 21, 2006 1:59 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
Sorry Chalit is Chaliy :). Hope author will fix my post.
Left by Mike Chaliy on Feb 21, 2006 2:02 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
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.
Left by Ashish Gupta on Jun 11, 2007 8:46 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
Please do it in VB.NET
Left by Luci on Oct 16, 2007 5:09 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
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
Left by maniraj on Feb 12, 2008 4:26 AM

# re: Dropdownlist
Requesting Gravatar...
how to use dropdownlist tool in vb.net2005
Left by gunavignesh on Feb 25, 2008 7:47 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
How to add new event to a toolstripmenu at running time??

plz help
best regards
Left by alex on Jul 03, 2008 8:14 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
hello
i need a menustrip for vb.net2005
Left by efat on Nov 09, 2008 2:46 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
can any body tell me how to bind menustip to database
Left by ravi mantra on Mar 10, 2009 5:49 AM

# ToolStrip
Requesting Gravatar...
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
Left by Jesu Balan on Jun 08, 2009 5:43 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
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
Left by Adel Daas on Sep 29, 2009 3:16 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
I want to menustrip and toolstrip for design in form assignment.
Left by Pheak on Jun 22, 2010 6:31 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
i want to create this menustrip in my software. pls help me, iam using vb dot net 2005
Left by Gayath on Jul 01, 2010 1:38 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
how to include the toolstrip in web application?
Left by ishwarya on Jul 19, 2010 12:51 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
default disable in asp & vb.net.so,hoe to enable or how to create menu strip & tool strip in asp.net and vb.net .....please tell me ?????????

please steps send to my mail id...


thanks,

kkss
Left by kkss on Sep 06, 2010 4:35 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
Unfortunately this example isn't designed to work on the web, it was originally written for Windows Forms, and in fact I believe there's been some breaking changes in the newer frameworks that this only works in Windows Forms 2.0
Left by Ryan on Sep 08, 2010 2:08 PM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
how can i change the border color to transparent, and set the background color to transparent
Left by oghenero on May 25, 2011 5:49 AM

# re: MenuStrip & ToolStrip
Requesting Gravatar...
it's very easy to understand...
Left by mahadevi on Mar 05, 2014 11:28 PM

Your comment:
 (will show your gravatar)


Copyright © Ryan Cain | Powered by: GeeksWithBlogs.net