欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#創(chuàng)建磁性窗體的實現(xiàn)方法

 更新時間:2024年04月17日 09:48:14   作者:wenchm  
經(jīng)常會遇到一種情況,即當(dāng)拖動一個窗體(主窗體)時,其他窗體(子窗體)隨著該窗體移動,當(dāng)拖動子窗體時,其他窗體將不跟隨移動,這就是磁性窗體,所以本文給大家介紹了C#創(chuàng)建磁性窗體的實現(xiàn)方法,需要的朋友可以參考下

一、磁性窗體

經(jīng)常會遇到一種情況,即當(dāng)拖動一個窗體(主窗體)時,其他窗體(子窗體)隨著該窗體移動,當(dāng)拖動子窗體時,其他窗體將不跟隨移動,這就是磁性窗體。

二、磁性窗體的實現(xiàn)方法

在主窗體移動時,通過改變跟隨窗體的Left和Top屬性值實現(xiàn)“磁性”。

(1)無標(biāo)題窗體的移動

無標(biāo)題窗體的移動主要是通過控件來移動窗體,比如,用Panel控件來進行。首先,在Panel控件的MouseDown事件中將鼠標(biāo)按下時的位置值(負(fù)值)存入到全局變量CPoint中,代碼如下:

private void panel_Title_MouseDown(object sender,MouseEventArgs e)
CPoint=new Point(-e.X,-e.Y);    //獲取鼠標(biāo)按下時的位置

然后,在Panel控件的MouseMove事件中按照CPoint變量的值,以屏幕坐標(biāo)平移指定的量,并用平移后的結(jié)果設(shè)置窗體的DesktopLocation屬性,代碼如下:

private void panel_Title_MouseMove(object sender,MouseEventArgs e)
if(e.Button==MouseButtons.Left)
{
    Point myPosittion=Control.MousePosition; //獲取當(dāng)前鼠標(biāo)的屏幕坐標(biāo)
    myPosittion.Offset(CPoint.X,CPoint.Y);   //以屏幕坐標(biāo)平移指定的量
    DesktopLocation=myPosittion;             //設(shè)置當(dāng)前窗體在屏幕上的位置
 
}

(2)Left屬性

該屬性用于獲取或設(shè)置控件左邊緣與其容器的工作區(qū)左邊緣之間的距離(以像素為單位)。語法格式如下:

public int Left {get;set;}
參數(shù)說明
屬性值:窗體左邊緣與其容器的工作區(qū)左邊緣之間的距離(以像素為單位)。

(3)Top屬性

該屬性用于獲取或設(shè)置控件上邊緣與其容器的工作區(qū)上邊緣之間的距離(以像素為單位)。語法格式如下:

public int Top{get;set;}
參數(shù)說明屬性值:窗體上邊緣與其容器的工作區(qū)上邊緣之間的距離(以像素為單位)。

二、設(shè)計一個磁性窗體的實例

本實例將制作一個磁性窗體,當(dāng)拖動主窗體移動時,兩個子窗體如果相連,則跟隨移動。

  • 三個窗體:主窗體Frm_Play.cs,2個子窗體:Frm_ListBox.cs、Frm_Libretto.cs;
  • 鼠標(biāo)按下任一窗體頂部的控件,可以拖動窗體;
  • 拖動子窗體時,會使得粘在一起的窗體分開,拖動主窗體時會使粘在一起的子窗體隨動;
  • 拖動主窗體靠近子窗體小于相互吸引的縫隙10時,松開鼠標(biāo),靠近的窗體會像磁鐵一樣吸引在一起;主窗體吸引子窗體后,該子窗體還可以吸引其它子窗體;
  • 雙擊主窗體的控件,激活所有窗體;

(1)資源管理器Resources.Designer.cs設(shè)計

項目使用的圖片資源應(yīng)設(shè)計到資源管理器,詳見本文作者寫的其它文章:詳解C#如何手動改變自制窗體的大小_C#教程_腳本之家 (jb51.net)

(2)公共類Frm_Play.cs

// 類設(shè)計
namespace _185
{
    internal class FrmClass
    {
 
        #region  磁性窗體-公共變量
        //記錄窗體的隱藏與顯示
        public static bool Frm_ListShow = false;
        public static bool Frm_LibrettoShow = false;
        public static bool Frm_ScreenShow = false;
 
        //記錄鼠標(biāo)的當(dāng)前位置
        public static Point CPoint;
        public static Point FrmPoint;
        public static int Gap = 10;//設(shè)置窗體間相互吸引的縫隙尺寸
 
        //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;    //獲取當(dāng)前鼠標(biāo)的屏幕坐標(biāo)
                myPosittion.Offset(CPoint.X, CPoint.Y);       //重載當(dāng)前鼠標(biāo)的位置
                Frm.DesktopLocation = myPosittion;            //設(shè)置當(dāng)前窗體在屏幕上的位置
            }
        }
        #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;//獲取當(dāng)前鼠標(biāo)的屏幕坐標(biāo)
                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  存儲各窗體的當(dāng)前信息
        /// <summary>
        /// 存儲各窗體的當(dāng)前信息
        /// </summary>
        /// <param Frm="Form">窗體</param>
        /// <param e="MouseEventArgs">控件的移動事件</param>
        public static void FrmPlace(Form Frm)
        {
            FrmInitialize(Frm);
            FrmMagnetism(Frm);
        }
        #endregion
 
        #region  窗體的磁性設(shè)置
        /// <summary>
        /// 窗體的磁性設(shè)置
        /// </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;    //判斷是否有磁性發(fā)生
            string Tem_MainForm = "";      //臨時記錄主窗體
            string Tem_AssistForm = "";    //臨時記錄輔窗體
 
            //上面進行磁性窗體
            if ((Frm.Top + Frm.Height - top) <= Gap && (Frm.Top + Frm.Height - top) >= -Gap)
            {
                //當(dāng)一個主窗體不包含輔窗體時
                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;
                }
                //當(dāng)一個主窗體包含輔窗體時
                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)
            {
                //當(dāng)一個主窗體不包含輔窗體時
                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;
                }
                //當(dāng)一個主窗體包含輔窗體時
                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)
            {
                //當(dāng)一個主窗體不包含輔窗體時
                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;
                }
                //當(dāng)一個主窗體包含輔窗體時
                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)
            {
                //當(dāng)一個主窗體不包含輔窗體時
                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;
                }
                //當(dāng)一個主窗體包含輔窗體時
                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  恢復(fù)窗體的初始大小
        /// <summary>
        /// 恢復(fù)窗體的初始大小(當(dāng)松開鼠標(biāo)時,如果窗體的大小小于300*200,恢復(fù)初始狀態(tài))
        /// </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)            //按下的是否為鼠標(biāo)左鍵
            {
                FrmClass.Is_Addhered_Check();             //檢測各窗體是否連在一起
                int Tem_Y = e.Y;
                FrmClass.FrmPoint = new Point(e.X, Tem_Y);//獲取鼠標(biāo)在窗體上的位置,用于磁性窗體
                FrmClass.CPoint = new Point(-e.X, -Tem_Y);//獲取鼠標(biāo)在屏幕上的位置,用于窗體的移動
                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)            //按下的是否為鼠標(biāo)左鍵
            {
 
                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)生成效果

三個窗體吸引在一起

三個窗體被拖動分開 

主窗體吸引子窗體再吸引子窗體 

以上就是C#創(chuàng)建磁性窗體的實現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于C#創(chuàng)建磁性窗體的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論