.NET Forum / Windows Forms / WinForm General / October 2004
How to change the font of the main menu in application window?
|
|
Thread rating:  |
Dave Leach - 28 Sep 2004 18:51 GMT 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
"Jeffrey Tan[MSFT]" - 29 Sep 2004 03:25 GMT 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
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Dave Leach - 30 Sep 2004 01:23 GMT 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
> Hi Dave, > [quoted text clipped - 20 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. Mick Doherty - 30 Sep 2004 07:08 GMT You'll find an Office style OwnerDraw menu example on my site, which shows how to do this. http://dotnetrix.co.uk/menus.html
 Signature Mick Doherty http://dotnetrix.co.uk/nothing.html
> Jeffrey, > [quoted text clipped - 44 lines] >> This posting is provided "as is" with no warranties and confers no >> rights. "Jeffrey Tan[MSFT]" - 30 Sep 2004 10:47 GMT 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
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Dave Leach - 30 Sep 2004 16:39 GMT 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
> Hi Dave, > [quoted text clipped - 12 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. "Jeffrey Tan[MSFT]" - 01 Oct 2004 03:34 GMT 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
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Mick Doherty - 01 Oct 2004 07:01 GMT 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; ///
 Signature Mick Doherty http://dotnetrix.co.uk/nothing.html
> Hi Dave, > [quoted text clipped - 47 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. "Jeffrey Tan[MSFT]" - 01 Oct 2004 08:06 GMT 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
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Dave Leach - 01 Oct 2004 20:39 GMT 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
> Hi Mick, > [quoted text clipped - 13 lines] > Get Secure! - www.microsoft.com/security > This posting is provided "as is" with no warranties and confers no rights. "Jeffrey Tan[MSFT]" - 04 Oct 2004 04:35 GMT Hi Dave,
You are welcome!
Best regards, Jeffrey Tan Microsoft Online Partner Support
 Signature Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|