目录
实践过程
效果
代码
public partial class Frm_Libretto : Form
{
public Frm_Libretto()
{
InitializeComponent();
}
#region 公共变量
FrmClass Cla_FrmClass = new FrmClass();
#endregion
private void Frm_Libretto_Load(object sender, EventArgs e)
{
this.Top = FrmClass.Example_Play_Top;
this.Left = FrmClass.Example_Play_Left + FrmClass.Example_Play_Width;
Cla_FrmClass.FrmInitialize(this);
}
private void panel_Title_MouseDown(object sender, MouseEventArgs e)
{
FrmClass.CPoint = new Point(-e.X, -e.Y);
}
private void panel_Title_MouseMove(object sender, MouseEventArgs e)
{
FrmClass.Example_Assistant_AdhereTo = false;
FrmClass.Example_Libretto_AdhereTo = false;
Cla_FrmClass.FrmMove(this, e);
}
private void panel_Title_MouseUp(object sender, MouseEventArgs e)
{
Cla_FrmClass.FrmPlace(this);
}
}
public partial class Frm_ListBox : Form
{
public Frm_ListBox()
{
InitializeComponent();
}
#region 公共变量
FrmClass Cla_FrmClass = new FrmClass();
#endregion
private void Frm_ListBox_Load(object sender, EventArgs e)
{
this.Left = FrmClass.Example_Play_Left;
this.Top = FrmClass.Example_Play_Top + FrmClass.Example_Play_Height;
Cla_FrmClass.FrmInitialize(this);
}
private void panel_Title_MouseDown(object sender, MouseEventArgs e)
{
FrmClass.CPoint = new Point(-e.X, -e.Y);
}
private void panel_Title_MouseMove(object sender, MouseEventArgs e)
{
FrmClass.Example_Assistant_AdhereTo = false;
FrmClass.Example_List_AdhereTo = false;
Cla_FrmClass.FrmMove(this, e);
}
private void panel_Title_MouseUp(object sender, MouseEventArgs e)
{
Cla_FrmClass.FrmPlace(this);
}
}
public partial class Frm_Play : Form
{
public Frm_Play()
{
InitializeComponent();
}
#region 公共变量
FrmClass Cla_FrmClass = new FrmClass();
public static Form F_List = new Form();
public static Form F_Libretto = new Form();
public static Form F_Screen = new Form();
#endregion
private void Frm_Play_Load(object sender, EventArgs e)
{
Cla_FrmClass.FrmInitialize(this);
}
private void panel_Title_MouseDown(object sender, MouseEventArgs e)
{
int Tem_Y =;
if (e.Button == MouseButtons.Left)
{
Cla_FrmClass.FrmBackCheck();
Tem_Y = e.Y;
FrmClass.FrmPoint = new Point(e.X, Tem_Y);
FrmClass.CPoint = new Point(-e.X, -Tem_Y);
if (FrmClass.Example_List_AdhereTo)
{
Cla_FrmClass.FrmDistanceJob(this, F_List);
if (FrmClass.Example_Assistant_AdhereTo)
{
Cla_FrmClass.FrmDistanceJob(this, F_Libretto);
}
}
if (FrmClass.Example_Libretto_AdhereTo)
{
Cla_FrmClass.FrmDistanceJob(this, F_Libretto);
if (FrmClass.Example_Assistant_AdhereTo)
{
Cla_FrmClass.FrmDistanceJob(this, F_List);
}
}
}
}
private void panel_Title_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cla_FrmClass.FrmMove(this, e);
if (FrmClass.Example_List_AdhereTo)
{
Cla_FrmClass.ManyFrmMove(this, e, F_List);
Cla_FrmClass.FrmInitialize(F_List);
if (FrmClass.Example_Assistant_AdhereTo)
{
Cla_FrmClass.ManyFrmMove(this, e, F_Libretto);
Cla_FrmClass.FrmInitialize(F_Libretto);
}
}
if (FrmClass.Example_Libretto_AdhereTo)
{
Cla_FrmClass.ManyFrmMove(this, e, F_Libretto);
Cla_FrmClass.FrmInitialize(F_Libretto);
if (FrmClass.Example_Assistant_AdhereTo)
{
Cla_FrmClass.ManyFrmMove(this, e, F_List);
Cla_FrmClass.FrmInitialize(F_List);
}
}
Cla_FrmClass.FrmInitialize(this);
}
}
private void panel_Title_MouseUp(object sender, MouseEventArgs e)
{
Cla_FrmClass.FrmPlace(this);
}
private void Frm_Play_Shown(object sender, EventArgs e)
{
F_List = new Frm_ListBox();
F_List.ShowInTaskbar = false;
FrmClass.Example_ListShow = true;
F_List.Show();
F_Libretto = new Frm_Libretto();
F_Libretto.ShowInTaskbar = false;
FrmClass.Example_LibrettoShow = true;
F_Libretto.Show();
F_Libretto.Left = this.Left + this.Width;
F_Libretto.Top = this.Top;
Cla_FrmClass.FrmInitialize(F_List);
Cla_FrmClass.FrmInitialize(F_Libretto);
}
private void panel_Close_Click(object sender, EventArgs e)
{
F_List.Close();
F_List.Dispose();
F_Libretto.Close();
F_Libretto.Dispose();
F_Screen.Close();
F_Screen.Dispose();
this.Close();
}
private void panel_Title_Click(object sender, EventArgs e)
{
F_List.Focus();
F_Libretto.Focus();
this.Focus();
}
}
class FrmClass
{
#region 磁性窗体-公共变量
public static bool Example_ListShow = false;
public static bool Example_LibrettoShow = false;
public static bool Example_ScreenShow = false;
public static Point CPoint;
public static Point FrmPoint;
public static int Example_FSpace =;
public static int Example_Play_Top =;
public static int Example_Play_Left =;
public static int Example_Play_Width =;
public static int Example_Play_Height =;
public static bool Example_Assistant_AdhereTo = false;
public static int Example_List_Top =;
public static int Example_List_Left =;
public static int Example_List_Width =;
public static int Example_List_Height =;
public static bool Example_List_AdhereTo = false;
public static int Example_Libretto_Top =;
public static int Example_Libretto_Left =;
public static int Example_Libretto_Width =;
public static int Example_Libretto_Height =;
public static bool Example_Libretto_AdhereTo = false;
public static int Example_List_space_Top =;
public static int Example_List_space_Left =;
public static int Example_Libretto_space_Top =;
public static int Example_Libretto_space_Left =;
#endregion
#region 检测各窗体是否连接在一起
public void FrmBackCheck()
{
bool Tem_Magnetism = false;
Tem_Magnetism = false;
if ((Example_Play_Top - Example_List_Top) ==)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_List_Left) ==)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_List_Left - Example_List_Width) ==)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_List_Left + Example_List_Width) ==)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_List_Top - Example_List_Height) ==)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_List_Top + Example_List_Height) ==)
Tem_Magnetism = true;
if (Tem_Magnetism)
Example_List_AdhereTo = true;
Tem_Magnetism = false;
if ((Example_Play_Top - Example_Libretto_Top) ==)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_Libretto_Left) ==)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_Libretto_Left - Example_Libretto_Width) ==)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_Libretto_Left + Example_Libretto_Width) ==)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_Libretto_Top - Example_Libretto_Height) ==)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_Libretto_Top + Example_Libretto_Height) ==)
Tem_Magnetism = true;
if (Tem_Magnetism)
Example_Libretto_AdhereTo = true;
Tem_Magnetism = false;
if ((Example_List_Top - Example_Libretto_Top) ==)
Tem_Magnetism = true;
if ((Example_List_Left - Example_Libretto_Left) ==)
Tem_Magnetism = true;
if ((Example_List_Left - Example_Libretto_Left - Example_Libretto_Width) ==)
Tem_Magnetism = true;
if ((Example_List_Left - Example_Libretto_Left + Example_Libretto_Width) ==)
Tem_Magnetism = true;
if ((Example_List_Top - Example_Libretto_Top - Example_Libretto_Height) ==)
Tem_Magnetism = true;
if ((Example_List_Top - Example_Libretto_Top + Example_Libretto_Height) ==)
Tem_Magnetism = true;
if (Tem_Magnetism)
Example_Assistant_AdhereTo = true;
}
#endregion
#region 利用窗体上的控件移动窗体
public void FrmMove(Form Frm, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point myPosittion = Control.MousePosition;
myPosittion.Offset(CPoint.X, CPoint.Y);
Frm.DesktopLocation = myPosittion;
}
}
#endregion
#region 计算窗体之间的距离差
public void FrmDistanceJob(Form Frm, Form Follow)
{
switch (Follow.Name)
{
case "Frm_ListBox":
{
Example_List_space_Top = Follow.Top - Frm.Top;
Example_List_space_Left = Follow.Left - Frm.Left;
break;
}
case "Frm_Libretto":
{
Example_Libretto_space_Top = Follow.Top - Frm.Top;
Example_Libretto_space_Left = Follow.Left - Frm.Left;
break;
}
}
}
#endregion
#region 磁性窗体的移动
public void
ManyFrmMove(Form Frm, MouseEventArgs e, Form Follow)
{
if (e.Button == MouseButtons.Left)
{
int Tem_Left =;
int Tem_Top =;
Point myPosittion = Control.MousePosition;
switch (Follow.Name)
{
case "Frm_ListBox":
{
Tem_Top = Example_List_space_Top - FrmPoint.Y;
Tem_Left = Example_List_space_Left - FrmPoint.X;
break;
}
case "Frm_Libretto":
{
Tem_Top = Example_Libretto_space_Top - FrmPoint.Y;
Tem_Left = Example_Libretto_space_Left - FrmPoint.X;
break;
}
}
myPosittion.Offset(Tem_Left, Tem_Top);
Follow.DesktopLocation = myPosittion;
}
}
#endregion
#region 对窗体的位置进行初始化
public void FrmInitialize(Form Frm)
{
switch (Frm.Name)
{
case "Frm_Play":
{
Example_Play_Top = Frm.Top;
Example_Play_Left = Frm.Left;
Example_Play_Width = Frm.Width;
Example_Play_Height = Frm.Height;
break;
}
case "Frm_ListBox":
{
Example_List_Top = Frm.Top;
Example_List_Left = Frm.Left;
Example_List_Width = Frm.Width;
Example_List_Height = Frm.Height;
break;
}
case "Frm_Libretto":
{
Example_Libretto_Top = Frm.Top;
Example_Libretto_Left = Frm.Left;
Example_Libretto_Width = Frm.Width;
Example_Libretto_Height = Frm.Height;
break;
}
}
}
#endregion
#region 存储各窗体的当前信息
public void FrmPlace(Form Frm)
{
FrmInitialize(Frm);
FrmMagnetism(Frm);
}
#endregion
#region 窗体的磁性设置
public void FrmMagnetism(Form Frm)
{
if (Frm.Name != "Frm_Play")
{
FrmMagnetismCount(Frm, Example_Play_Top, Example_Play_Left, Example_Play_Width, Example_Play_Height,
"Frm_Play");
}
if (Frm.Name != "Frm_ListBos")
{
FrmMagnetismCount(Frm, Example_List_Top, Example_List_Left, Example_List_Width, Example_List_Height,
"Frm_ListBos");
}
if (Frm.Name != "Frm_Libratto")
{
FrmMagnetismCount(Frm, Example_Libretto_Top, Example_Libretto_Left, Example_Libretto_Width,
Example_Libretto_Height, "Frm_Libratto");
}
FrmInitialize(Frm);
}
#endregion
#region 磁性窗体的计算
public void FrmMagnetismCount(Form Frm, int top, int left, int width, int height, string Mforms)
{
bool Tem_Magnetism = false;
string Tem_MainForm = "";
string Tem_AssistForm = "";
if ((Frm.Top + Frm.Height - top) <= Example_FSpace && (Frm.Top + Frm.Height - top) >= -Example_FSpace)
{
if ((Frm.Left >= left && Frm.Left <= (left + width)) ||
((Frm.Left + Frm.Width) >= left && (Frm.Left + Frm.Width) <= (left + width)))
{
Frm.Top = top - Frm.Height;
if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
Frm.Left = left;
Tem_Magnetism = true;
}
if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width))
{
Frm.Top = top - Frm.Height;
if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
Frm.Left = left;
Tem_Magnetism = true;
}
}
if ((Frm.Top - (top + height)) <= Example_FSpace && (Frm.Top - (top + height)) >= -Example_FSpace)
{
if ((Frm.Left >= left && Frm.Left <= (left + width)) ||
((Frm.Left + Frm.Width) >= left && (Frm.Left + Frm.Width) <= (left + width)))
{
Frm.Top = top + height;
if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
Frm.Left = left;
Tem_Magnetism = true;
}
if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width))
{
Frm.Top = top + height;
if ((Frm.Left - left) <= Example_FSpace && (Frm.Left - left) >= -Example_FSpace)
Frm.Left = left;
Tem_Magnetism = true;
}
}
if ((Frm.Left + Frm.Width - left) <= Example_FSpace && (Frm.Left + Frm.Width - left) >= -Example_FSpace)
{
if ((Frm.Top > top && Frm.Top <= (top + height)) ||
((Frm.Top + Frm.Height) >= top && (Frm.Top + Frm.Height) <= (top + height)))
{
Frm.Left = left - Frm.Width;
if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
Frm.Top = top;
Tem_Magnetism = true;
}
if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height))
{
Frm.Left = left - Frm.Width;
if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
Frm.Top = top;
Tem_Magnetism = true;
}
}
if ((Frm.Left - (left + width)) <= Example_FSpace && (Frm.Left - (left + width)) >= -Example_FSpace)
{
if ((Frm.Top > top && Frm.Top <= (top + height)) ||
((Frm.Top + Frm.Height) >= top && (Frm.Top + Frm.Height) <= (top + height)))
{
Frm.Left = left + width;
if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
Frm.Top = top;
Tem_Magnetism = true;
}
if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height))
{
Frm.Left = left + width;
if ((Frm.Top - top) <= Example_FSpace && (Frm.Top - top) >= -Example_FSpace)
Frm.Top = top;
Tem_Magnetism = true;
}
}
if (Frm.Name == "Frm_Play")
Tem_MainForm = "Frm_Play";
else
Tem_AssistForm = Frm.Name;
if (Mforms == "Frm_Play")
Tem_MainForm = "Frm_Play";
else
Tem_AssistForm = Mforms;
if (Tem_MainForm == "")
{
Example_Assistant_AdhereTo = Tem_Magnetism;
}
else
{
switch (Tem_AssistForm)
{
case "Frm_ListBos":
Example_List_AdhereTo = Tem_Magnetism;
break;
case "Frm_Libratto":
Example_Libretto_AdhereTo = Tem_Magnetism;
break;
}
}
}
#endregion
#region 恢复窗体的初始大小
public void FrmScreen_FormerlySize(Form Frm, int PWidth, int PHeight)
{
if (Frm.Width < PWidth || Frm.Height < PHeight)
{
Frm.Width = PWidth;
Frm.Height = PHeight;
}
}
#endregion
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Frm_Play());
}
}