C#创建磁性窗体的方法:创建特殊窗体

2024-04-17 00:36

本文主要是介绍C#创建磁性窗体的方法:创建特殊窗体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、磁性窗体

二、磁性窗体的实现方法

(1)无标题窗体的移动

(2)Left属性

(3)Top属性

二、设计一个磁性窗体的实例

(1)资源管理器Resources.Designer.cs设计

(2)公共类Frm_Play.cs

(3)主窗体

1.Frm_Play.cs

2.Frm_Play.Designer.cs

(4)子窗体1

1.Frm_ListBox.cs

2.Frm_ListBox.Designer.cs

(5)子窗体2

1.Frm_Libretto.cs

2.Frm_Libretto.Designer.cs

(6)生成效果


一、磁性窗体

        经常会遇到一种情况,即当拖动一个窗体(主窗体)时,其他窗体(子窗体)随着该窗体移动,当拖动子窗体时,其他窗体将不跟随移动,这就是磁性窗体。

二、磁性窗体的实现方法

        在主窗体移动时,通过改变跟随窗体的Left和Top属性值实现“磁性”。

(1)无标题窗体的移动

        无标题窗体的移动主要是通过控件来移动窗体,比如,用Panel控件来进行。首先,在Panel控件的MouseDown事件中将鼠标按下时的位置值(负值)存入到全局变量CPoint中,代码如下:

private void panel_Title_MouseDown(object sender,MouseEventArgs e)
CPoint=new Point(-e.X,-e.Y);    //获取鼠标按下时的位置

        然后,在Panel控件的MouseMove事件中按照CPoint变量的值,以屏幕坐标平移指定的量,并用平移后的结果设置窗体的DesktopLocation属性,代码如下:

