How to make the height of context menu items not fixed (for example, scale to fit a specific item) in .Net?

See two images below. I do not want each item on the menu to be the height of the largest. It must match the size of the content. I played with a number of properties and could not prevent this behavior. Is it possible?

Desired height
(source: blakerobertson.com )

Fixed Height For all!
(source: blakerobertson.com )

+4
source share
2 answers

Set MenuItem.OwnerDraw to true, then handle MenuItem.MeasureItem . This allows you to tell Windows Forms the size of this menu item regardless of the size of the others, although due to the fact that you need to display this item yourself.

Note. This does not lead to an automatic match size: you will need to use the GDI + functions to calculate the desired size.

+2
source

Old question, but I had the same problem with ToolStripMenuItem shown for NotifyIcon . Solved setting AutoSize = False , but it did not draw text well, I can not understand why. Then I had to draw it by doing my Paint action.

  Private Sub OneMenuItem_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles OneMenuItem.Paint If Me.DesignMode Then Return Dim g As Graphics = e.Graphics Dim it = OneMenuItem Dim p = it.GetCurrentParent Using b As New Drawing.SolidBrush(it.ForeColor) g.DrawString(it.Text, it.Font, b, p.Padding.Left + 4 + it.Padding.Left, p.Padding.Top + 4 + it.Padding.Top) End Using End Sub 

don't ask me what magic 4 is, they worked well comparing both drawn text in DesignMode (it draws text in design mode and you can compare).
VS2008, by the way.

0
source

Source: https://habr.com/ru/post/1299497/


All Articles