OwnerDrawn Menus From Extender Component

Volte

Neutiquam Erro
Joined
Nov 17, 2002
Messages
2,172
OK, I'm developing a component in VB.NET to simplify menus with
bitmaps. It implements the IExtenderProvider interface and adds
an 'ImageIndex' property to every MenuItem on the form. The goal
is to ownerdraw every menuitem on the form with just dropping this
component on the form.

However, I've hit a snag. I don't know where to do the OwnerDrawing.
If I put it in the CanExtend function (Implemented from IExtenderProvider;
the function checks to see if a specific control should be extended
with a new property), it actually changes the OwnerDraw property
at design time (i.e. actually, sets OwnerDraw = True in the property
window and underlying form code). If I set it in the SetImageIndex
method (it is called every time the ImageIndex extended property
is changed to set up the appropriate values), it also changes it
at design time.

My question is this: where should I put the appropriate OwnerDraw = True
and AddHandlers in the component so that it OwnerDraws every
MenuItem on the form that it's contained on, but at run time only.

If possible, I'd like to do it with as little code on the form as possible
(I'm trying to concentrate all the the code in this component).
 
Ok, after doing a little research on the matter I have a couple of ideas.

You should hook up the events in the SetImageIndex function. Every Component has a protected DesignMode property so you can see whether or not to change the properties based on that.

As far as making sure you've hooked up events for *every* menu item, I'm a little stumped as to the best way to do this. My best idea as the moment is, whenever a call is made to SetImageIndex, you hook up the Popup event of the parent menu (if you haven't already done so). Now, when that event fires, you loop through all child menus, and if you haven't set their OwnerDraw properties, do so then.

I'm sure there must be a better way to do this but haven't found one in the limited time I've been able to devote to this. The MainMenu class offered a promising GetForm() method, which unfortunately returns null at that stage. If it didn't, one could hook the Load event of the Form, to find all menus and ownerdraw them.

If you find a better way, be sure to post it!
 
Back
Top