基于C#实现磁性吸附窗体
作者:芝麻粒儿 发布时间:2022-09-04 07:04:51
标签:C#,磁性,吸附,窗体
实践过程
效果
代码
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());
}
}
来源:https://zhima.blog.csdn.net/article/details/128020694
0
投稿
猜你喜欢
- Get请求1.简单发送Get请求/// <summary>/// 指定Url地址使用Get 方式获取全部字符串/// </
- starter起步依赖starter起步依赖是springboot一种非常重要的机制,它打包了某些场景下需要用到依赖,将其统一集成到star
- 概述从今天开始, 小白我将带大家开启 Jave 数据结构 & 算法的新篇章.队列队列 (Queue) 遵循先进先出的原则 (Firs
- 一、打印直角三角形这个循环控制打印十行空格for (int x = 1; x <= 10; x++) {//因为要打印一个十行的直角三
- springboot整合tkmapper1.导入pom依赖1.1 导入springboot的parent依赖<parent> &
- 问题?在很多公司(如阿里、华为等)的编程规范中,非常明确地禁止使用Executors快捷创建线程池,为什么呢?这里从源码讲起,介绍使用Exe
- 过滤器实现过滤器需要实现 javax.servlet.Filter 接口。重写三个方法。其中 init()&n
- 一、概述RocketMQ主要提供了两种消费模式:集群消费以及广播消费。我们只需要在定义消费者的时候通过setMessageModel(Mes
- java实体类转成map1.第一种 <!-- 配置gson -->  
- java中的interface接口实例详解接口:Java接口是一些方法表征的集合,但是却不会在接口里实现具体的方法。java接口
- 本文实例为大家分享了Unity实现俄罗斯方块第3部分,供大家参考,具体内容如下解决穿透问题逻辑部分1、在物体进行移动的过程中更新格子的信息,
- 1. 查找系统中坏味道的异常处理代码在上篇文章杂谈异常处理try-catch-finally中主要详细介绍了C#异常处理的概念,
- 本文主要介绍了Maven实战之搭建Maven * 和镜像的方法(图文),分享给大家,具体如下:准备工作安装3.6.*的Nexus理论Nexus
- package com.smart.frame.task.autoTask;import java.util.Collection;impo
- 本文实例讲述了Java实现求解一元n次多项式的方法。分享给大家供大家参考,具体如下:项目需要做趋势预测,采用线性拟合、2阶曲线拟合和指数拟合
- 策略模式的应用场景策略模式是否要使用,取决于业务场景是否符合,有没有必要。是否符合如果业务是处于不同的场景时,采取不同的处理方式的话,就满足
- 常用配置ObjectMapper objectMapper = new ObjectMapper();objectMapper.config
- 在一个完整的项目中,如果每一个控制器的方法都返回不同的结果,那么对项目的维护和扩展都会很麻烦;并且现在主流的开发模式时前后端分离的模式,如果
- 1.创建项目修改依赖版本2.创建配置文件package com.huanmingjie.elasticsearch.config;impor
- 关于Android实现文字上下滚动这个功能,我目前有两种方法实现: 一个是在TextView 中加上翻转的动画效果,然后设置循环滚动;一种是