基于C#实现磁性吸附窗体
更新时间:2023年1月3日 12:54 点击:534 作者:芝麻粒儿
实践过程
效果
代码
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) //按下的是否为鼠标左键 { 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) //如果与frm_ListBox窗体相连接 { Cla_FrmClass.FrmDistanceJob(this, F_List); //计算窗体的距离差 if (FrmClass.Example_Assistant_AdhereTo) //两个辅窗体是否连接在一起 { Cla_FrmClass.FrmDistanceJob(this, F_Libretto); //计算窗体的距离差 } } if (FrmClass.Example_Libretto_AdhereTo) //如果与frm_Libretto窗体相连接 { 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) //如果frm_ListBox窗体与主窗体连接 { Cla_FrmClass.ManyFrmMove(this, e, F_List); //磁性窗体的移动 Cla_FrmClass.FrmInitialize(F_List); //对frm_ListBox窗体的位置进行初始化 if (FrmClass.Example_Assistant_AdhereTo) //如果两个子窗体连接在一起 { 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; //记录鼠标的当前位置 public static Point CPoint; //添加命名空间using System.Drawing; public static Point FrmPoint; public static int Example_FSpace = 10; //设置窗体间的距离 //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 检测各窗体是否连接在一起 /// <summary> /// 检测各窗体是否连接在一起 /// </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; //两个辅窗体 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 利用窗体上的控件移动窗体 /// <summary> /// 利用控件移动窗体 /// </summary> /// <param Frm="Form">窗体</param> /// <param e="MouseEventArgs">控件的移动事件</param> public void FrmMove(Form Frm, MouseEventArgs e) //Form或MouseEventArgs添加命名空间using System.Windows.Forms; { if (e.Button == MouseButtons.Left) { Point myPosittion = Control.MousePosition; //获取当前鼠标的屏幕坐标 myPosittion.Offset(CPoint.X, CPoint.Y); //重载当前鼠标的位置 Frm.DesktopLocation = myPosittion; //设置当前窗体在屏幕上的位置 } } #endregion #region 计算窗体之间的距离差 /// <summary> /// 计算窗体之间的距离差 /// </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 磁性窗体的移动 /// <summary> /// 磁性窗体的移动 /// </summary> /// <param Frm="Form">窗体</param> /// <param e="MouseEventArgs">控件的移动事件</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; //获取当前鼠标的屏幕坐标 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 对窗体的位置进行初始化 /// <summary> /// 对窗体的位置进行初始化 /// </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 存储各窗体的当前信息 /// <summary> /// 存储各窗体的当前信息 /// </summary> /// <param Frm="Form">窗体</param> /// <param e="MouseEventArgs">控件的移动事件</param> public void FrmPlace(Form Frm) { FrmInitialize(Frm); FrmMagnetism(Frm); } #endregion #region 窗体的磁性设置 /// <summary> /// 窗体的磁性设置 /// </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 磁性窗体的计算 /// <summary> /// 磁性窗体的计算 /// </summary> /// <param Frm="Form">窗体</param> /// <param e="MouseEventArgs">控件的移动事件</param> 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 恢复窗体的初始大小 /// <summary> /// 恢复窗体的初始大小(当松开鼠标时,如果窗体的大小小于300*200,恢复初始状态) /// </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> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Frm_Play()); } }
到此这篇关于基于C#实现磁性吸附窗体的文章就介绍到这了,更多相关C#磁性吸附窗体内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://zhima.blog.csdn.net/article/details/128020694
上一篇: C#实现自定义屏保的示例代码
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25