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";
            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

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

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

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


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

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

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

         三个窗体吸引在一起

         三个窗体被拖动分开

 

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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Java Spring 框架下利用 MyBatis 实现请求 MySQL 数据库的存储过程

Java Spring 框架下利用 MyBatis 实现请求 MySQL 数据库的存储过程 环境准备与前置知识1. 创建 MySQL 存储过程2. 配置数据源3. 创建实体类4. 创建 Mapper 接口5. 创建 Mapper XML 文件6. 创建 Service 接口及Impl实现类7. 创建 Controller 类8. 测试与总结 在现代的 Web 应用开…

STM32 F103 C8T6开发笔记14:与HLK-LD303-24G测距雷达通信

今日尝试配通STM32 F103 ZET6与HLK-LD303-24G测距雷达的串口通信解码 文章提供测试代码...... 目录 HLK-LD303-24G测距雷达外观&#xff1a; 线路连接准备&#xff1a; 定时器与串口配置准备&#xff1a; 定时器2的初始化&#xff1a; 串口1、2初始化&#xff1a; 串口1、2自定…

ARP代理

10.1.0.1/8 和10.2.0.1/8是在同一个网段 10.1.0.2/16 和10.2.0.2/16 不在同一个网段 10.1.0.1/8 和10.1.0.2/16 是可以ping通的 包发出来了&#xff0c;报文有发出来&#xff0c;目的地址是广播包 广播请求&#xff0c;发到路由器的接口G 0/0/0 target不是本接口&#xff0…

【C++学习】C++IO流

这里写目录标题 &#x1f680;C语言的输入与输出&#x1f680;什么是流&#x1f680;CIO流&#x1f680;C标准IO流&#x1f680;C文件IO流 &#x1f680;C语言的输入与输出 C语言中我们用到的最频繁的输入输出方式就是scanf ()与printf()。 scanf(): 从标准输入设备(键盘)读取…

windows网络驱动开发

基石&#xff1a;WFP 1、简介 Windows过滤平台&#xff08;Windows Filtering Platform, WFP&#xff09;&#xff0c;是从Vista系统后新增的一套系统API和服务。开发者可以在WFP框架已划分的不同分层中进行过滤、重定向、修改网络数据包&#xff0c;以实现防火墙、入侵检测系…

pdf做批注编辑工具 最新pdf reader pro3.3.1.0激活版

PDF Reader Pro是一款功能强大的PDF阅读和编辑工具。它提供了多种工具和功能&#xff0c;帮助用户对PDF文档进行浏览、注释、编辑、转换和签名等操作。以下是PDF Reader Pro的一些主要特色&#xff1a; 最新pdf reader pro3.3.1.0激活版下载 多种查看模式&#xff1a;PDF Reade…

上海计算机学会 2023年10月月赛 乙组T4 树的覆盖(树、最小点覆盖、树形dp)

第四题&#xff1a;T4树的覆盖 标签&#xff1a;树、最小点覆盖、树形 d p dp dp题意&#xff1a;求树的最小点覆盖集的大小和对应的数量&#xff0c;数量对 1 , 000 , 000 , 007 1,000,000,007 1,000,000,007取余数。所谓覆盖集&#xff0c;是该树的点构成的集合&#xff0c;…

vue:如何通过两个点的经纬度进行距离的计算(很简单)

首先假设从api获取到了自己的纬经度和别人的纬经度 首先有一个概念需要说一下 地球半径 由于地球不是一个完美的球体&#xff0c;所以并不能用一个特别准确的值来表示地球的实际半径&#xff0c;不过由于地球的形状很接近球体&#xff0c;用[6357km] 到 [6378km]的范围值可以…

板式热交换器强度

1、不同标准中对于板换压板的规定 (1) NB/T 47004.1-2017《板式热交换器 第1部分&#xff1a;可拆卸板式热交换器》6.3压紧板6.3.3条“压紧板应有足够的刚性&#xff0c;以保证板式热交换器在正常操作状态不发生泄漏”。 (2) NB/T 47004-2009《板式热交换器》5.3紧板5.3.3条“…

