简体   繁体   English

如何以编程方式添加菜单带走垂直空间?

[英]How to take away vertical space for programmatically added menu?

I add a MenuStrip to my form and I would like to add other controls below it like usual Point(0, 0) is the top left corner of empty form space. 我在我的表单中添加了一个MenuStrip ,我想在它下面添加其他控件,就像通常的Point(0,0)是空表单空间的左上角一样。 After I add the menu to my form and add more controls they overlap each other. 将菜单添加到表单并添加更多控件后,它们相互重叠。 So I want to take away some height of the client rect for the menu and a button with Location = (0,0) must be RIGHT below the menu. 因此,我想取消菜单中客户端矩形的某个高度,并且Location = (0,0)的按钮必须在菜单下方右侧

How do I do that ? 我怎么做 ?

If I assign a MainMenu to Menu property of the form it does that automatically but I really want and need to use MenuStrip. 如果我将一个MainMenu分配给表单的Menu属性,它会自动执行,但我真的想要并且需要使用MenuStrip。


Edit: this doesn't work: 编辑:这不起作用:

 MenuStrip menu = new MenuStrip(); menu.Items.Add("File"); menu.AutoSize = false; menu.Height = 50; menu.Dock = DockStyle.Top; MainMenuStrip = menu; Controls.Add(menu); Button b = new Button(); b.Text = "hello world"; b.SetBounds(0, 25, 128, 50); Controls.Add(b); 

While this works like I would like it to do with MenuStrip: 虽然这与我想用MenuStrip一样有效:

 Menu = new MainMenu(); Menu.MenuItems.Add("File"); Button b = new Button(); b.Text = "hello world"; b.SetBounds(0, 0, 128, 50); Controls.Add(b); 

When you SetBounds(0, 25, 128, 50) , you are actually setting b.Top to 25 (the second parameter). 当您设置SetBounds(0, 25, 128, 50) b.Top SetBounds(0, 25, 128, 50) ,实际上是将b.Top设置为25 (第二个参数)。 In order to set the top bound relative to the menu control, use: 要设置相对于menu控件的上限,请使用:

b.SetBounds(0, menu.Bottom, 128, 50);

[UPDATE] [UPDATE]

Alternatively, you could use: 或者,您可以使用:

public partial class Form1 : Form
{
    private int menuStripHeight = 50;

    public Form1()
    {
        InitializeComponent();
        this.ControlAdded += Form1_ControlAdded;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        MenuStrip menu = new MenuStrip();
        menu.Items.Add("File");
        menu.AutoSize = false;
        menu.Height = menuStripHeight; ;
        menu.Dock = DockStyle.Top;
        MainMenuStrip = menu;
        Controls.Add(menu);

        Button b = new Button();
        b.Text = "hello world";

        // note that the position used is 0,0
        b.SetBounds(0, 0, 128, 50);

        Controls.Add(b);
    }

    void Form1_ControlAdded(object sender, ControlEventArgs e)
    {
        if (e.Control.GetType().FullName != "System.Windows.Forms.MenuStrip")
            e.Control.Top += menuStripHeight;
    }
}

[UPDATE 2] [更新2]

Or you could just use a Panel : 或者您可以使用Panel

MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);

Panel p = new Panel();
p.SetBounds(0, menuStripHeight, this.Width, this.Height);
Controls.Add(p);

Button b = new Button();
b.Text = "hello world";
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);

Use DockStyle.Top in both MenuStrip and Panel, but add them in the reverse order. 在MenuStrip和Panel中使用DockStyle.Top,但以相反的顺序添加它们。 Adding a control with Dock=Top puts this last control closest to the border, that is, on top of all the other controls. 使用Dock = Top添加控件会使最后一个控件最靠近边框,即在所有其他控件之上。 So without resorting to private constants and event handlers: 所以不使用私有常量和事件处理程序:

MenuStrip menu = new MenuStrip() {
  AutoSize = false,
  Dock = DockStyle.Top
};
menu.Items.Add("File");

Panel p = new Panel(){
   Dock = DockStyle.Top
};

Controls.Add(p);
Controls.Add(menu);
MainMenuStrip = menu;

Button b = new Button(){
  Text = "hello world"
};
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);

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

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