ToolStripMenuItem bigger vertical padding, or vertically centering text in a bigger ToolStripMenuItem
Asked Answered
M

3

7

I'm trying to set a bigger vertical padding for ToolStripMenuItems in a ContextMenuStrip. However, changing the Padding.Top property adds padding to the bottom, instead of the top.

I also tried setting a larger Height for the ToolStripMenuItem, it works, however, the text always gets aligned on top, even if the TextAlign property is MiddleCenter. It should be vertical aligned to the center!

I've tried different settings for different properties, nothing works. The idea is that I cannot get the ToolStripMenuItem to have more space around its text, both to the top and to the bottom.

I'm using C#, Windows Forms, Net 2.0, Visual Studio 2010 Express, Windows 7.

Meloniemelony answered 1/6, 2011 at 17:1 Comment(0)
U
2

You can get the same effect using Margin instead of Padding which will keep the Text of the ToolStripMenuItem aligned.

The drawback is that this wont modify the size of the highlight rectangle when the item is selected so it can look a little strange if you increase a lot the height.

Underpart answered 1/6, 2011 at 17:34 Comment(1)
You can adjust the "TextRectangle" of the ToolStripMenuItem's renderer if you add a few lines of code. See my answer below for details.Keaton
K
3

In addition to InBetween's answer, you can fix the highlight rectangle by using a custom renderer and adjusting its "TextRectangle" property. Here's some sample code:

    var itemHeight = 36;
    var verticalPadding = 36 - TextRenderer.MeasureText("A", _DisplayNameFont).Height) / 2;
    menu.Renderer = new MyRenderer { VerticalPadding = verticalPadding };

    class MyRenderer : ToolStripSystemRenderer
    {
        public int VerticalPadding { get; set; }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            if (null == e)
            { return; }
            e.TextFormat &= ~TextFormatFlags.HidePrefix;
            e.TextFormat |= TextFormatFlags.VerticalCenter;
            var rect = e.TextRectangle;
            rect.Offset(0, VerticalPadding);
            e.TextRectangle = rect;
            base.OnRenderItemText(e);
        }
    }
Keaton answered 17/11, 2015 at 0:27 Comment(0)
U
2

You can get the same effect using Margin instead of Padding which will keep the Text of the ToolStripMenuItem aligned.

The drawback is that this wont modify the size of the highlight rectangle when the item is selected so it can look a little strange if you increase a lot the height.

Underpart answered 1/6, 2011 at 17:34 Comment(1)
You can adjust the "TextRectangle" of the ToolStripMenuItem's renderer if you add a few lines of code. See my answer below for details.Keaton
K
2

Adding a new line does the job.

It's not the best solution, but it's a quick way to add some padding.

Works well when adding support for small touch screens.

this.configToolStripMenuItem.Text = "\r\nSettings";
Kish answered 20/9, 2016 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.