Springboot+Vue项目-基于Java+MySQL的蜗牛兼职网系统(附源码+演示视频+LW)

大家好&#xff01;我是程序猿老A&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f49e;当前专栏&#xff1a;Java毕业设计 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f380; Python毕业设计 &…

每日一题 — 串联所有单词的子串

30. 串联所有单词的子串 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a;因为words里面的每一个字符串的长度都是固定的&#xff0c;所以可以将题转换成字符在字符串中的所有异位词 设出哈希表定义left和right进窗口维护count判断出窗口维护count 代码&#xff1a; …

[html]一个动态js倒计时小组件

先看效果 代码 <style>.alert-sec-circle {stroke-dasharray: 735;transition: stroke-dashoffset 1s linear;} </style><div style"width: 110px; height: 110px; float: left;"><svg style"width:110px;height:110px;"><cir…

【Qt】:界面优化(一:基本语法)

界面优化 一.基本语法1.设置指定控件样式2.设置全局控件样式3.从文件加载样式表4.使⽤Qt Designer编辑样式&#xff08;最常用&#xff09; 二.选择器1.概述2.子控件选择器3.伪类型选择器 三.盒模型 在网页前端开发领域中,CSS是一个至关重要的部分.描述了一个网页的"样式&…

快速删除node_modules依赖包的命令rimraf

1、安装rimraf npm install -g rimraf 2、使用命令删除node_modules rimraf node_modules *** window系统&#xff0c;使用命令很快就删除node_modules ***

Jmeter 场景测试:登录--上传--下载--登出

为了练习Jmeter的使用&#xff0c;今天我要测试的场景是“登录--上传--下载--登出”这样一个过程. 测试的目标是我曾经练手写的一个文件分享系统&#xff0c;它要求用户只有登录后才可以下载想要的文件。 Jmeter总体结构&#xff1a; 第一步&#xff1a;添加HTTP Cookie管理器…

微信公众号-获取用户位置

目前获取方式为&#xff0c;在用户进入公众号时&#xff0c;提示是否允许获取地理位置&#xff0c;允许后&#xff0c;将地理位置在每次进入公众号时上报给公众号。 则可以根据公众号开发文档&#xff0c;进行上报提示&#xff0c;例如引入邮件系统&#xff0c;进行管理员提示&…

vscode如何方便地添加todo和管理todo

如果想在vscode中更加方便的添加和管理TODO标签&#xff0c;比如添加高亮提醒和查看哪里有TODO标签等&#xff0c;就可以通过安装插件快速实现。 安装插件 VSCode关于TODO使用人数最多的插件是TODO Height和Todo Tree 按住 CtrlShiftX按键进入应用扩展商店&#xff0c;输入to…

Jmeter03:直连数据库

1 Jmete组件&#xff1a;直连数据库 1.1 是什么&#xff1f; 让Jmeter直接和数据库交互 1.2 为什么&#xff1f; 之前是通过接口操作数据库&#xff0c;可能出现的问题&#xff1a;比如查询可能有漏查误查的情况&#xff0c;解决方案是人工对不&#xff0c;效率低且有安全隐患…

【C++题解】1317. 正多边形每个内角的度数?

问题&#xff1a;1317. 正多边形每个内角的度数&#xff1f; 类型&#xff1a;基本运算、小数运算 题目描述&#xff1a; 根据多边形内角和定理&#xff0c;正多边形内角和等于&#xff1a;&#xff08; n&#xff0d;2 &#xff09; 180∘ ( n 大于等于 3 且 n 为整数&#…

STM32 PB3 PB4 无法作为 GPIO 使用解决办法

如下所示&#xff0c;PA13 PA14 PB3 PB4 PB5, 默认是JTAG SWD的 PIN, 需要引脚ReMap 才能作为GPIO 使用。 HAL库解决办法 // __HAL_AFIO_REMAP_SWJ_ENABLE(); //Full SWJ (JTAG-DP SW-DP):// __HAL_AFIO_REMAP_SWJ_NONJTRST(); //Full SWJ (JTAG-DP SW-DP) but without NJTR…
最新文章