private void panel_Title_MouseMove(object sender,MouseEventArgs e)
if(e.Button==MouseButtons.Left)
{Point myPosittion=Control.MousePosition; //获取当前鼠标的屏幕坐标myPosittion.Offset(CPoint.X,CPoint.Y);   //以屏幕坐标平移指定的量DesktopLocation=myPosittion;             //设置当前窗体在屏幕上的位置}

(2)Left属性

        该属性用于获取或设置控件左边缘与其容器的工作区左边缘之间的距离(以像素为单位)。语法格式如下:

public int Left {get;set;}
参数说明
属性值:窗体左边缘与其容器的工作区左边缘之间的距离(以像素为单位)。

(3)Top属性

        该属性用于获取或设置控件上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。语法格式如下:

public int Top{get;set;}
参数说明属性值:窗体上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。

二、设计一个磁性窗体的实例

        本实例将制作一个磁性窗体,当拖动主窗体移动时,两个子窗体如果相连,则跟随移动。

  • 三个窗体:主窗体Frm_Play.cs,2个子窗体:Frm_ListBox.cs、Frm_Libretto.cs;
  • 鼠标按下任一窗体顶部的控件,可以拖动窗体;
  • 拖动子窗体时,会使得粘在一起的窗体分开,拖动主窗体时会使粘在一起的子窗体随动;
  • 拖动主窗体靠近子窗体小于相互吸引的缝隙10时,松开鼠标,靠近的窗体会像磁铁一样吸引在一起;主窗体吸引子窗体后,该子窗体还可以吸引其它子窗体;
  • 双击主窗体的控件,激活所有窗体;

(1)资源管理器Resources.Designer.cs设计

        项目使用的图片资源应设计到资源管理器,详见本文作者写的其它文章:C#手动改变自制窗体的大小-CSDN博客 https://wenchm.blog.csdn.net/article/details/137027140

(2)公共类Frm_Play.cs

// 类设计
namespace _185
{internal class FrmClass{#region  磁性窗体-公共变量//记录窗体的隐藏与显示public static bool Frm_ListShow = false;public static bool Frm_LibrettoShow = false;public static bool Frm_ScreenShow = false;//记录鼠标的当前位置public static Point CPoint;public static Point FrmPoint;public static int Gap = 10;//设置窗体间相互吸引的缝隙尺寸//Frm_Play窗体的位置及大小public static int Frm_Play_Top = 0;public static int Frm_Play_Left = 0;public static int Frm_Play_Width = 0;public static int Frm_Play_Height = 0;public static bool Is_TwoAssitForm_AdhereTo = false;//辅助窗体是否磁性在一起//Frm_ListBos窗体的位置及大小public static int Frm_List_Top = 0;public static int Frm_List_Left = 0;public static int Frm_List_Width = 0;public static int Frm_List_Height = 0;public static bool Is_Frm_List_AdhereTo = false;//辅助窗体是否与主窗体磁性在一起//Frm_Libretto窗体的位置及大小public static int Frm_Libretto_Top = 0;public static int Frm_Libretto_Left = 0;public static int Frm_Libretto_Width = 0;public static int Frm_Libretto_Height = 0;public static bool Is_Frm_Libretto_AdhereTo = false;//辅助窗体是否与主窗体磁性在一起//窗体之间的距离差public static int Frm_List_Gap_Top = 0;public static int Frm_List_Gap_Left = 0;public static int Frm_Libretto_Gap_Top = 0;public static int Frm_Libretto_Gap_Left = 0;#endregion#region  检测各窗体是否连接在一起/// <summary>/// 检测各窗体是否连接在一起/// </summary>public static void Is_Addhered_Check(){//Frm_ListBos与主窗体bool Temp_Magnetism = false;if ((Frm_Play_Top - Frm_List_Top) == 0)Temp_Magnetism = true;if ((Frm_Play_Left - Frm_List_Left) == 0)Temp_Magnetism = true;if ((Frm_Play_Left - Frm_List_Left - Frm_List_Width) == 0)Temp_Magnetism = true;if ((Frm_Play_Left - Frm_List_Left + Frm_List_Width) == 0)Temp_Magnetism = true;if ((Frm_Play_Top - Frm_List_Top - Frm_List_Height) == 0)Temp_Magnetism = true;if ((Frm_Play_Top - Frm_List_Top + Frm_List_Height) == 0)Temp_Magnetism = true;if (Temp_Magnetism)Is_Frm_List_AdhereTo = true;//Frm_Libretto与主窗体Temp_Magnetism = false;if ((Frm_Play_Top - Frm_Libretto_Top) == 0)Temp_Magnetism = true;if ((Frm_Play_Left - Frm_Libretto_Left) == 0)Temp_Magnetism = true;if ((Frm_Play_Left - Frm_Libretto_Left - Frm_Libretto_Width) == 0)Temp_Magnetism = true;if ((Frm_Play_Left - Frm_Libretto_Left + Frm_Libretto_Width) == 0)Temp_Magnetism = true;if ((Frm_Play_Top - Frm_Libretto_Top - Frm_Libretto_Height) == 0)Temp_Magnetism = true;if ((Frm_Play_Top - Frm_Libretto_Top + Frm_Libretto_Height) == 0)Temp_Magnetism = true;if (Temp_Magnetism)Is_Frm_Libretto_AdhereTo = true;//两个辅窗体Temp_Magnetism = false;if ((Frm_List_Top - Frm_Libretto_Top) == 0)Temp_Magnetism = true;if ((Frm_List_Left - Frm_Libretto_Left) == 0)Temp_Magnetism = true;if ((Frm_List_Left - Frm_Libretto_Left - Frm_Libretto_Width) == 0)Temp_Magnetism = true;if ((Frm_List_Left - Frm_Libretto_Left + Frm_Libretto_Width) == 0)Temp_Magnetism = true;if ((Frm_List_Top - Frm_Libretto_Top - Frm_Libretto_Height) == 0)Temp_Magnetism = true;if ((Frm_List_Top - Frm_Libretto_Top + Frm_Libretto_Height) == 0)Temp_Magnetism = true;if (Temp_Magnetism)Is_TwoAssitForm_AdhereTo = true;}#endregion#region  利用窗体上的控件移动窗体/// <summary>/// 利用控件移动窗体/// </summary>/// <param Frm="Form">窗体</param>/// <param e="MouseEventArgs">控件的移动事件</param>public static void MoveForm(Form Frm, MouseEventArgs e) {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 static void Calculate_Gap(Form Frm, Form Follow){switch (Follow.Name){case "Frm_ListBox":{Frm_List_Gap_Top = Follow.Top - Frm.Top;Frm_List_Gap_Left = Follow.Left - Frm.Left;break;}case "Frm_Libretto":{Frm_Libretto_Gap_Top = Follow.Top - Frm.Top;Frm_Libretto_Gap_Left = Follow.Left - Frm.Left;break;}}}#endregion#region  磁性窗体的移动/// <summary>/// 磁性窗体的移动/// </summary>/// <param Frm="Form">窗体</param>/// <param e="MouseEventArgs">控件的移动事件</param>/// <param Follow="Form">跟随窗体</param>public static void MoveManyForm(Form Frm, MouseEventArgs e, Form Follow){ArgumentNullException.ThrowIfNull(Frm);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 = Frm_List_Gap_Top - FrmPoint.Y;Tem_Left = Frm_List_Gap_Left - FrmPoint.X;break;}case "Frm_Libretto":{Tem_Top = Frm_Libretto_Gap_Top - FrmPoint.Y;Tem_Left = Frm_Libretto_Gap_Left - FrmPoint.X;break;}}myPosittion.Offset(Tem_Left, Tem_Top);Follow.DesktopLocation = myPosittion;}}#endregion#region  对窗体的位置进行初始化/// <summary>/// 对窗体的位置进行初始化/// </summary>/// <param Frm="Form">窗体</param>public static void FrmInitialize(Form Frm){switch (Frm.Name){case "Frm_Play":{Frm_Play_Top = Frm.Top;Frm_Play_Left = Frm.Left;Frm_Play_Width = Frm.Width;Frm_Play_Height = Frm.Height;break;}case "Frm_ListBox":{Frm_List_Top = Frm.Top;Frm_List_Left = Frm.Left;Frm_List_Width = Frm.Width;Frm_List_Height = Frm.Height;break;}case "Frm_Libretto":{Frm_Libretto_Top = Frm.Top;Frm_Libretto_Left = Frm.Left;Frm_Libretto_Width = Frm.Width;Frm_Libretto_Height = Frm.Height;break;}}}#endregion#region  存储各窗体的当前信息/// <summary>/// 存储各窗体的当前信息/// </summary>/// <param Frm="Form">窗体</param>/// <param e="MouseEventArgs">控件的移动事件</param>public static void FrmPlace(Form Frm){FrmInitialize(Frm);FrmMagnetism(Frm);}#endregion#region  窗体的磁性设置/// <summary>/// 窗体的磁性设置/// </summary>/// <param Frm="Form">窗体</param>public static void FrmMagnetism(Form Frm){if (Frm.Name != "Frm_Play"){FrmMagnetismCount(Frm, Frm_Play_Top, Frm_Play_Left, Frm_Play_Width, Frm_Play_Height, "Frm_Play");}if (Frm.Name != "Frm_ListBos"){FrmMagnetismCount(Frm, Frm_List_Top, Frm_List_Left, Frm_List_Width, Frm_List_Height, "Frm_ListBos");}if (Frm.Name != "Frm_Libratto"){FrmMagnetismCount(Frm, Frm_Libretto_Top, Frm_Libretto_Left, Frm_Libretto_Width, Frm_Libretto_Height, "Frm_Libratto");}FrmInitialize(Frm);}#endregion#region  磁性窗体的计算/// <summary>/// 磁性窗体的计算/// </summary>/// <param Frm="Form">窗体</param>/// <param e="MouseEventArgs">控件的移动事件</param>public static 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) <= Gap && (Frm.Top + Frm.Height - top) >= -Gap){//当一个主窗体不包含辅窗体时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) <= Gap && (Frm.Left - left) >= -Gap)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) <= Gap && (Frm.Left - left) >= -Gap)Frm.Left = left;Tem_Magnetism = true;}}//下面进行磁性窗体if ((Frm.Top - (top + height)) <= Gap && (Frm.Top - (top + height)) >= -Gap){//当一个主窗体不包含辅窗体时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) <= Gap && (Frm.Left - left) >= -Gap)Frm.Left = left;Tem_Magnetism = true;}//当一个主窗体包含辅窗体时if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width)){Frm.Top = top + height;if ((Frm.Left - left) <= Gap && (Frm.Left - left) >= -Gap)Frm.Left = left;Tem_Magnetism = true;}}//左面进行磁性窗体if ((Frm.Left + Frm.Width - left) <= Gap && (Frm.Left + Frm.Width - left) >= -Gap){//当一个主窗体不包含辅窗体时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) <= Gap && (Frm.Top - top) >= -Gap)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) <= Gap && (Frm.Top - top) >= -Gap)Frm.Top = top;Tem_Magnetism = true;}}//右面进行磁性窗体if ((Frm.Left - (left + width)) <= Gap && (Frm.Left - (left + width)) >= -Gap){//当一个主窗体不包含辅窗体时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) <= Gap && (Frm.Top - top) >= -Gap)Frm.Top = top;Tem_Magnetism = true;}//当一个主窗体包含辅窗体时if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height)){Frm.Left = left + width;if ((Frm.Top - top) <= Gap && (Frm.Top - top) >= -Gap)Frm.Top = top;Tem_Magnetism = true;}}if (Frm.Name == "Frm_Play")Tem_MainForm = "Frm_Play";elseTem_AssistForm = Frm.Name;if (Mforms == "Frm_Play")Tem_MainForm = "Frm_Play";elseTem_AssistForm = Mforms;if (Tem_MainForm == ""){Is_TwoAssitForm_AdhereTo = Tem_Magnetism;}else{switch (Tem_AssistForm){case "Frm_ListBos":Is_Frm_List_AdhereTo = Tem_Magnetism;break;case "Frm_Libratto":Is_Frm_Libretto_AdhereTo = Tem_Magnetism;break;}}}#endregion#region  恢复窗体的初始大小/// <summary>/// 恢复窗体的初始大小(当松开鼠标时,如果窗体的大小小于300*200,恢复初始状态)/// </summary>/// <param Frm="Form">窗体</param>public static 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}
}

(3)主窗体

1.Frm_Play.cs

namespace _185
{public partial class Frm_Play : Form{public Frm_Play(){InitializeComponent();}#region  公共变量FrmClass Cla_FrmClass = new();public static Form F_List = new();public static Form F_Libretto = new();public static Form F_Screen = new();#endregionprivate void Frm_Play_Load(object sender, EventArgs e){FrmClass.FrmInitialize(this);                //窗体位置的初始化}private void Panel1_MouseDown(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left)            //按下的是否为鼠标左键{FrmClass.Is_Addhered_Check();             //检测各窗体是否连在一起int Tem_Y = e.Y;FrmClass.FrmPoint = new Point(e.X, Tem_Y);//获取鼠标在窗体上的位置,用于磁性窗体FrmClass.CPoint = new Point(-e.X, -Tem_Y);//获取鼠标在屏幕上的位置,用于窗体的移动if (FrmClass.Is_Frm_List_AdhereTo)              //如果与frm_ListBox窗体相连接{FrmClass.Calculate_Gap(this, F_List);        //计算窗体的距离差if (FrmClass.Is_TwoAssitForm_AdhereTo)       //两个辅窗体是否连接在一起{FrmClass.Calculate_Gap(this, F_Libretto);//计算窗体的距离差}}if (FrmClass.Is_Frm_Libretto_AdhereTo)        //如果与frm_Libretto窗体相连接{FrmClass.Calculate_Gap(this, F_Libretto); //计算窗体的距离差if (FrmClass.Is_TwoAssitForm_AdhereTo)    //两个辅窗体是否连接在一起{FrmClass.Calculate_Gap(this, F_List); //计算窗体的距离差}}}}private void Panel1_MouseMove(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left)            //按下的是否为鼠标左键{FrmClass.MoveForm(this, e);               //利用控件移动窗体if (FrmClass.Is_Frm_List_AdhereTo)        //如果frm_ListBox窗体与主窗体连接{FrmClass.MoveManyForm(this, e, F_List);//磁性窗体的移动FrmClass.FrmInitialize(F_List);        //对frm_ListBox窗体的位置进行初始化if (FrmClass.Is_TwoAssitForm_AdhereTo) //如果两个子窗体连接在一起{FrmClass.MoveManyForm(this, e, F_Libretto);FrmClass.FrmInitialize(F_Libretto);}}if (FrmClass.Is_Frm_Libretto_AdhereTo)    //如果frm_Libretto窗体与主窗体连接{FrmClass.MoveManyForm(this, e, F_Libretto);FrmClass.FrmInitialize(F_Libretto);if (FrmClass.Is_TwoAssitForm_AdhereTo){FrmClass.MoveManyForm(this, e, F_List);FrmClass.FrmInitialize(F_List);}}FrmClass.FrmInitialize(this);}}private void Panel1_MouseUp(object sender, MouseEventArgs e){FrmClass.FrmPlace(this);}private void Frm_Play_Shown(object sender, EventArgs e){//显示列表窗体F_List = new Frm_ListBox{ShowInTaskbar = false};FrmClass.Frm_ListShow = true;F_List.Show();//显示歌词窗体F_Libretto = new Frm_Libretto{ShowInTaskbar = false,Left = Left + Width,Top = Top};FrmClass.Frm_LibrettoShow = true;F_Libretto.Show();//各窗体位置的初始化FrmClass.FrmInitialize(F_List);FrmClass.FrmInitialize(F_Libretto);}private void Panel2_Click(object sender, EventArgs e){F_List.Close();F_List.Dispose();F_Libretto.Close();F_Libretto.Dispose();F_Screen.Close();F_Screen.Dispose();Close();}private void Panel1_Click(object sender, EventArgs e){F_List.Focus();F_Libretto.Focus();Focus();}}
}

2.Frm_Play.Designer.cs

namespace _185
{partial class Frm_Play{/// <summary>///  Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>///  Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>///  Required method for Designer support - do not modify///  the contents of this method with the code editor./// </summary>private void InitializeComponent(){panel1 = new Panel();pictureBox1 = new PictureBox();panel2 = new Panel();((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();SuspendLayout();// // panel1// panel1.BackgroundImage = Properties.Resources._5;panel1.BackgroundImageLayout = ImageLayout.Stretch;panel1.Dock = DockStyle.Top;panel1.Location = new Point(0, 0);panel1.Name = "panel1";panel1.Size = new Size(290, 31);panel1.TabIndex = 0;panel1.Click += Panel1_Click;panel1.MouseDown += Panel1_MouseDown;panel1.MouseMove += Panel1_MouseMove;panel1.MouseUp += Panel1_MouseUp;// // pictureBox1// pictureBox1.BackgroundImage = Properties.Resources._01;pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;pictureBox1.Dock = DockStyle.Fill;pictureBox1.Location = new Point(0, 31);pictureBox1.Name = "pictureBox1";pictureBox1.Size = new Size(290, 89);pictureBox1.TabIndex = 1;pictureBox1.TabStop = false;// // panel2// panel2.BackgroundImage = Properties.Resources.Close;panel2.BackgroundImageLayout = ImageLayout.Stretch;panel2.Location = new Point(265, 5);panel2.Name = "panel2";panel2.Size = new Size(18, 18);panel2.TabIndex = 0;panel2.Click += Panel2_Click;// // Frm_Play// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(290, 120);Controls.Add(panel2);Controls.Add(pictureBox1);Controls.Add(panel1);FormBorderStyle = FormBorderStyle.None;Name = "Frm_Play";Text = "Form1";Load += Frm_Play_Load;Shown += Frm_Play_Shown;((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();ResumeLayout(false);}#endregionprivate Panel panel1;private PictureBox pictureBox1;private Panel panel2;}
}

(4)子窗体1

1.Frm_ListBox.cs

namespace _185
{public partial class Frm_ListBox : Form{public Frm_ListBox(){InitializeComponent();}#region  公共变量FrmClass Cla_FrmClass = new();#endregionprivate void Frm_ListBox_Load(object sender, EventArgs e){Left = FrmClass.Frm_Play_Left;Top = FrmClass.Frm_Play_Top + FrmClass.Frm_Play_Height;FrmClass.FrmInitialize(this);}private void Panel1_MouseDown(object sender, MouseEventArgs e){FrmClass.CPoint = new Point(-e.X, -e.Y);}private void Panel1_MouseMove(object sender, MouseEventArgs e){FrmClass.Is_TwoAssitForm_AdhereTo = false;FrmClass.Is_Frm_List_AdhereTo = false;FrmClass.MoveForm(this, e);}private void Panel1_MouseUp(object sender, MouseEventArgs e){FrmClass.FrmPlace(this);}}
}

2.Frm_ListBox.Designer.cs


namespace _185
{partial class Frm_ListBox{/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){panel1 = new Panel();pictureBox1 = new PictureBox();((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();SuspendLayout();// // panel1// panel1.BackgroundImage = Properties.Resources._5;panel1.Dock = DockStyle.Top;panel1.Location = new Point(0, 0);panel1.Name = "panel1";panel1.Size = new Size(290, 31);panel1.TabIndex = 0;panel1.MouseDown += Panel1_MouseDown;panel1.MouseMove += Panel1_MouseMove;panel1.MouseUp += Panel1_MouseUp;// // pictureBox1// pictureBox1.BackgroundImage = Properties.Resources._02;pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;pictureBox1.Dock = DockStyle.Fill;pictureBox1.Location = new Point(0, 31);pictureBox1.Name = "pictureBox1";pictureBox1.Size = new Size(290, 89);pictureBox1.TabIndex = 1;pictureBox1.TabStop = false;// // Frm_ListBox// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(290, 120);Controls.Add(pictureBox1);Controls.Add(panel1);FormBorderStyle = FormBorderStyle.None;Name = "Frm_ListBox";Text = "Form1";Load += Frm_ListBox_Load;((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();ResumeLayout(false);}#endregionprivate Panel panel1;private PictureBox pictureBox1;}
}

(5)子窗体2

1.Frm_Libretto.cs

namespace _185
{public partial class Frm_Libretto : Form{public Frm_Libretto(){InitializeComponent();}#region  公共变量FrmClass Cla_FrmClass = new();#endregionprivate void Frm_Libretto_Load(object sender, EventArgs e){Left = FrmClass.Frm_Play_Left;Top = FrmClass.Frm_Play_Top + FrmClass.Frm_Play_Height;FrmClass.FrmInitialize(this);}private void Panel1_MouseDown(object sender, MouseEventArgs e){FrmClass.CPoint = new Point(-e.X, -e.Y);}private void Panel1_MouseMove(object sender, MouseEventArgs e){FrmClass.Is_TwoAssitForm_AdhereTo = false;FrmClass.Is_Frm_List_AdhereTo = false;FrmClass.MoveForm(this, e);}private void Panel1_MouseUp(object sender, MouseEventArgs e){FrmClass.FrmPlace(this);}}
}

2.Frm_Libretto.Designer.cs

namespace _185
{partial class Frm_Libretto{/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// Clean up any resources being used./// </summary>/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){panel1 = new Panel();pictureBox1 = new PictureBox();((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();SuspendLayout();// // panel1// panel1.BackgroundImage = Properties.Resources._5;panel1.BackgroundImageLayout = ImageLayout.Stretch;panel1.Dock = DockStyle.Top;panel1.Location = new Point(0, 0);panel1.Name = "panel1";panel1.Size = new Size(290, 31);panel1.TabIndex = 0;panel1.MouseDown += Panel1_MouseDown;panel1.MouseMove += Panel1_MouseMove;panel1.MouseUp += Panel1_MouseUp;// // pictureBox1// pictureBox1.BackgroundImage = Properties.Resources._03;pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;pictureBox1.Dock = DockStyle.Fill;pictureBox1.Location = new Point(0, 31);pictureBox1.Name = "pictureBox1";pictureBox1.Size = new Size(290, 209);pictureBox1.TabIndex = 1;pictureBox1.TabStop = false;// // Frm_Libretto// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(290, 240);Controls.Add(pictureBox1);Controls.Add(panel1);FormBorderStyle = FormBorderStyle.None;Name = "Frm_Libretto";Text = "Form2";Load += Frm_Libretto_Load;((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();ResumeLayout(false);}#endregionprivate Panel panel1;private PictureBox pictureBox1;}
}

(6)生成效果

         三个窗体吸引在一起

         三个窗体被拖动分开

 

        主窗体吸引子窗体再吸引子窗体 

这篇关于C#创建磁性窗体的方法:创建特殊窗体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/910337

相关文章

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

Java中Object类的常用方法小结

《Java中Object类的常用方法小结》JavaObject类是所有类的父类,位于java.lang包中,本文为大家整理了一些Object类的常用方法,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. public boolean equals(Object obj)2. public int ha

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

golang1.23版本之前 Timer Reset方法无法正确使用

《golang1.23版本之前TimerReset方法无法正确使用》在Go1.23之前,使用`time.Reset`函数时需要先调用`Stop`并明确从timer的channel中抽取出东西,以避... 目录golang1.23 之前 Reset ​到底有什么问题golang1.23 之前到底应该如何正确的

C#实现系统信息监控与获取功能

《C#实现系统信息监控与获取功能》在C#开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取CPU、GPU资源信息,本文将详细介绍如何使用C#来实现... 目录前言一、C# 监控键盘1. 原理与实现思路2. 代码实现二、读取 CPU、GPU 资源信息1.