当前位置: 代码网 > it编程>编程语言>C# > C#创建磁性窗体的实现方法

C#创建磁性窗体的实现方法

2024年05月28日 C# 我要评论
一、磁性窗体经常会遇到一种情况,即当拖动一个窗体(主窗体)时,其他窗体(子窗体)随着该窗体移动,当拖动子窗体时,其他窗体将不跟随移动,这就是磁性窗体。二、磁性窗体的实现方法在主窗体移动时,通过改变跟随

一、磁性窗体

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

二、磁性窗体的实现方法

在主窗体移动时,通过改变跟随窗体的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#如何手动改变自制窗体的大小_c#教程_代码网 (jb51.net)

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

三个窗体吸引在一起

三个窗体被拖动分开 

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

以上就是c#创建磁性窗体的实现方法的详细内容,更多关于c#创建磁性窗体的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com