单击菜单C#后打开Panel作为ToolStrip MenuItem

时间:2014-03-07 01:52:26

标签: c# .net-4.0 panel menuitem

Visual Studio Express 2013中有特殊菜单项。
点击Panel后看起来似乎已MenuItem打开。例如:

enter image description here

如何为连接到MenuStrip的{​​{1}}创建“面板菜单项”?它只是标准MenuItem和标准隐藏MenuItem,具有基于Panel边框的动态排名?或者它是一个修改后的Form来包含Graphics和Uri?

我搜索了类似的内容,但我找不到如何在MenuItem内创建Panel,我不知道是否应该使用菜单创建普通ToolStrip,或者某处的解决方案(自定义组件)。

1 个答案:

答案 0 :(得分:1)

假设WinForms,您可以使用ToolStripControlHost来保存Panel或UserControl:

弹出窗口中显示的控件:

var panel = new Panel() {
  BackColor = Color.White,
  MinimumSize = new Size(150, 72),
  Size = MinimumSize,     
};
panel.Paint += (s, e) => {
  TextRenderer.DrawText(e.Graphics, "Pop-up Panel",
  SystemFonts.DefaultFont, panel.ClientRectangle,
  Color.Black, Color.Empty,
  TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
};

ToolStrip控件显示面板:

  var hostTool = new ToolStripControlHost(panel) {
    Padding = Padding.Empty,
    Margin = Padding.Empty
  };

  var downButton = new ToolStripDropDownButton("Panel Menu") {
    Alignment = ToolStripItemAlignment.Right,
    DisplayStyle = ToolStripItemDisplayStyle.Text,
    DropDownDirection = ToolStripDropDownDirection.BelowLeft,
  };

  ((ToolStripDropDownMenu)downButton.DropDown).ShowCheckMargin = false;
  ((ToolStripDropDownMenu)downButton.DropDown).ShowImageMargin = false;
  downButton.DropDown.AutoSize = false;
  downButton.DropDown.Size = new Size(panel.Width + 12, panel.Height + 4);
  downButton.DropDown.Items.Add(hostTool);
  var tool = new ToolStrip();
  tool.Items.Add(downButton);
  this.Controls.Add(tool);

结果:

enter image description here