C# UserControl的子项在移动后消失

C# UserControl的子项在移动后消失,c#,winforms,user-controls,C#,Winforms,User Controls,我有个严重的问题。 通过使用MouseDown、MouseMove、MouseUp事件,我制作了一个用户控件,它可以在其父控件中移动(就像一个窗口)。 使用[Designer(“System.Windows.Forms.Design.ParentControlDesigner,System.Design”,typeof(IDesigner))]属性,我可以在VS的设计器中将控件添加到此用户控件 声明: 移动这些UserControls效果很好(Usercontrol按预期移动…) 控件可以添加

我有个严重的问题。 通过使用
MouseDown、MouseMove、MouseUp
事件,我制作了一个用户控件,它可以在其父控件中移动(就像一个窗口)。 使用
[Designer(“System.Windows.Forms.Design.ParentControlDesigner,System.Design”,typeof(IDesigner))]
属性,我可以在VS的设计器中将控件添加到此用户控件

声明:

  • 移动这些UserControls效果很好(Usercontrol按预期移动…)
  • 控件可以添加到VS的设计器中,并在运行时按设计显示[可见,就像它应该显示的那样]
  • 通过移动UserControl,子控件将不可见,但是
    .Visible=true
    不会改变
  • .BringToFront()没有影响(我想它们可能在容器后面)
