У меня есть что-то вроде серьезной проблемы. Я создал UserControl, который движется (как окно) в своем родителе, используя события MouseDown, MouseMove,MouseUp
. [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
Атрибут используется, чтобы добавить элементы управления в этот UserControl в VS's Designer.Дети UserControl исчезают после перемещения
Состояние:
- Перемещение этих 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 }
Итак, как я могу это исправить?
Вы могли бы опубликовать класс UserControl? – Rariolu