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中,代码如下:

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

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

cs 复制代码
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属性

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

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

(3)Top属性

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

cs 复制代码
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

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";
            else
                Tem_AssistForm = Frm.Name;
            if (Mforms == "Frm_Play")
                Tem_MainForm = "Frm_Play";
            else
                Tem_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

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();
        #endregion

        private 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

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);
        }

        #endregion

        private Panel panel1;
        private PictureBox pictureBox1;
        private Panel panel2;
    }
}

(4)子窗体1

1.Frm_ListBox.cs

cs 复制代码
namespace _185
{
    public partial class Frm_ListBox : Form
    {
        public Frm_ListBox()
        {
            InitializeComponent();
        }

        #region  公共变量
        FrmClass Cla_FrmClass = new();
        #endregion

        private 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

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);
        }

        #endregion

        private Panel panel1;
        private PictureBox pictureBox1;
    }
}

(5)子窗体2

1.Frm_Libretto.cs

cs 复制代码
namespace _185
{
    public partial class Frm_Libretto : Form
    {
        public Frm_Libretto()
        {
            InitializeComponent();
        }

        #region  公共变量
        FrmClass Cla_FrmClass = new();
        #endregion

        private 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

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);
        }

        #endregion

        private Panel panel1;
        private PictureBox pictureBox1;
    }
}

(6)生成效果

三个窗体吸引在一起

三个窗体被拖动分开

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

相关推荐
赖赖赖先生3 分钟前
fastadmin 框架 生成qr code 二维码图片,PHP 7.4版本
开发语言·php
玉红7771 小时前
R语言的数据类型
开发语言·后端·golang
夜斗(dou)1 小时前
node.js文件压缩包解析,反馈解析进度,解析后的文件字节正常
开发语言·javascript·node.js
觅远1 小时前
python+PyMuPDF库:(一)创建pdf文件及内容读取和写入
开发语言·python·pdf
神雕杨2 小时前
node js 过滤空白行
开发语言·前端·javascript
lvbu_2024war012 小时前
MATLAB语言的网络编程
开发语言·后端·golang
single5942 小时前
【c++笔试强训】(第四十五篇)
java·开发语言·数据结构·c++·算法
游客5202 小时前
自动化办公-合并多个excel
开发语言·python·自动化·excel
Cshaosun2 小时前
js版本之ES6特性简述【Proxy、Reflect、Iterator、Generator】(五)
开发语言·javascript·es6
凡人的AI工具箱3 小时前
每天40分玩转Django:Django部署概述
开发语言·数据库·后端·python·django