自定义一个控件,如何能让他成为一个类似pannel一样的容器,并且具有ToolStrip那种停靠在上方并且不会被其他容器挤压或者占用他已经占用的区域
求大神

解决方案 »

  1.   

    继承pannel类只解决了他是个容器,
    最难搞的是他如何能像类似于ToolStrip一样停靠在最上面
      

  2.   

    当我们拖一个pannel到设计器的时候,所有的控件会被Toolstrip往下挤25个像素,所以自定义个控件想完成这种功能,表示很无助啊,以前没做过CS的东西,不太会弄,搞半下午了没搞定
      

  3.   

    说错了
    拖一个pannel到设计器不会有像拖一个ToolStrip把所有元素往下挤25个像素这种效果,现在想自定义一个控件继承自pannel   并且有ToolStrip的这种功能
      

  4.   

    1.重写Panel控件  如楼主所说 解决容器问题
    2.理解“设计时(DesignTime)”和“运行时(RunTime)”的概念
      在OnLoad方法中 处理停靠、改变其他控件的位置
      protected override OnLoad(...)
      {
          if(DesignMode)  //控件处于 “设计时”  如果是“运行时” 就不需要了
          {
               //处理停靠、改变其他控件位置
               
           }
      }
    运行时  设计时 参考http://www.cnblogs.com/xiaozhi_5638/archive/2013/06/10/3131019.html
      

  5.   

    楼主 不好意思  刚才试了一下  7#的解决方案 只是在设计器中有效果  运行后无效果  完整的解决方法如下:using System.ComponentModel.Design;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    namespace Gravity
    {
        [Designer(typeof(MyPanelDesigner))]
        class MyPanel : Panel
        {
            public MyPanel()
            {
                Dock = DockStyle.Top;
                BackColor = System.Drawing.Color.Red;
            }
        }
        class MyPanelDesigner : ControlDesigner
        {
            public override void Initialize(IComponent component)
            {
                base.Initialize(component);
                IDesignerHost ids = GetService(typeof(IDesignerHost)) as IDesignerHost;            Control c = this.Control; //当前被设计的组件  MyPanel
                Control parent = ids.RootComponent as Control; //父窗体            MemberDescriptor aMem_Controls = TypeDescriptor.GetProperties(parent)["Controls"];
                RaiseComponentChanging(aMem_Controls); //通知设计器 parent中的子控件发生变化
                parent.Size = new System.Drawing.Size(parent.Width, parent.Height + c.Height); //父控件(窗体)高度增加
                foreach (Control cc in parent.Controls) //改变父控件中每一个子控件位置
                {
                    cc.Location = new System.Drawing.Point(cc.Location.X, cc.Location.Y + c.Height); 
                }
                RaiseComponentChanged(aMem_Controls,null,null); //通知设计器 parent中子控件以及发生变化 设计器会自动生成代码
            }
     
        }
    }