Solved

OwnerDrawn Menus in C#, Problems

Posted on 2004-10-23
240 Views
Last Modified: 2010-04-15
I'm trying to do an OwnerDrawn Menu in C#. Using an Overridden MenuItem object and specifying "ownerdrawn" to true. But ..no matter what I put in the OnDrawItem method, NOTHING happens..


public class MenuItemEx : MenuItem
{
  public MenuItemEx()
  {
    InitializeComponent(); Text = "Test":
  }
 
  protected override void OnDrawItem(DrawItemEventArgs e)
  {
    // NOTHING happens when the Item is Painted, NO MATTER what I
    // put here!!!!!
  }

  private void InitializeComponent()
  {
   this.OwnerDraw = true;
  }
}
0
Question by:derekthornton
    7 Comments
     
    LVL 4

    Accepted Solution

    by:
    Hi,
    First, do not forget calling base class OnDrawItem. If you call it, you can use e.Graphics methods to draw text and graphics in your MenuItem.

    A simple example:
          protected override void OnDrawItem(DrawItemEventArgs e)
          {
                base.OnDrawItem(e);
                Brush br;

                e.Graphics.FillRectangle(SystemBrushes.ControlLight, e.Bounds);
                br = new SolidBrush(SystemColors.WindowText);
                e.Graphics.DrawString(Text, new Font("Tahoma", 8), br, e.Bounds.Left + 5, e.Bounds.Top + 2);
                br.Dispose();
          }

    <Farshid/>
    0
     
    LVL 2

    Author Comment

    by:derekthornton
    No. That doesnt' work. It still draws nothing to the screen.
    0
     
    LVL 2

    Author Comment

    by:derekthornton
    here is my code. try adding one of these MenuItems to a ContextMenu and see what happens. it doesn't do anything ...

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace Canvas.Windows.Forms
    {
          public sealed class CanvasMenuItem : MenuItem
          {
                //================================================
                // Private Instance Fields
                //================================================

                //================================================
                // Class Constructors
                //================================================

                /// <summary>
                /// Constructs a new CanvasMenuItem
                /// </summary>
                public CanvasMenuItem( string Text ):base(Text)
                {
                      // Required for the Windows Form Designer Support
                      InitializeComponent();
                }

                //================================================
                // Painting Methods (WM_PAINT)
                //================================================

                protected override void OnDrawItem(DrawItemEventArgs e)
                {
                      base.OnDrawItem(e);
                      Brush br;

                      e.Graphics.FillRectangle(Brushes.Green, e.Bounds);
                      br = new SolidBrush(SystemColors.WindowText);
                      e.Graphics.DrawString(Text, new Font("Tahoma", 8), br, e.Bounds.Left + 5, e.Bounds.Top + 2);
                      br.Dispose();
                }

                protected override void OnMeasureItem(MeasureItemEventArgs e)
                {
                      base.OnMeasureItem (e);
                }

                //================================================
                // Designers
                //================================================

                #region Windows Form Designer generated code

                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                      //
                      // CanvasMenuItem
                      //
                      this.OwnerDraw = true;

                }

                #endregion

          }
    }
    0
     
    LVL 2

    Author Comment

    by:derekthornton
    http://www.cis3351.com/derek/CanvasProject/CanvasFormPainting.zip

    there's the entire solution, including resources and such, if you'd like to see for yourself.
    0
     
    LVL 2

    Author Comment

    by:derekthornton
    The problem turned out to be that there was nothign to draw on. I wasn't doing anything with OnMeasureItem.
    0
     
    LVL 4

    Expert Comment

    by:Farshid-Zaker
    yes, You were drawing on a zero area!

    Thanks anyway,

    <Farshid/>
    0
     
    LVL 2

    Author Comment

    by:derekthornton
    Is it possible to eliminate the borders around menu items?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now