简体   繁体   English

如何在ToolStripMenuItem中删除右侧空白区域

[英]How to remove right side empty space in ToolStripMenuItem

SS

In screenshot i marked that empty space with green rectangle, i want left and right space to be equal size in ToolStripMenuItem but right side have bigger empty area which i can't remove. 在屏幕截图中,我用绿色矩形标记了空白空间,我希望在ToolStripMenuItem中左右空间大小相等,但右侧有更大的空白区域,我无法删除。

Codes: 代码:

    private void UpdateWorkflowsMenu()
    {
        ((ToolStripDropDownMenu)tsddbWorkflows.DropDown).ShowImageMargin = false;

        tsddbWorkflows.DropDownItems.Clear();

        Program.HotkeyManager.Hotkeys.ForEach<HotkeySettings>(x =>
        {
            if (x.TaskSettings.Job != HotkeyType.None && (!Program.Settings.WorkflowsOnlyShowEdited || !x.TaskSettings.IsUsingDefaultSettings))
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(x.TaskSettings.Description);
                if (x.HotkeyInfo.IsValidHotkey) tsmi.ShortcutKeyDisplayString = " " + x.HotkeyInfo.ToString();
                tsmi.Click += (sender, e) => HandleTask(x.TaskSettings);
                tsddbWorkflows.DropDownItems.Add(tsmi);
            }
        });

        tsddbWorkflows.Visible = tsddbWorkflows.DropDownItems.Count > 0;
    }

VB version (actually there are 18 pixels for arrow: 10 for size and 8 for margin, leave 2 pixels for margin) VB版本(实际上箭头有18个像素:10个用于大小,8个用于边距,保留2个像素用于边距)

Parent.DropDown.GetType.GetField("ArrowPadding", 
Reflection.BindingFlags.NonPublic Or 
Reflection.BindingFlags.Static).SetValue(Nothing, New Padding(0, 0, -16, 0))

As answered above this space is reserved for 'open submenu' arrow, and in general I do not recommend to touch this, but of course it is possible to remove that space. 如上所述,这个空间是为“打开子菜单”箭头保留的,一般我不建议触摸它,但当然可以删除该空间。 And actually there is several methods to do that, but all of them require some coding. 实际上有几种方法可以做到这一点,但所有这些方法都需要一些编码。 Here the snippet for simplest way to do that, you must know expected width however (it can be calculated via ToolstripItem.GetPreferredSize): 这里是最简单的方法,你必须知道预期的宽度(它可以通过ToolstripItem.GetPreferredSize计算):

private void RecentButton_DropDownOpening(object sender, EventArgs e)
{
  ToolStripDropDownItem RecentButton = (ToolStripDropDownItem)sender;
  RecentButton.DropDown.SuspendLayout();
  try
  {
    RecentButton.DropDownItems.Clear();

    // Populate items

    RecentButton.DropDown.MinimumSize = new Size(RecentButton.Bounds.Right - DisplayRectangle.Left, 0);
    RecentButton.DropDown.MaximumSize = RecentButton.DropDown.MinimumSize;
  }
  finally
  {
    RecentButton.DropDown.ResumeLayout();
  }
}

ToolStip engine in general very flexible and it is possible to implement very interesting things using it when you know its internals. ToolStip引擎通常非常灵活,当您了解其内部结构时,可以使用它来实现非常有趣的事情。

Converted Ark 's answer to C#: 转换方舟对C#的回答:

public static void HideArrowMargin(this ToolStripDropDownItem tsddi)
{
    tsddi.DropDown.GetType().GetField("ArrowPadding", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, new Padding(0, 0, -14, 0));
}

public static void HideImageMargin(this ToolStripDropDownItem tsddi)
{
    ((ToolStripDropDownMenu)tsddi.DropDown).ShowImageMargin = false;
}

Using extension for it, that way I can use it like this in multiple places: 使用扩展,这样我可以在多个地方使用它:

tsddbWorkflows.HideImageMargin();
tsddbWorkflows.HideArrowMargin();

Edit: 编辑:

I noticed now because it is static field, it removes arrow padding from all controls. 我现在注意到它是静态字段,它从所有控件中删除箭头填充。 So this is not decent solution too. 所以这也不是一个好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM