基于C#實(shí)現(xiàn)磁性吸附窗體
更新時(shí)間:2022年12月31日 10:27:41 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)磁性吸附窗體,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過(guò)程
效果

代碼
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 = 0;
if (e.Button == MouseButtons.Left) //按下的是否為鼠標(biāo)左鍵
{
Cla_FrmClass.FrmBackCheck(); //檢測(cè)各窗體是否連在一起
Tem_Y = e.Y;
FrmClass.FrmPoint = new Point(e.X, Tem_Y); //獲取鼠標(biāo)在窗體上的位置,用于磁性窗體
FrmClass.CPoint = new Point(-e.X, -Tem_Y); //獲取鼠標(biāo)在屏幕上的位置,用于窗體的移動(dòng)
if (FrmClass.Example_List_AdhereTo) //如果與frm_ListBox窗體相連接
{
Cla_FrmClass.FrmDistanceJob(this, F_List); //計(jì)算窗體的距離差
if (FrmClass.Example_Assistant_AdhereTo) //兩個(gè)輔窗體是否連接在一起
{
Cla_FrmClass.FrmDistanceJob(this, F_Libretto); //計(jì)算窗體的距離差
}
}
if (FrmClass.Example_Libretto_AdhereTo) //如果與frm_Libretto窗體相連接
{
Cla_FrmClass.FrmDistanceJob(this, F_Libretto); //計(jì)算窗體的距離差
if (FrmClass.Example_Assistant_AdhereTo) //兩個(gè)輔窗體是否連接在一起
{
Cla_FrmClass.FrmDistanceJob(this, F_List); //計(jì)算窗體的距離差
}
}
}
}
private void panel_Title_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) //按下的是否為鼠標(biāo)左鍵
{
Cla_FrmClass.FrmMove(this, e); //利用控件移動(dòng)窗體
if (FrmClass.Example_List_AdhereTo) //如果frm_ListBox窗體與主窗體連接
{
Cla_FrmClass.ManyFrmMove(this, e, F_List); //磁性窗體的移動(dòng)
Cla_FrmClass.FrmInitialize(F_List); //對(duì)frm_ListBox窗體的位置進(jìn)行初始化
if (FrmClass.Example_Assistant_AdhereTo) //如果兩個(gè)子窗體連接在一起
{
Cla_FrmClass.ManyFrmMove(this, e, F_Libretto);
Cla_FrmClass.FrmInitialize(F_Libretto);
}
}
if (FrmClass.Example_Libretto_AdhereTo) //如果frm_Libretto窗體與主窗體連接
{
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;
//記錄鼠標(biāo)的當(dāng)前位置
public static Point CPoint; //添加命名空間using System.Drawing;
public static Point FrmPoint;
public static int Example_FSpace = 10; //設(shè)置窗體間的距離
//Frm_Play窗體的位置及大小
public static int Example_Play_Top = 0;
public static int Example_Play_Left = 0;
public static int Example_Play_Width = 0;
public static int Example_Play_Height = 0;
public static bool Example_Assistant_AdhereTo = false; //輔助窗體是否磁性在一起
//Frm_ListBos窗體的位置及大小
public static int Example_List_Top = 0;
public static int Example_List_Left = 0;
public static int Example_List_Width = 0;
public static int Example_List_Height = 0;
public static bool Example_List_AdhereTo = false; //輔助窗體是否與主窗體磁性在一起
//Frm_Libretto窗體的位置及大小
public static int Example_Libretto_Top = 0;
public static int Example_Libretto_Left = 0;
public static int Example_Libretto_Width = 0;
public static int Example_Libretto_Height = 0;
public static bool Example_Libretto_AdhereTo = false; //輔助窗體是否與主窗體磁性在一起
//窗體之間的距離差
public static int Example_List_space_Top = 0;
public static int Example_List_space_Left = 0;
public static int Example_Libretto_space_Top = 0;
public static int Example_Libretto_space_Left = 0;
#endregion
#region 檢測(cè)各窗體是否連接在一起
/// <summary>
/// 檢測(cè)各窗體是否連接在一起
/// </summary>
public void FrmBackCheck()
{
bool Tem_Magnetism = false;
//Frm_ListBos與主窗體
Tem_Magnetism = false;
if ((Example_Play_Top - Example_List_Top) == 0)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_List_Left) == 0)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_List_Left - Example_List_Width) == 0)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_List_Left + Example_List_Width) == 0)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_List_Top - Example_List_Height) == 0)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_List_Top + Example_List_Height) == 0)
Tem_Magnetism = true;
if (Tem_Magnetism)
Example_List_AdhereTo = true;
//Frm_Libretto與主窗體
Tem_Magnetism = false;
if ((Example_Play_Top - Example_Libretto_Top) == 0)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_Libretto_Left) == 0)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_Libretto_Left - Example_Libretto_Width) == 0)
Tem_Magnetism = true;
if ((Example_Play_Left - Example_Libretto_Left + Example_Libretto_Width) == 0)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_Libretto_Top - Example_Libretto_Height) == 0)
Tem_Magnetism = true;
if ((Example_Play_Top - Example_Libretto_Top + Example_Libretto_Height) == 0)
Tem_Magnetism = true;
if (Tem_Magnetism)
Example_Libretto_AdhereTo = true;
//兩個(gè)輔窗體
Tem_Magnetism = false;
if ((Example_List_Top - Example_Libretto_Top) == 0)
Tem_Magnetism = true;
if ((Example_List_Left - Example_Libretto_Left) == 0)
Tem_Magnetism = true;
if ((Example_List_Left - Example_Libretto_Left - Example_Libretto_Width) == 0)
Tem_Magnetism = true;
if ((Example_List_Left - Example_Libretto_Left + Example_Libretto_Width) == 0)
Tem_Magnetism = true;
if ((Example_List_Top - Example_Libretto_Top - Example_Libretto_Height) == 0)
Tem_Magnetism = true;
if ((Example_List_Top - Example_Libretto_Top + Example_Libretto_Height) == 0)
Tem_Magnetism = true;
if (Tem_Magnetism)
Example_Assistant_AdhereTo = true;
}
#endregion
#region 利用窗體上的控件移動(dòng)窗體
/// <summary>
/// 利用控件移動(dòng)窗體
/// </summary>
/// <param Frm="Form">窗體</param>
/// <param e="MouseEventArgs">控件的移動(dòng)事件</param>
public void FrmMove(Form Frm, MouseEventArgs e) //Form或MouseEventArgs添加命名空間using System.Windows.Forms;
{
if (e.Button == MouseButtons.Left)
{
Point myPosittion = Control.MousePosition; //獲取當(dāng)前鼠標(biāo)的屏幕坐標(biāo)
myPosittion.Offset(CPoint.X, CPoint.Y); //重載當(dāng)前鼠標(biāo)的位置
Frm.DesktopLocation = myPosittion; //設(shè)置當(dāng)前窗體在屏幕上的位置
}
}
#endregion
#region 計(jì)算窗體之間的距離差
/// <summary>
/// 計(jì)算窗體之間的距離差
/// </summary>
/// <param Frm="Form">窗體</param>
/// <param Follow="Form">跟隨窗體</param>
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 磁性窗體的移動(dòng)
/// <summary>
/// 磁性窗體的移動(dòng)
/// </summary>
/// <param Frm="Form">窗體</param>
/// <param e="MouseEventArgs">控件的移動(dòng)事件</param>
/// <param Follow="Form">跟隨窗體</param>
public void
ManyFrmMove(Form Frm, MouseEventArgs e, Form Follow) //Form或MouseEventArgs添加命名空間using System.Windows.Forms;
{
if (e.Button == MouseButtons.Left)
{
int Tem_Left = 0;
int Tem_Top = 0;
Point myPosittion = Control.MousePosition; //獲取當(dāng)前鼠標(biāo)的屏幕坐標(biāo)
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 對(duì)窗體的位置進(jìn)行初始化
/// <summary>
/// 對(duì)窗體的位置進(jìn)行初始化
/// </summary>
/// <param Frm="Form">窗體</param>
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 存儲(chǔ)各窗體的當(dāng)前信息
/// <summary>
/// 存儲(chǔ)各窗體的當(dāng)前信息
/// </summary>
/// <param Frm="Form">窗體</param>
/// <param e="MouseEventArgs">控件的移動(dòng)事件</param>
public void FrmPlace(Form Frm)
{
FrmInitialize(Frm);
FrmMagnetism(Frm);
}
#endregion
#region 窗體的磁性設(shè)置
/// <summary>
/// 窗體的磁性設(shè)置
/// </summary>
/// <param Frm="Form">窗體</param>
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 磁性窗體的計(jì)算
/// <summary>
/// 磁性窗體的計(jì)算
/// </summary>
/// <param Frm="Form">窗體</param>
/// <param e="MouseEventArgs">控件的移動(dòng)事件</param>
public void FrmMagnetismCount(Form Frm, int top, int left, int width, int height, string Mforms)
{
bool Tem_Magnetism = false; //判斷是否有磁性發(fā)生
string Tem_MainForm = ""; //臨時(shí)記錄主窗體
string Tem_AssistForm = ""; //臨時(shí)記錄輔窗體
//上面進(jìn)行磁性窗體
if ((Frm.Top + Frm.Height - top) <= Example_FSpace && (Frm.Top + Frm.Height - top) >= -Example_FSpace)
{
//當(dāng)一個(gè)主窗體不包含輔窗體時(shí)
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;
}
//當(dāng)一個(gè)主窗體包含輔窗體時(shí)
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;
}
}
//下面進(jìn)行磁性窗體
if ((Frm.Top - (top + height)) <= Example_FSpace && (Frm.Top - (top + height)) >= -Example_FSpace)
{
//當(dāng)一個(gè)主窗體不包含輔窗體時(shí)
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;
}
//當(dāng)一個(gè)主窗體包含輔窗體時(shí)
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;
}
}
//左面進(jìn)行磁性窗體
if ((Frm.Left + Frm.Width - left) <= Example_FSpace && (Frm.Left + Frm.Width - left) >= -Example_FSpace)
{
//當(dāng)一個(gè)主窗體不包含輔窗體時(shí)
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;
}
//當(dāng)一個(gè)主窗體包含輔窗體時(shí)
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;
}
}
//右面進(jìn)行磁性窗體
if ((Frm.Left - (left + width)) <= Example_FSpace && (Frm.Left - (left + width)) >= -Example_FSpace)
{
//當(dāng)一個(gè)主窗體不包含輔窗體時(shí)
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;
}
//當(dāng)一個(gè)主窗體包含輔窗體時(shí)
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 恢復(fù)窗體的初始大小
/// <summary>
/// 恢復(fù)窗體的初始大小(當(dāng)松開(kāi)鼠標(biāo)時(shí),如果窗體的大小小于300*200,恢復(fù)初始狀態(tài))
/// </summary>
/// <param Frm="Form">窗體</param>
public void FrmScreen_FormerlySize(Form Frm, int PWidth, int PHeight)
{
if (Frm.Width < PWidth || Frm.Height < PHeight)
{
Frm.Width = PWidth;
Frm.Height = PHeight;
//Example_Size = false;
}
}
#endregion
}
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Frm_Play());
}
}
到此這篇關(guān)于基于C#實(shí)現(xiàn)磁性吸附窗體的文章就介紹到這了,更多相關(guān)C#磁性吸附窗體內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#安裝OpenCvSharp4的實(shí)現(xiàn)步驟
OpenCv是一款開(kāi)源的圖像處理庫(kù),本文就介紹了C#安裝OpenCvSharp4的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05
C#實(shí)現(xiàn)簡(jiǎn)易猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
C#基礎(chǔ)之Lambda表達(dá)式用法實(shí)例教程
這篇文章主要介紹了C#中Lambda表達(dá)式用法,并與之前所述的匿名方法做一比較,詳細(xì)的講述了Lambda表達(dá)式的定義及具體用法,需要的朋友可以參考下2014-09-09
C#實(shí)現(xiàn)簡(jiǎn)單串口通信的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)串口通信的相關(guān)知識(shí),文中示例代碼介紹的非常詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴們可以跟隨小編一起了解一下2023-10-10
C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF
線性化PDF文件是PDF文件的一種特殊格式,可以通過(guò)Internet更快地進(jìn)行查看。這篇文章主要介紹了如何通過(guò)C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12
C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法,涉及C#委托的使用技巧,需要的朋友可以參考下2015-04-04

