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)生成效果

         三个窗体吸引在一起

         三个窗体被拖动分开

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/624378.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JavaWeb前端/后端开发规范——接口文档概述及YApi平台的使用

前言&#xff1a; 整理下笔记&#xff0c;打好基础&#xff0c;daydayup!!! 接口文档 什么是接口文档&#xff1f; 目前主流的开发模式为前后端分离式开发&#xff0c;为了方便前后端的对接&#xff0c;就需要使用接口文件进行统一规范。 接口文档记载什么信息&#xff1f; 1&…

李飞飞团队发布《2024年人工智能指数报告》,预测人工智能未来发展趋势

昨天&#xff0c;斯坦福大学 Human-Center Artificial Intelligence (HAI)研究中心发布了《2024年人工智能指数报告》。 由斯坦福大学发起的人工智能指数&#xff08;AI Index&#xff09;是一个追踪 AI 动态和进展的非营利性项目&#xff0c;旨在全面研究 AI 行业状况&#xf…

物联网的核心价值是什么?——青创智通

工业物联网解决方案-工业IOT-青创智通 物联网&#xff0c;这个词汇在当今的科技领域已经变得耳熟能详。但当我们深入探索物联网的核心价值时&#xff0c;我们会发现它远不止是一个简单的技术概念&#xff0c;而是一种能够彻底改变我们生活方式和工作方式的革命性力量。 物联网…

libcurl 简单使用

LibCurl是一个开源的免费的多协议数据传输开源库&#xff0c;该框架具备跨平台性&#xff0c;开源免费&#xff0c;并提供了包括HTTP、FTP、SMTP、POP3等协议的功能&#xff0c;使用libcurl可以方便地进行网络数据传输操作&#xff0c;如发送HTTP请求、下载文件、发送电子邮件等…

C语言基础入门案例(3)

目录 第一题&#xff1a;一维数组的最大值和最小值求解 第二题&#xff1a;求一维数组中的第二大的数 第三题&#xff1a;计算5个整数的平均值 第四题&#xff1a;查找整数在数组中的索引位置 第五题&#xff1a;统计字符串中数字字符的个数 第一题&#xff1a;一维数组的…

服务器Linux搭建NPM私有仓库

服务器Linux搭建NPM私有仓库 环境搭建 安装 nodejs nodejs官网&#xff1a;https://nodejs.org/en/download/package-manager 可以去官网自行下载nodejs的Linux版本&#xff0c;但是出于别的原因考虑&#xff0c;可以使用nvm去下载nodejs这样会切换nodejs也方便。 nvm 这…

MySQL进阶-----limit、count、update优化

目录 前言 一、limit优化 1. 未优化案例 2.优化后案例 二、count优化 count用法 三、update优化 1.锁行情况&#xff08;有索引&#xff09; 2.锁表情况&#xff08;无索引&#xff09; 前言 上一期我们学习了order by优化和group by优化&#xff0c;本期我们就继续学习…

程序员接单的渠道有没有可靠介绍?

程序员接单的渠道有很多&#xff0c;但总结下来无非就是个人介绍和程序员接单平台。 这里就不多说废话了&#xff0c;直接上当前市面上靠谱且稳定的程序员接单平台list。 程序员客栈 近100w程序员都在使用的程序员接单平台。作为一个靠谱的线上接单渠道&#xff0c;程序员客栈…

SSRF靶场

SSRF概述 ​ 强制服务器发送一个攻击者的请求 ​ 互联网上的很多web应用提供了从其他服务器&#xff08;也可以是本地)获取数据的功能。使用用户指定的URL&#xff0c;web应用可以获取图片&#xff08;载入图片&#xff09;、文件资源&#xff08;下载或读取)。如下图所示&…

笔试题1 -- 吃掉字符串中相邻的相同字符(点击消除_牛客网)

吃掉字符串中相邻的相同字符 文章目录 吃掉字符串中相邻的相同字符题目重现解法一&#xff1a;(基于 erase() 函数实现)解法二&#xff1a;&#xff08;利用 栈 辅助实现&#xff09;总结 题目链接&#xff1a; 点击消除_牛客网 题目重现 牛牛拿到了一个字符串。 他每次“点击…

关联规则挖掘(一)

目录 一、关联规则的概念&#xff08;一&#xff09;基本概念&#xff08;二&#xff09;项集的性质 二、关联规则的Apriori算法&#xff08;一&#xff09;发现频繁项集&#xff08;二&#xff09;产生关联规则 一、关联规则的概念 &#xff08;一&#xff09;基本概念 事务数…

byobu

byobu 终端多路复用器 一、byobu 安装二、byobu 使用三、其他终端多路复用器四、ssh byobu 远程协作 系统环境: linux(ubuntu,debian,kali) 一、byobu 安装 byobu 是包装过的tmux #sudo apt install tmux sudo apt install byobubyobu二、byobu 使用 创建窗口: Ctrl a c…