Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

OwnerDrawn Menus in C#, Problems

Posted on 2004-10-23
7
Medium Priority
?
245 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
Comment
Question by:derekthornton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
Farshid-Zaker earned 2000 total points
ID: 12389335
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
ID: 12390045
No. That doesnt' work. It still draws nothing to the screen.
0
 
LVL 2

Author Comment

by:derekthornton
ID: 12390057
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:derekthornton
ID: 12390077
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
ID: 12403624
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
ID: 12408546
yes, You were drawing on a zero area!

Thanks anyway,

<Farshid/>
0
 
LVL 2

Author Comment

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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