• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • 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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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