Owner Drawn Menu item -- Matching Font?
-
I have a menu which I have to owner draw the few menu items which contain icons. I snagged the code to do this from Petzold's Prgramming Windows in C# book. It helped a lot, but one thing that bothers me is the hardcoded font size and unknown style. It comes up looking odd because the font is slightly different than the rest of the context menu. I need to to find two items. 1. Default system font and font size for menu items. 2. Background color Default size and font to match font itself. Is there something like SystemColors class that I can get into for the proper font style adn size for menu items. When 2 is discovered, how do I changed the background color of the drawn menu item rectangle? It looks fine on my machine which is XP and the background is white already. When I ran my proggy on Win2K, the regular items had the standard silver/gray background, but my owner drawn items have a white background. Yuck! Example of my current owner drawn menu items (measure and draw) are below for reference.
// Measure Item override for the AddSmiley context menu item owner draw private void MeasureItemAddSmileyMenuItem(object obj, MeasureItemEventArgs miea) { int iFontPointSize = 10; MenuItem mi = (MenuItem)obj; Font ItemFont = new Font(mi.Text.Substring(1), iFontPointSize); StringFormat strfmt = new StringFormat(); SizeF sizef = miea.Graphics.MeasureString(mi.Text, ItemFont, 1000, strfmt); CSmilies oSmile = new
-
I have a menu which I have to owner draw the few menu items which contain icons. I snagged the code to do this from Petzold's Prgramming Windows in C# book. It helped a lot, but one thing that bothers me is the hardcoded font size and unknown style. It comes up looking odd because the font is slightly different than the rest of the context menu. I need to to find two items. 1. Default system font and font size for menu items. 2. Background color Default size and font to match font itself. Is there something like SystemColors class that I can get into for the proper font style adn size for menu items. When 2 is discovered, how do I changed the background color of the drawn menu item rectangle? It looks fine on my machine which is XP and the background is white already. When I ran my proggy on Win2K, the regular items had the standard silver/gray background, but my owner drawn items have a white background. Yuck! Example of my current owner drawn menu items (measure and draw) are below for reference.
// Measure Item override for the AddSmiley context menu item owner draw private void MeasureItemAddSmileyMenuItem(object obj, MeasureItemEventArgs miea) { int iFontPointSize = 10; MenuItem mi = (MenuItem)obj; Font ItemFont = new Font(mi.Text.Substring(1), iFontPointSize); StringFormat strfmt = new StringFormat(); SizeF sizef = miea.Graphics.MeasureString(mi.Text, ItemFont, 1000, strfmt); CSmilies oSmile = new
Nobody seemed to know, but I was able to figure it out with a little help from this article right here at Code Project: http://www.codeproject.com/cs/menu/MenuImage.asp I am reposting my DrawItem as it is now to show the correct way to do so in case anyone else runs into the same question.
// DrawItem override for the AddSmiley context menu item owner draw private void DrawAddSmileyMenuItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { MenuItem mi = (MenuItem)sender; // Default menu font Font menuFont = SystemInformation.MenuFont ; SolidBrush menuBrush = null ; // Determine menu brush for painting if ( mi.Enabled == false ) { // disabled text menuBrush = new SolidBrush( SystemColors.GrayText ) ; } else { if ( (e.State & DrawItemState.Selected) != 0) { // Text color when selected (highlighted) menuBrush = new SolidBrush( SystemColors.HighlightText ) ; } else