我试图在手动设置高度为 60 的上下文菜单项中将文本垂直居中对齐。但是无论我尝试什么,文本始终位于顶部。无需我做任何事情,同一项目中的图像将自行正确对齐。
我试过以下方法:
foreach (ToolStripItem item in ContextMenuStrip1.Items)
{
item.AutoSize = false;
item.Height = 60;
item.Width = maxWidth;
item.TextAlign = ContentAlignment.MiddleCenter;
}
并创建一个新的 customeRender 类:
public sealed class CustomRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
if (e.Item.IsOnDropDown)
{
e.TextFormat |= TextFormatFlags.VerticalCenter;
}
base.OnRenderItemText(e);
}
}
但是这对我不起作用。文本垂直保留在项目的顶部。 添加到这些项目之一的图像会居中,至于更多的箭头。
任何帮助都会很棒。 艾丹
最佳答案
使用 ToolStripButton 而不是 ToolStripItem - 文本对齐似乎在此控件上工作正常
var b = new ToolStripButton("Hello");
b.TextAlign = ContentAlignment.MiddleCenter;
contextMenuStrip1.Items.Add(b);
关于c# - 使用手动设置的高度对齐上下文菜单项垂直居中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6355919/