MenuItem Drawing text with Formatted Shortcuts

L

Lex

As you may have seen in other posts I have a C# app that has custom
shortcuts so I need to OwnerDraw my MenuItems.

Thanx to vJ and Mick for the tips so far. I am almost there :)

My last (I hope) issue is aligning the menu text and shortcut text
properly. I need it to line up as if there are two columns - 1 for
the menu text and one for the shortcut text. The menu text is left
justified in col 1 and shortcur text is left justified in col 2. This
is what a standard menu with shortcuts looks like.

Anyone have any ideas on how to accompilsh this?


Best Regards
 
M

Mick Doherty

When I do this I right-align the shorcut. Take a look at Office and VS.net menus and you'll see that that's what MS are doing too. The problem with left aligning in seperate columns is that you must know the longest menuitems text and the longest shortcut text in a popupmenu before your OnMeasureItem method runs. This also results in longer menus and wasted space as demonstrated below.

Left aligned text + right aligned shortcut
----------------------------------------------
Short Text Ctrl+Shift+Ins |
And a much longer string in a menu item Ins |
----------------------------------------------

Left aligned text + left aligned shortcut in second column
 
M

Mike Kitchen

Hi,

I thought this was the default for two columns. In the following example, no
matter what you put in the first column of each menuitem, the second column
lines up.

// Add File Menu
MenuItem File = mainMenu.MenuItems.Add("&File");
File.MenuItems.Add( new MenuItem( "&New",
new EventHandler(
this.FileNew_Clicked ),
Shortcut.CtrlN ));
File.MenuItems.Add( new MenuItem( "&Open",
new EventHandler(
this.FileOpen_Clicked ),
Shortcut.CtrlO ));
File.MenuItems.Add("-"); // Gives us a seperator
File.MenuItems.Add( new MenuItem( "E&xit",
new EventHandler(
this.FileExit_Clicked ),
Shortcut.CtrlX ));

I have a sample which shows this in action here.
http://www.publicjoe.f9.co.uk/csharp\csharp14.html

Look at code for menudemo3.cs near the bottom of the page.

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
 
L

Lex

Thanx again Mick. You seem to be the expert on custom menus :)

Have you every had to deal with the following?

I have a menu that has a mixture of OwnerDrawn and non-OwnerDrawn
items. I want my OwnerDrawn items to indent the exact amount needed
to match the non_OwnerDrawn indent. I can eyeball it and use
e.Bounds.Left + x but is there a better way to match the indent?

Same issue with getting a matching height in the MeasureItem event.

Any suggestions (other than making all the items owner drawn, which is
an option but I'd rather avoid that for some coding reasons that are
not important for this discussion).

Regards
 
M

Mick Doherty

Height:
\\\
SystemInformation.MenuHeight
///
Text Offset:
\\\
SystemInformation.MenuCheckSize.Width + _
SystemInformation.FixedFrameBorderSize.Width
///

If you're going to mix and match then you'll have to use two columns for
menuitemtext since that's the default for non-ownerdraw menuitems. As
explained in the last message you will need to know the longest shortcut
text when calling measureitem on the menuitem with the longest caption.
 
S

Supra

http://www.vbdotnetheaven.com/
Thanx again Mick. You seem to be the expert on custom menus :)

Have you every had to deal with the following?

I have a menu that has a mixture of OwnerDrawn and non-OwnerDrawn
items. I want my OwnerDrawn items to indent the exact amount needed
to match the non_OwnerDrawn indent. I can eyeball it and use
e.Bounds.Left + x but is there a better way to match the indent?

Same issue with getting a matching height in the MeasureItem event.

Any suggestions (other than making all the items owner drawn, which is
an option but I'd rather avoid that for some coding reasons that are
not important for this discussion).

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top