Trying to add a ToolStrip to a ToolStripPanel side-by-side with an existing ToolStrip

前端 未结 3 1579
你的背包
你的背包 2021-01-05 20:37

I\'m using .net 2.0 with Visual Studio 2005 and I am trying to add two different toolstrips to the top of the form such that they show up side-by-side. I want it to be like

相关标签:
3条回答
  • 2021-01-05 21:32

    System.Windows.Forms.FlowLayoutPanel can do the job.

    Just put the ToolStrip controls in it with correct order.

    0 讨论(0)
  • 2021-01-05 21:33

    I created a custom ToolStripPanel so that I could overload the LayoutEngine;

    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Layout;
    
    namespace CustomGUI
    {
      class CustomToolStripPanel : ToolStripPanel
      {
        private LayoutEngine _layoutEngine;
    
        public override LayoutEngine LayoutEngine
        {
          get
          {
            if (_layoutEngine == null) _layoutEngine = new CustomLayoutEngine();
            return _layoutEngine;
          }
        }
    
        public override Size GetPreferredSize(Size proposedSize)
        {
          Size size = base.GetPreferredSize(proposedSize);
    
          foreach(Control control in Controls)
          {
            int newHeight = control.Height + control.Margin.Vertical + Padding.Vertical;
            if (newHeight > size.Height) size.Height = newHeight;
          }
    
          return size;
        }
      }
    }
    

    Then the custom LayoutEngine lays out the ToolStrips;

    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Layout;
    
    namespace CustomGUI
    {
      class CustomLayoutEngine : LayoutEngine
      {
        public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
        {
          Control parent = container as Control;
    
          Rectangle parentDisplayRectangle = parent.DisplayRectangle;
    
          Control [] source = new Control[parent.Controls.Count];
          parent.Controls.CopyTo(source, 0);
    
          Point nextControlLocation = parentDisplayRectangle.Location;
    
          foreach (Control c in source)
          {
            if (!c.Visible) continue;
    
            nextControlLocation.Offset(c.Margin.Left, c.Margin.Top);
            c.Location = nextControlLocation;
    
            if (c.AutoSize)
            {
              c.Size = c.GetPreferredSize(parentDisplayRectangle.Size);
            }
    
            nextControlLocation.Y = parentDisplayRectangle.Y;
            nextControlLocation.X += c.Width + c.Margin.Right + parent.Padding.Horizontal;
          }
    
          return false;
        }
      }
    }
    

    One thing that took a while is that changing the location / size of one ToolStrip item will cause the layout to re-fire, with the controls reordered. So I take a copy of the controls before the layout loop. And you cant use AddRange(...) to add items to the Custom Panel for some reason - need to Add(...) them one at a time.

    hope that helps (it's based on MSDN LayoutEngine Example, fixed for ToolStripPanels)

    Wyzfen

    0 讨论(0)
  • 2021-01-05 21:34

    I think in your case you can get away with just setting the LayoutStyle on your ToolStrip to ToolStripLayoutStyle.HorizontalStackWithOverflow rather than need your own custom LayoutEngine.

    I asked a different question on a similar topic on how to handle layout with dynamic items to have better control of the overflow.

    0 讨论(0)
提交回复
热议问题