实例介绍
【实例简介】
磁性窗体设计实例,类似千千静音效果,详见下图
【实例截图】

【核心代码】
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
}
标签: QQ
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论