void GetLocation()//定位子窗体 { if (ChildForm == null) { return; } //自动判断 if (Pos != MagneticPosition.BottomUp) { if (ChildForm.Left < 0) { Pos = MagneticPosition.Right; } else { if (MainForm.Left - ChildForm.Width >= 50) { Pos = MagneticPosition.Left; } } if (ChildForm.Right > DesktopWidth) { Pos = MagneticPosition.Left; } } //if (ChildForm.Top < 0) Pos = MagneticPosition.Bottom; //if (ChildForm.Bottom > DesktopHeight) Pos = MagneticPosition.Top; if (Pos == MagneticPosition.Left) { LocationPt = new Point(MainForm.Left - ChildForm.Width, MainForm.Top); } else if (Pos == MagneticPosition.Top) { LocationPt = new Point(MainForm.Left, MainForm.Top - ChildForm.Height); } else if (Pos == MagneticPosition.Right) { LocationPt = new Point(MainForm.Right, MainForm.Top); } else if (Pos == MagneticPosition.Bottom) { LocationPt = new Point(MainForm.Left, MainForm.Bottom); } else if (Pos == MagneticPosition.BottomUp) { int height; try { var v = (Form1)MainForm; height = MainForm.Top + v.splitContainer1.Location.Y + v.splitContainer1.SplitterDistance + 97 - LengthMessage.Height; } catch { height = MainForm.Height - LengthMessage.Height - 30; } LocationPt = new Point(MainForm.Location.X + MainForm.Width / 2 - LengthMessage.Width / 2, height);// } ChildForm.Location = LocationPt; }
public MagneticMagnager(Form _MainForm, Form _ChildForm, MagneticPosition _pos) { //IsFirstPos = false; step = 20; MainForm = _MainForm; ChildForm = _ChildForm; Pos = _pos; MainForm.LocationChanged += new EventHandler(MainForm_LocationChanged); //ChildForm.LocationChanged += new EventHandler(ChildForm_LocationChanged); MainForm.SizeChanged += new EventHandler(MainForm_SizeChanged); ChildForm.SizeChanged += new EventHandler(ChildForm_SizeChanged); ChildForm.Load += new EventHandler(ChildForm_Load); MainForm.Load += new EventHandler(MainForm_Load); }