I'm custom drawing a menu item in a MenuStrip
. The problem I'm having is that the menu item insists on sizing itself based on the text, which is not what I want (there is no text). I can set AutoSize
to false and explicitly specify a size, but the containing menu (ToolStripDropDown
) still sizes itself based on the text, which causes it to be too small to contain the entire menu item.
Is there a straightforward way to set the size of a menu item?
It can be achieved by adding an empty image to the toolStripItem
. Now change the size of image to give the desired size to your ToolStripItem
. I have done this in my project as below:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
Bitmap emptyImage = new Bitmap(48, 48);
ToolStripMenuItem m_Item = new ToolStripMenuItem(System.Convert.ToString("All Programs",emptyImage);
m_Item.ImageAlign = ContentAlignment.MiddleLeft;
m_Item.ImageScaling = ToolStripItemImageScaling.None;
m_Item.Name = btnId.ToString();
MenuStrip menu = new MenuStrip();
menu.Items.Add(m_Item);
}
}
This comments in an article on CodeProject explains the nature of my issue:
While you can handle the Paint event for a ToolStripMenuItem, ToolStripMenuItem isn't intended to be "owner drawn". If you want to handle the drawing of a particular tool strip item, the recommended means is to create your own ToolStripItem-derived type. See ToolStripItem Class[^] for an example.
In general ToolStrip is most properly architectured control in the WinForms namespace. It has almost unlimited possibilities and very extensible.
Update: According to your comment. I cannot say much, because while ToolStrip is most architectured it is also commented very good, and many particular things need to be discovered. I still added #3 to my answer, but many things can be discovered only by trying, and by using Reflector of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With