下面是UserControl类:

  [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class MovableContainer : UserControl
    {
        bool mdown = false;
        Point mpos;
        [EditorBrowsable(EditorBrowsableState.Always)]
        [SettingsBindable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Axis Rasta { get; set; }
        public static int DefautlRasta = 10;
        public MovableContainer()
        {
            rasta = DefautlRasta;
            InitializeComponent();
            this.MouseDown += ((object o, MouseEventArgs e) =>
            {
                mdown = true;
                mpos = this.PointToClient(MousePosition);
            });
            this.MouseUp += ((object o, MouseEventArgs e) => mdown = false);
            this.MouseMove += MovableContainer_MouseMove;
            this.Paint += (object o, PaintEventArgs e) =>
            {
                Console.WriteLine("BTF");
                this.Parent.Controls.OfType<Control>().ToList().ForEach(x => x.BringToFront());
                this.Controls.OfType<Control>().ToList().ForEach(x => x.BringToFront());
                this.Controls.OfType<Control>().ToList().ForEach(x => x.Show());

            };

            this.ParentChanged += ((object o, EventArgs e) =>
                {
                    if (this.Parent == null)
                    {
                        try { this.Parent.SizeChanged -= Parent_SizeChanged; }
                        catch { }
                    }
                    else
                    {
                        try { this.Parent.SizeChanged += Parent_SizeChanged; }
                        catch { }
                    }
                }
                );

            // this.KeyDown += ((object o, KeyEventArgs e) => {
            ///kdown = (RastaKey == e.KeyCode); Console.WriteLine("K:"+kdown);
            //});

            //this.KeyUp += ((object o, KeyEventArgs e) => kdown = false);
        }

        void Parent_SizeChanged(object sender, EventArgs e)
        {
            this.Boundis = new Rectangle(Parent.Padding.Left, Parent.Padding.Top, Parent.Size.Width - Parent.Padding.Horizontal, Parent.Size.Height - Parent.Padding.Vertical);
            {
                this.Location = this.Location.Add(this.PointToClient(MousePosition).Sub(mpos)).Rasta(Rasta, rasta);
                Rectangle rct = new Rectangle(this.Location, this.Size);
                if (this.Boundis.X > rct.X)
                {
                    this.Location = new Point(this.Boundis.X, this.Location.Y);
                    Console.Write("R");
                }
                //left
                if (this.Boundis.Right < rct.Right)
                {
                    this.Location = new Point(this.Boundis.Right - rct.Width, rct.Y);
                    Console.Write("L");
                }
                //top
                if (this.Boundis.Y > rct.Y)
                {
                    this.Location = new Point(rct.X, this.Boundis.Y);
                    Console.Write("T");
                }
                //bottom
                if (this.Boundis.Bottom < rct.Bottom)
                {
                    this.Location = new Point(rct.X, this.Boundis.Bottom - rct.Height);
                    Console.Write("B");

                }
                Console.WriteLine();
            }
        }


        void MovableContainer_MouseMove(object sender, MouseEventArgs e)
        {
            if (mdown)
            {
                this.Location = this.Location.Add(this.PointToClient(MousePosition).Sub(mpos)).Rasta(Rasta, rasta);
                Rectangle rct = new Rectangle(this.Location, this.Size);
                if (this.Boundis.X > rct.X)
                {
                    this.Location = new Point(this.Boundis.X, this.Location.Y);
                    Console.Write("R");
                }
                //left
                if (this.Boundis.Right < rct.Right)
                {
                    this.Location = new Point(this.Boundis.Right - rct.Width, rct.Y);
                    Console.Write("L");
                }
                //top
                if (this.Boundis.Y > rct.Y)
                {
                    this.Location = new Point(rct.X, this.Boundis.Y);
                    Console.Write("T");
                }
                //bottom
                if (this.Boundis.Bottom < rct.Bottom)
                {
                    this.Location = new Point(rct.X, this.Boundis.Bottom - rct.Height);
                    Console.Write("B");

                }
                Console.WriteLine();

            }

        }
        public Rectangle Boundis { get; set; }

    }
    public enum Axis { X, Y, None }
[设计器(“System.Windows.Forms.Design.ParentControlDesigner,System.Design”,typeof(IDesigner))]
公共部分类MovableContainer:UserControl
{
bool mdown=false;
mpos点;
[EditorBrowsable(EditorBrowsableState.Always)]
[设置易懂(正确)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共轴Rasta{get;set;}
公共静态int DefautlRasta=10;
公共可移动容器()
{
拉斯塔=德乌特拉斯塔;
初始化组件();
this.MouseDown+=((对象o,MouseEventArgs e)=>
{
mdown=true;
mpos=this.PointToClient(MousePosition);
});
this.MouseUp+=((对象o,MouseEventArgs e)=>mdown=false);
this.MouseMove+=MovableContainer\u MouseMove;
this.Paint+=(对象o,PaintEventArgs e)=>
{
控制台写入线(“BTF”);
this.Parent.Controls.OfType().ToList().ForEach(x=>x.BringToFront());
this.Controls.OfType().ToList().ForEach(x=>x.BringToFront());
this.Controls.OfType().ToList().ForEach(x=>x.Show());
};
this.ParentChanged+=((对象o,事件参数e)=>
{
if(this.Parent==null)
{
请尝试{this.Parent.SizeChanged-=Parent\u SizeChanged;}
捕获{}
}
其他的
{
请尝试{this.Parent.SizeChanged+=Parent\u SizeChanged;}
捕获{}
}
}
);
//this.KeyDown+=((对象o,KeyEventArgs e)=>{
///kdown=(RastaKey==e.KeyCode);Console.WriteLine(“K:+kdown”);
//});
//this.KeyUp+=((对象o,KeyEventArgs e)=>kdown=false);
}
无效父项大小已更改(对象发送方,事件参数e)
{
this.Boundis=新矩形(Parent.Padding.Left、Parent.Padding.Top、Parent.Size.Width-Parent.Padding.Horizontal、Parent.Size.Height-Parent.Padding.Vertical);
{
this.Location=this.Location.Add(this.PointToClient(MousePosition).Sub(mpos)).Rasta(Rasta,Rasta);
矩形rct=新矩形(this.Location,this.Size);
if(this.Boundis.X>rct.X)
{
this.Location=新点(this.Boundis.X,this.Location.Y);
控制台。写入(“R”);
}
//左
如果(this.Boundis.Rightrct.Y)
{
this.Location=新点(rct.X,this.Boundis.Y);
控制台。写入(“T”);
}
//底部
如果(this.Boundis.Bottomrct.X)
{
this.Location=新点(this.Boundis.X,this.Location.Y);
控制台。写入(“R”);
}
//左
如果(this.Boundis.Rightrct.Y)
{
this.Location=新点(rct.X,this.Boundis.Y);
控制台。写入(“T”);
}
//底部
如果(this.Boundis.Bottom

那么,我该如何解决这个问题呢?

坦率地说,你发布的代码一团糟——你放在那里的大部分东西都没有意义。从我看到的情况来看,您正在尝试使用剪裁实现运行时可移动容器。一个简单的继承的
面板
也可以做同样的事情,而不需要那些设计器属性等
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class MovableContainer : UserControl
{
    bool mdown = false;
    Point mpos;
    int rasta;
    Control parent;

    [EditorBrowsable(EditorBrowsableState.Always)]
    [SettingsBindable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Axis Rasta { get; set; }
    public static int DefautlRasta = 10;
    public MovableContainer()
    {
        rasta = DefautlRasta;
        InitializeComponent();
        this.MouseDown += (sender, e) =>
        {
            mdown = true;
            mpos = e.Location;
        };
        this.MouseUp += (sender, e) =>
        {
            mdown = false;
        };
        this.MouseMove += (sender, e) =>
        {
            if (mdown)
                SetLocation(this.Location.Add(e.Location.Sub(mpos)).Rasta(Rasta, rasta));
        };
        EventHandler onParentSizeChanged = (sender, e) =>
        {
            SetLocation(this.Location);
        };
        this.ParentChanged += (sender, e) =>
        {
            if (parent != null) parent.SizeChanged -= onParentSizeChanged;
            parent = Parent;
            if (parent != null) parent.SizeChanged += onParentSizeChanged;
        };
    }
    private void SetLocation(Point location)
    {
        var rect = new Rectangle(location, Size);
        var clipRect = Parent.DisplayRectangle;
        if (rect.Right > clipRect.Right) rect.X -= (rect.Right - clipRect.Right);
        if (rect.X < clipRect.X) rect.X = clipRect.X;
        if (rect.Bottom > clipRect.Bottom) rect.Y -= (rect.Bottom - clipRect.Bottom);
        if (rect.Y < clipRect.Y) rect.Y = clipRect.Y;
        location = rect.Location;
        if (this.Location == location) return;
        this.Location = location;
    }

}
public enum Axis { X, Y, None }
// You haven't provided these, so I'm guessing by the usage 
static class Utils
{
    public static Point Add(this Point left, Point right)
    {
        return new Point(left.X + right.X, left.Y + right.Y);
    }
    public static Point Sub(this Point left, Point right)
    {
        return new Point(left.X - right.X, left.Y - right.Y);
    }
    public static Point Rasta(this Point pt, Axis axis, int value)
    {
        // Have absolutely no idea what is this about
        return pt;
    }
}