Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Owner Drawn Menu item -- Matching Font?

Owner Drawn Menu item -- Matching Font?

Scheduled Pinned Locked Moved C#
questioncsharpgraphicsregexjson
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KingTermite
    wrote on last edited by
    #1

    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
    
    K 1 Reply Last reply
    0
    • K KingTermite

      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
      
      K Offline
      K Offline
      KingTermite
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups