• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

OwnerDrawn Menus in C#, Problems

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
derekthornton
Asked:
derekthornton
  • 5
  • 2
1 Solution
 
Farshid-ZakerCommented:
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
 
derekthorntonAuthor Commented:
No. That doesnt' work. It still draws nothing to the screen.
0
 
derekthorntonAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
derekthorntonAuthor Commented:
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
 
derekthorntonAuthor Commented:
The problem turned out to be that there was nothign to draw on. I wasn't doing anything with OnMeasureItem.
0
 
Farshid-ZakerCommented:
yes, You were drawing on a zero area!

Thanks anyway,

<Farshid/>
0
 
derekthorntonAuthor Commented:
Is it possible to eliminate the borders around menu items?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now