OwnerDrawContextMenu, ver. 2.0.0

[How To Use] [Properties] [Methods]
[Hints] [Known Bugs] [Comments]

OwnerDrawContextMenu is a class for adding your own menu items to the shell context menu (the menu you get when you right-click a file or folder). It is descended from CustomContextMenu. Its additional feature is that it allows you to create owner-drawn menu items.

Although the two classes, OwnerDrawContextMenu and CustomContextMenu, exist in the same unit, I've chosen to separate them in the documentation. This document describes only the added features of OwnerDrawContextMenu. For an explanation of all its inherited features, and a general guide on how to create a context menu handler, see the CustomContextMenu documentation.

How To Use

In order to draw the context menu items manually, you must override the two abstract methods, MeasureMenuItem and DrawMenuItem, described below. You must also insert the relevant menu items as owner-drawn items (using the MFT_OWNERDRAW constant).

Properties

Methods

MeasureMenuItem procedure MeasureMenuItem(IdCmd: UINT; var Width: UINT; var Height: UINT); virtual; abstract;
When you're drawing a menu item manually Windows needs to know its size. This method must return the appropriate width and height of your owner-drawn menu item.
Parameters:
IdCmd is the ID of the menu item that requires redrawing (which you specified in AddMenuItems).
Width is the required width of the menu item.
Height is the required height of the menu item.
NOTE: You must specify a width and height. You can't expect the variables to be predefined with meaningful values.
NOTE: Windows internally adds right and left margins to the width, so don't include any margin.
DrawMenuItem procedure DrawMenuItem(Menu: HMENU; IdCmd: UINT; DC: HDC; Rect: TRect; State: TOwnerDrawState); virtual; abstract;
This method is called when the owner-drawn menu item must draw itself.
Parameters:
Menu is the handle of the context menu or a submenu.
IdCmd is the ID of the menu item that requires redrawing.
DC is the device context to draw on.
Rect is the rectangle to draw inside. You should stay inside this area. You can draw anywhere on the menu, but whatever you paint outside your menu item's area will likely be painted over by the other menu items.
State is the state of the menu item (whether it is highlighted, disabled, etc.) You should paint the item accordingly.
GetStandardMenuItemHeight function GetStandardMenuItemHeight: Longint;
This is a utility method that returns the height of a standard menu item. It is useful in the MeasureMenuItem method if you want your menu item to be of standard height.
GetStandardTextWidth function GetStandardTextWidth(Text: String): Longint;
This is a utility method that returns the width of a text when it's rendered in the standard menu font. It is useful in the MeasureMenuItem method if you want your menu item to contain text in the standard menu font.
Parameters:
Text is the text to calculate the width of.

Hints

Known Bugs

WinME (and maybe also Win95 and Win98, please tell me) has a bug: If you attempt to install your DLL using an *.inf file, Windows will report it can't find the DLL if the DLL has a long file name (more than 8.3 letters). Using the "Browse" button in the dialog Windows presents makes no difference. So consider using a filename in the 8.3 format, if you intend to install your handler via an *.inf file.

Comments

CustomContextMenu and OwnerDrawContextMenu are freeware. Feel free to use and improve them, but please include all original files if you redistribute the zip-file. If you have any comments or corrections I would very much like to hear them.

Get the latest version from http://www3.brinkster.com/troels/delphi.asp.

Troels Jakobsen
delphiuser@get2net.dk