How to change the font of the main menu in application window?

G

Guest

I am developing a windows forms application using VS.NET in C#. I am using
the MDI style. I have a main menu defined with several layers of menu items.
I know that I can change the appearance of all menus by changing the Menu
property within the advanced appearance of the system display properties.
But I only want to change the font size and style of the menu within my
application.

How can I change the font of the menu only for my application?

Thanks,
Dave
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Based on my understanding, you want to change the font of the Winform
application menu.

Net did not have build-in support for setting the menu font. But we may
owner-draw the menu, and draw the text in specific font. For more
information, please refer to below article:
"Owner Draw Menus in C#"
http://www.c-sharpcorner.com/Code/2002/April/OwnerDrawMenusSK.asp

=============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Jeffrey,

The link you sent helped out some. However, the example does not explain
how to get similar behavior as the standard MenuItem class. For example, I
would like my menu items (MyMenuItem) to support underlining the accelerator
character. The example just underlines the whole menu item text. Basically,
I want my application menu items to behave and appear just like default menu
items except I want to specify the font (family,size, style).

I believe I know how to handle all of the DrawItemEventArgs members except
the State member.

How do I handle all of the different DrawItemEventArgs.State values with
appropriate forecolor, backcolor and underlining?

Thanks,
Dave
 
M

Mick Doherty

You'll find an Office style OwnerDraw menu example on my site, which shows
how to do this.
http://dotnetrix.co.uk/menus.html

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Dave Leach said:
Jeffrey,

The link you sent helped out some. However, the example does not explain
how to get similar behavior as the standard MenuItem class. For example,
I
would like my menu items (MyMenuItem) to support underlining the
accelerator
character. The example just underlines the whole menu item text.
Basically,
I want my application menu items to behave and appear just like default
menu
items except I want to specify the font (family,size, style).

I believe I know how to handle all of the DrawItemEventArgs members except
the State member.

How do I handle all of the different DrawItemEventArgs.State values with
appropriate forecolor, backcolor and underlining?

Thanks,
Dave

"Jeffrey Tan[MSFT]" said:
Hi Dave,

Based on my understanding, you want to change the font of the Winform
application menu.

.Net did not have build-in support for setting the menu font. But we may
owner-draw the menu, and draw the text in specific font. For more
information, please refer to below article:
"Owner Draw Menus in C#"
http://www.c-sharpcorner.com/Code/2002/April/OwnerDrawMenusSK.asp

=============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to
be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your feedback.

I think you want to draw your own menuitem text, but with the accelerator
character a underline to clew the user that he can use "alt"+ "accelerator
character" to simulate the click operation. Yes?

I will spend some time on it, and reply to you some time later. Thanks for
your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Jeffrey,

Yes, I want the same accelerator behavior as the MenuItem class. I really
just
need to be able to control the Font.

Thanks,
Dave
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your waiting.

Yes, after some research, we may use StringFormat to specify its
HotkeyPrefix property to System.Drawing.Text.HotkeyPrefix.Show, then place
a "&" infront of accelerator character, then draw this text out in specific
font, you will get what you want, sample code like this:

private void menuItem1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
string myCaption="&Main Menu";

Brush myBrush = System.Drawing.Brushes.Black;
Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline,
GraphicsUnit.Pixel);

Console.WriteLine(e.State.ToString());
myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Regular,
GraphicsUnit.Pixel);
StringFormat sf = new StringFormat();
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;

e.Graphics.DrawString(myCaption, myFont, myBrush, e.Bounds.X, e.Bounds.Y,
sf);
}

private void menuItem1_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
string myCaption="&Main Menu";

Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline,
GraphicsUnit.Pixel);
Size mySize = e.Graphics.MeasureString(myCaption, myFont).ToSize();

e.ItemHeight=mySize.Height;
e.ItemWidth=mySize.Width;
}
===================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Mick Doherty

To get the same behaviour as windows menu's, you would check whether the
mnemonic underline is visible. The DrawItemState Enumeration has a
NoAccelerator flag which we can use to check for whether the underscore
should be shown.

\\\
if (System.Convert.ToBoolean(e.State & DrawItemState.NoAccelerator))
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Hide;
else
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


"Jeffrey Tan[MSFT]" said:
Hi Dave,

Thanks for your waiting.

Yes, after some research, we may use StringFormat to specify its
HotkeyPrefix property to System.Drawing.Text.HotkeyPrefix.Show, then place
a "&" infront of accelerator character, then draw this text out in
specific
font, you will get what you want, sample code like this:

private void menuItem1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
string myCaption="&Main Menu";

Brush myBrush = System.Drawing.Brushes.Black;
Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline,
GraphicsUnit.Pixel);

Console.WriteLine(e.State.ToString());
myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Regular,
GraphicsUnit.Pixel);
StringFormat sf = new StringFormat();
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;

e.Graphics.DrawString(myCaption, myFont, myBrush, e.Bounds.X, e.Bounds.Y,
sf);
}

private void menuItem1_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
string myCaption="&Main Menu";

Font myFont = new Font(FontFamily.GenericSerif, 14, FontStyle.Underline,
GraphicsUnit.Pixel);
Size mySize = e.Graphics.MeasureString(myCaption, myFont).ToSize();

e.ItemHeight=mySize.Height;
e.ItemWidth=mySize.Width;
}
===================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Mick,

Oh, yes, you are correct. This is because the default setting in Control
Panel -> Display Properties -> Appearance -> Effects -> "Hide underlined
letter for keyboard navigation" is checked, so the underscore will be
displayed only when we pressed "Alt"(only when you pressed "Alt" key, the
DrawItemEventArgs.State will not have DrawItemStates.NoAccelerator flag).

So, we should use DrawItemEventArgs.State to get if windows wanted the
underscore through NoAccelerator flag, if needed, just paint with
HotkeyPrefix.Show.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Jeffrey and Mark,

I really appreciate the help you have provided. I have my MenuItem subclass
working just great now. It now accounts for State conditions I care about and
even handles the case where a menu item is used as a separator.

Mark, thanks for the link. The code samples there really helped a lot.

Thanks again,
Dave
 

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