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

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

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

    1. private void panel_Title_MouseMove(object sender,MouseEventArgs e)
    2. if(e.Button==MouseButtons.Left)
    3. {
    4. Point myPosittion=Control.MousePosition; //获取当前鼠标的屏幕坐标
    5. myPosittion.Offset(CPoint.X,CPoint.Y); //以屏幕坐标平移指定的量
    6. DesktopLocation=myPosittion; //设置当前窗体在屏幕上的位置
    7. }

    (2)Left属性

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

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

    (3)Top属性

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

    1. public int Top{get;set;}
    2. 参数说明属性值:窗体上边缘与其容器的工作区上边缘之间的距离(以像素为单位)。

    二、设计一个磁性窗体的实例

            本实例将制作一个磁性窗体,当拖动主窗体移动时,两个子窗体如果相连,则跟随移动。

    • 三个窗体:主窗体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

    1. // 类设计
    2. namespace _185
    3. {
    4. internal class FrmClass
    5. {
    6. #region 磁性窗体-公共变量
    7. //记录窗体的隐藏与显示
    8. public static bool Frm_ListShow = false;
    9. public static bool Frm_LibrettoShow = false;
    10. public static bool Frm_ScreenShow = false;
    11. //记录鼠标的当前位置
    12. public static Point CPoint;
    13. public static Point FrmPoint;
    14. public static int Gap = 10;//设置窗体间相互吸引的缝隙尺寸
    15. //Frm_Play窗体的位置及大小
    16. public static int Frm_Play_Top = 0;
    17. public static int Frm_Play_Left = 0;
    18. public static int Frm_Play_Width = 0;
    19. public static int Frm_Play_Height = 0;
    20. public static bool Is_TwoAssitForm_AdhereTo = false;//辅助窗体是否磁性在一起
    21. //Frm_ListBos窗体的位置及大小
    22. public static int Frm_List_Top = 0;
    23. public static int Frm_List_Left = 0;
    24. public static int Frm_List_Width = 0;
    25. public static int Frm_List_Height = 0;
    26. public static bool Is_Frm_List_AdhereTo = false;//辅助窗体是否与主窗体磁性在一起
    27. //Frm_Libretto窗体的位置及大小
    28. public static int Frm_Libretto_Top = 0;
    29. public static int Frm_Libretto_Left = 0;
    30. public static int Frm_Libretto_Width = 0;
    31. public static int Frm_Libretto_Height = 0;
    32. public static bool Is_Frm_Libretto_AdhereTo = false;//辅助窗体是否与主窗体磁性在一起
    33. //窗体之间的距离差
    34. public static int Frm_List_Gap_Top = 0;
    35. public static int Frm_List_Gap_Left = 0;
    36. public static int Frm_Libretto_Gap_Top = 0;
    37. public static int Frm_Libretto_Gap_Left = 0;
    38. #endregion
    39. #region 检测各窗体是否连接在一起
    40. ///
    41. /// 检测各窗体是否连接在一起
    42. ///
    43. public static void Is_Addhered_Check()
    44. {
    45. //Frm_ListBos与主窗体
    46. bool Temp_Magnetism = false;
    47. if ((Frm_Play_Top - Frm_List_Top) == 0)
    48. Temp_Magnetism = true;
    49. if ((Frm_Play_Left - Frm_List_Left) == 0)
    50. Temp_Magnetism = true;
    51. if ((Frm_Play_Left - Frm_List_Left - Frm_List_Width) == 0)
    52. Temp_Magnetism = true;
    53. if ((Frm_Play_Left - Frm_List_Left + Frm_List_Width) == 0)
    54. Temp_Magnetism = true;
    55. if ((Frm_Play_Top - Frm_List_Top - Frm_List_Height) == 0)
    56. Temp_Magnetism = true;
    57. if ((Frm_Play_Top - Frm_List_Top + Frm_List_Height) == 0)
    58. Temp_Magnetism = true;
    59. if (Temp_Magnetism)
    60. Is_Frm_List_AdhereTo = true;
    61. //Frm_Libretto与主窗体
    62. Temp_Magnetism = false;
    63. if ((Frm_Play_Top - Frm_Libretto_Top) == 0)
    64. Temp_Magnetism = true;
    65. if ((Frm_Play_Left - Frm_Libretto_Left) == 0)
    66. Temp_Magnetism = true;
    67. if ((Frm_Play_Left - Frm_Libretto_Left - Frm_Libretto_Width) == 0)
    68. Temp_Magnetism = true;
    69. if ((Frm_Play_Left - Frm_Libretto_Left + Frm_Libretto_Width) == 0)
    70. Temp_Magnetism = true;
    71. if ((Frm_Play_Top - Frm_Libretto_Top - Frm_Libretto_Height) == 0)
    72. Temp_Magnetism = true;
    73. if ((Frm_Play_Top - Frm_Libretto_Top + Frm_Libretto_Height) == 0)
    74. Temp_Magnetism = true;
    75. if (Temp_Magnetism)
    76. Is_Frm_Libretto_AdhereTo = true;
    77. //两个辅窗体
    78. Temp_Magnetism = false;
    79. if ((Frm_List_Top - Frm_Libretto_Top) == 0)
    80. Temp_Magnetism = true;
    81. if ((Frm_List_Left - Frm_Libretto_Left) == 0)
    82. Temp_Magnetism = true;
    83. if ((Frm_List_Left - Frm_Libretto_Left - Frm_Libretto_Width) == 0)
    84. Temp_Magnetism = true;
    85. if ((Frm_List_Left - Frm_Libretto_Left + Frm_Libretto_Width) == 0)
    86. Temp_Magnetism = true;
    87. if ((Frm_List_Top - Frm_Libretto_Top - Frm_Libretto_Height) == 0)
    88. Temp_Magnetism = true;
    89. if ((Frm_List_Top - Frm_Libretto_Top + Frm_Libretto_Height) == 0)
    90. Temp_Magnetism = true;
    91. if (Temp_Magnetism)
    92. Is_TwoAssitForm_AdhereTo = true;
    93. }
    94. #endregion
    95. #region 利用窗体上的控件移动窗体
    96. ///
    97. /// 利用控件移动窗体
    98. ///
    99. /// 窗体
    100. /// 控件的移动事件
    101. public static void MoveForm(Form Frm, MouseEventArgs e)
    102. {
    103. if (e.Button == MouseButtons.Left)
    104. {
    105. Point myPosittion = Control.MousePosition; //获取当前鼠标的屏幕坐标
    106. myPosittion.Offset(CPoint.X, CPoint.Y); //重载当前鼠标的位置
    107. Frm.DesktopLocation = myPosittion; //设置当前窗体在屏幕上的位置
    108. }
    109. }
    110. #endregion
    111. #region 计算窗体之间的缝隙
    112. ///
    113. /// 计算窗体之间的距离差
    114. ///
    115. /// 窗体
    116. /// 跟随窗体
    117. public static void Calculate_Gap(Form Frm, Form Follow)
    118. {
    119. switch (Follow.Name)
    120. {
    121. case "Frm_ListBox":
    122. {
    123. Frm_List_Gap_Top = Follow.Top - Frm.Top;
    124. Frm_List_Gap_Left = Follow.Left - Frm.Left;
    125. break;
    126. }
    127. case "Frm_Libretto":
    128. {
    129. Frm_Libretto_Gap_Top = Follow.Top - Frm.Top;
    130. Frm_Libretto_Gap_Left = Follow.Left - Frm.Left;
    131. break;
    132. }
    133. }
    134. }
    135. #endregion
    136. #region 磁性窗体的移动
    137. ///
    138. /// 磁性窗体的移动
    139. ///
    140. /// 窗体
    141. /// 控件的移动事件
    142. /// 跟随窗体
    143. public static void MoveManyForm(Form Frm, MouseEventArgs e, Form Follow)
    144. {
    145. ArgumentNullException.ThrowIfNull(Frm);
    146. if (e.Button == MouseButtons.Left)
    147. {
    148. int Tem_Left = 0;
    149. int Tem_Top = 0;
    150. Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
    151. switch (Follow.Name)
    152. {
    153. case "Frm_ListBox":
    154. {
    155. Tem_Top = Frm_List_Gap_Top - FrmPoint.Y;
    156. Tem_Left = Frm_List_Gap_Left - FrmPoint.X;
    157. break;
    158. }
    159. case "Frm_Libretto":
    160. {
    161. Tem_Top = Frm_Libretto_Gap_Top - FrmPoint.Y;
    162. Tem_Left = Frm_Libretto_Gap_Left - FrmPoint.X;
    163. break;
    164. }
    165. }
    166. myPosittion.Offset(Tem_Left, Tem_Top);
    167. Follow.DesktopLocation = myPosittion;
    168. }
    169. }
    170. #endregion
    171. #region 对窗体的位置进行初始化
    172. ///
    173. /// 对窗体的位置进行初始化
    174. ///
    175. /// 窗体
    176. public static void FrmInitialize(Form Frm)
    177. {
    178. switch (Frm.Name)
    179. {
    180. case "Frm_Play":
    181. {
    182. Frm_Play_Top = Frm.Top;
    183. Frm_Play_Left = Frm.Left;
    184. Frm_Play_Width = Frm.Width;
    185. Frm_Play_Height = Frm.Height;
    186. break;
    187. }
    188. case "Frm_ListBox":
    189. {
    190. Frm_List_Top = Frm.Top;
    191. Frm_List_Left = Frm.Left;
    192. Frm_List_Width = Frm.Width;
    193. Frm_List_Height = Frm.Height;
    194. break;
    195. }
    196. case "Frm_Libretto":
    197. {
    198. Frm_Libretto_Top = Frm.Top;
    199. Frm_Libretto_Left = Frm.Left;
    200. Frm_Libretto_Width = Frm.Width;
    201. Frm_Libretto_Height = Frm.Height;
    202. break;
    203. }
    204. }
    205. }
    206. #endregion
    207. #region 存储各窗体的当前信息
    208. ///
    209. /// 存储各窗体的当前信息
    210. ///
    211. /// 窗体
    212. /// 控件的移动事件
    213. public static void FrmPlace(Form Frm)
    214. {
    215. FrmInitialize(Frm);
    216. FrmMagnetism(Frm);
    217. }
    218. #endregion
    219. #region 窗体的磁性设置
    220. ///
    221. /// 窗体的磁性设置
    222. ///
    223. /// 窗体
    224. public static void FrmMagnetism(Form Frm)
    225. {
    226. if (Frm.Name != "Frm_Play")
    227. {
    228. FrmMagnetismCount(Frm, Frm_Play_Top, Frm_Play_Left, Frm_Play_Width, Frm_Play_Height, "Frm_Play");
    229. }
    230. if (Frm.Name != "Frm_ListBos")
    231. {
    232. FrmMagnetismCount(Frm, Frm_List_Top, Frm_List_Left, Frm_List_Width, Frm_List_Height, "Frm_ListBos");
    233. }
    234. if (Frm.Name != "Frm_Libratto")
    235. {
    236. FrmMagnetismCount(Frm, Frm_Libretto_Top, Frm_Libretto_Left, Frm_Libretto_Width, Frm_Libretto_Height, "Frm_Libratto");
    237. }
    238. FrmInitialize(Frm);
    239. }
    240. #endregion
    241. #region 磁性窗体的计算
    242. ///
    243. /// 磁性窗体的计算
    244. ///
    245. /// 窗体
    246. /// 控件的移动事件
    247. public static void FrmMagnetismCount(Form Frm, int top, int left, int width, int height, string Mforms)
    248. {
    249. bool Tem_Magnetism = false; //判断是否有磁性发生
    250. string Tem_MainForm = ""; //临时记录主窗体
    251. string Tem_AssistForm = ""; //临时记录辅窗体
    252. //上面进行磁性窗体
    253. if ((Frm.Top + Frm.Height - top) <= Gap && (Frm.Top + Frm.Height - top) >= -Gap)
    254. {
    255. //当一个主窗体不包含辅窗体时
    256. if ((Frm.Left >= left && Frm.Left <= (left + width)) || ((Frm.Left + Frm.Width) >= left && (Frm.Left + Frm.Width) <= (left + width)))
    257. {
    258. Frm.Top = top - Frm.Height;
    259. if ((Frm.Left - left) <= Gap && (Frm.Left - left) >= -Gap)
    260. Frm.Left = left;
    261. Tem_Magnetism = true;
    262. }
    263. //当一个主窗体包含辅窗体时
    264. if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width))
    265. {
    266. Frm.Top = top - Frm.Height;
    267. if ((Frm.Left - left) <= Gap && (Frm.Left - left) >= -Gap)
    268. Frm.Left = left;
    269. Tem_Magnetism = true;
    270. }
    271. }
    272. //下面进行磁性窗体
    273. if ((Frm.Top - (top + height)) <= Gap && (Frm.Top - (top + height)) >= -Gap)
    274. {
    275. //当一个主窗体不包含辅窗体时
    276. if ((Frm.Left >= left && Frm.Left <= (left + width)) || ((Frm.Left + Frm.Width) >= left && (Frm.Left + Frm.Width) <= (left + width)))
    277. {
    278. Frm.Top = top + height;
    279. if ((Frm.Left - left) <= Gap && (Frm.Left - left) >= -Gap)
    280. Frm.Left = left;
    281. Tem_Magnetism = true;
    282. }
    283. //当一个主窗体包含辅窗体时
    284. if (Frm.Left <= left && (Frm.Left + Frm.Width) >= (left + width))
    285. {
    286. Frm.Top = top + height;
    287. if ((Frm.Left - left) <= Gap && (Frm.Left - left) >= -Gap)
    288. Frm.Left = left;
    289. Tem_Magnetism = true;
    290. }
    291. }
    292. //左面进行磁性窗体
    293. if ((Frm.Left + Frm.Width - left) <= Gap && (Frm.Left + Frm.Width - left) >= -Gap)
    294. {
    295. //当一个主窗体不包含辅窗体时
    296. if ((Frm.Top > top && Frm.Top <= (top + height)) || ((Frm.Top + Frm.Height) >= top && (Frm.Top + Frm.Height) <= (top + height)))
    297. {
    298. Frm.Left = left - Frm.Width;
    299. if ((Frm.Top - top) <= Gap && (Frm.Top - top) >= -Gap)
    300. Frm.Top = top;
    301. Tem_Magnetism = true;
    302. }
    303. //当一个主窗体包含辅窗体时
    304. if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height))
    305. {
    306. Frm.Left = left - Frm.Width;
    307. if ((Frm.Top - top) <= Gap && (Frm.Top - top) >= -Gap)
    308. Frm.Top = top;
    309. Tem_Magnetism = true;
    310. }
    311. }
    312. //右面进行磁性窗体
    313. if ((Frm.Left - (left + width)) <= Gap && (Frm.Left - (left + width)) >= -Gap)
    314. {
    315. //当一个主窗体不包含辅窗体时
    316. if ((Frm.Top > top && Frm.Top <= (top + height)) || ((Frm.Top + Frm.Height) >= top && (Frm.Top + Frm.Height) <= (top + height)))
    317. {
    318. Frm.Left = left + width;
    319. if ((Frm.Top - top) <= Gap && (Frm.Top - top) >= -Gap)
    320. Frm.Top = top;
    321. Tem_Magnetism = true;
    322. }
    323. //当一个主窗体包含辅窗体时
    324. if (Frm.Top <= top && (Frm.Top + Frm.Height) >= (top + height))
    325. {
    326. Frm.Left = left + width;
    327. if ((Frm.Top - top) <= Gap && (Frm.Top - top) >= -Gap)
    328. Frm.Top = top;
    329. Tem_Magnetism = true;
    330. }
    331. }
    332. if (Frm.Name == "Frm_Play")
    333. Tem_MainForm = "Frm_Play";
    334. else
    335. Tem_AssistForm = Frm.Name;
    336. if (Mforms == "Frm_Play")
    337. Tem_MainForm = "Frm_Play";
    338. else
    339. Tem_AssistForm = Mforms;
    340. if (Tem_MainForm == "")
    341. {
    342. Is_TwoAssitForm_AdhereTo = Tem_Magnetism;
    343. }
    344. else
    345. {
    346. switch (Tem_AssistForm)
    347. {
    348. case "Frm_ListBos":
    349. Is_Frm_List_AdhereTo = Tem_Magnetism;
    350. break;
    351. case "Frm_Libratto":
    352. Is_Frm_Libretto_AdhereTo = Tem_Magnetism;
    353. break;
    354. }
    355. }
    356. }
    357. #endregion
    358. #region 恢复窗体的初始大小
    359. ///
    360. /// 恢复窗体的初始大小(当松开鼠标时,如果窗体的大小小于300*200,恢复初始状态)
    361. ///
    362. /// 窗体
    363. public static void FrmScreen_FormerlySize(Form Frm, int PWidth, int PHeight)
    364. {
    365. if (Frm.Width < PWidth || Frm.Height < PHeight)
    366. {
    367. Frm.Width = PWidth;
    368. Frm.Height = PHeight;
    369. //Example_Size = false;
    370. }
    371. }
    372. #endregion
    373. }
    374. }

    (3)主窗体

    1.Frm_Play.cs

    1. namespace _185
    2. {
    3. public partial class Frm_Play : Form
    4. {
    5. public Frm_Play()
    6. {
    7. InitializeComponent();
    8. }
    9. #region 公共变量
    10. FrmClass Cla_FrmClass = new();
    11. public static Form F_List = new();
    12. public static Form F_Libretto = new();
    13. public static Form F_Screen = new();
    14. #endregion
    15. private void Frm_Play_Load(object sender, EventArgs e)
    16. {
    17. FrmClass.FrmInitialize(this); //窗体位置的初始化
    18. }
    19. private void Panel1_MouseDown(object sender, MouseEventArgs e)
    20. {
    21. if (e.Button == MouseButtons.Left) //按下的是否为鼠标左键
    22. {
    23. FrmClass.Is_Addhered_Check(); //检测各窗体是否连在一起
    24. int Tem_Y = e.Y;
    25. FrmClass.FrmPoint = new Point(e.X, Tem_Y);//获取鼠标在窗体上的位置,用于磁性窗体
    26. FrmClass.CPoint = new Point(-e.X, -Tem_Y);//获取鼠标在屏幕上的位置,用于窗体的移动
    27. if (FrmClass.Is_Frm_List_AdhereTo) //如果与frm_ListBox窗体相连接
    28. {
    29. FrmClass.Calculate_Gap(this, F_List); //计算窗体的距离差
    30. if (FrmClass.Is_TwoAssitForm_AdhereTo) //两个辅窗体是否连接在一起
    31. {
    32. FrmClass.Calculate_Gap(this, F_Libretto);//计算窗体的距离差
    33. }
    34. }
    35. if (FrmClass.Is_Frm_Libretto_AdhereTo) //如果与frm_Libretto窗体相连接
    36. {
    37. FrmClass.Calculate_Gap(this, F_Libretto); //计算窗体的距离差
    38. if (FrmClass.Is_TwoAssitForm_AdhereTo) //两个辅窗体是否连接在一起
    39. {
    40. FrmClass.Calculate_Gap(this, F_List); //计算窗体的距离差
    41. }
    42. }
    43. }
    44. }
    45. private void Panel1_MouseMove(object sender, MouseEventArgs e)
    46. {
    47. if (e.Button == MouseButtons.Left) //按下的是否为鼠标左键
    48. {
    49. FrmClass.MoveForm(this, e); //利用控件移动窗体
    50. if (FrmClass.Is_Frm_List_AdhereTo) //如果frm_ListBox窗体与主窗体连接
    51. {
    52. FrmClass.MoveManyForm(this, e, F_List);//磁性窗体的移动
    53. FrmClass.FrmInitialize(F_List); //对frm_ListBox窗体的位置进行初始化
    54. if (FrmClass.Is_TwoAssitForm_AdhereTo) //如果两个子窗体连接在一起
    55. {
    56. FrmClass.MoveManyForm(this, e, F_Libretto);
    57. FrmClass.FrmInitialize(F_Libretto);
    58. }
    59. }
    60. if (FrmClass.Is_Frm_Libretto_AdhereTo) //如果frm_Libretto窗体与主窗体连接
    61. {
    62. FrmClass.MoveManyForm(this, e, F_Libretto);
    63. FrmClass.FrmInitialize(F_Libretto);
    64. if (FrmClass.Is_TwoAssitForm_AdhereTo)
    65. {
    66. FrmClass.MoveManyForm(this, e, F_List);
    67. FrmClass.FrmInitialize(F_List);
    68. }
    69. }
    70. FrmClass.FrmInitialize(this);
    71. }
    72. }
    73. private void Panel1_MouseUp(object sender, MouseEventArgs e)
    74. {
    75. FrmClass.FrmPlace(this);
    76. }
    77. private void Frm_Play_Shown(object sender, EventArgs e)
    78. {
    79. //显示列表窗体
    80. F_List = new Frm_ListBox
    81. {
    82. ShowInTaskbar = false
    83. };
    84. FrmClass.Frm_ListShow = true;
    85. F_List.Show();
    86. //显示歌词窗体
    87. F_Libretto = new Frm_Libretto
    88. {
    89. ShowInTaskbar = false,
    90. Left = Left + Width,
    91. Top = Top
    92. };
    93. FrmClass.Frm_LibrettoShow = true;
    94. F_Libretto.Show();
    95. //各窗体位置的初始化
    96. FrmClass.FrmInitialize(F_List);
    97. FrmClass.FrmInitialize(F_Libretto);
    98. }
    99. private void Panel2_Click(object sender, EventArgs e)
    100. {
    101. F_List.Close();
    102. F_List.Dispose();
    103. F_Libretto.Close();
    104. F_Libretto.Dispose();
    105. F_Screen.Close();
    106. F_Screen.Dispose();
    107. Close();
    108. }
    109. private void Panel1_Click(object sender, EventArgs e)
    110. {
    111. F_List.Focus();
    112. F_Libretto.Focus();
    113. Focus();
    114. }
    115. }
    116. }

    2.Frm_Play.Designer.cs

    1. namespace _185
    2. {
    3. partial class Frm_Play
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. panel1 = new Panel();
    29. pictureBox1 = new PictureBox();
    30. panel2 = new Panel();
    31. ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
    32. SuspendLayout();
    33. //
    34. // panel1
    35. //
    36. panel1.BackgroundImage = Properties.Resources._5;
    37. panel1.BackgroundImageLayout = ImageLayout.Stretch;
    38. panel1.Dock = DockStyle.Top;
    39. panel1.Location = new Point(0, 0);
    40. panel1.Name = "panel1";
    41. panel1.Size = new Size(290, 31);
    42. panel1.TabIndex = 0;
    43. panel1.Click += Panel1_Click;
    44. panel1.MouseDown += Panel1_MouseDown;
    45. panel1.MouseMove += Panel1_MouseMove;
    46. panel1.MouseUp += Panel1_MouseUp;
    47. //
    48. // pictureBox1
    49. //
    50. pictureBox1.BackgroundImage = Properties.Resources._01;
    51. pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
    52. pictureBox1.Dock = DockStyle.Fill;
    53. pictureBox1.Location = new Point(0, 31);
    54. pictureBox1.Name = "pictureBox1";
    55. pictureBox1.Size = new Size(290, 89);
    56. pictureBox1.TabIndex = 1;
    57. pictureBox1.TabStop = false;
    58. //
    59. // panel2
    60. //
    61. panel2.BackgroundImage = Properties.Resources.Close;
    62. panel2.BackgroundImageLayout = ImageLayout.Stretch;
    63. panel2.Location = new Point(265, 5);
    64. panel2.Name = "panel2";
    65. panel2.Size = new Size(18, 18);
    66. panel2.TabIndex = 0;
    67. panel2.Click += Panel2_Click;
    68. //
    69. // Frm_Play
    70. //
    71. AutoScaleDimensions = new SizeF(7F, 17F);
    72. AutoScaleMode = AutoScaleMode.Font;
    73. ClientSize = new Size(290, 120);
    74. Controls.Add(panel2);
    75. Controls.Add(pictureBox1);
    76. Controls.Add(panel1);
    77. FormBorderStyle = FormBorderStyle.None;
    78. Name = "Frm_Play";
    79. Text = "Form1";
    80. Load += Frm_Play_Load;
    81. Shown += Frm_Play_Shown;
    82. ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
    83. ResumeLayout(false);
    84. }
    85. #endregion
    86. private Panel panel1;
    87. private PictureBox pictureBox1;
    88. private Panel panel2;
    89. }
    90. }

    (4)子窗体1

    1.Frm_ListBox.cs

    1. namespace _185
    2. {
    3. public partial class Frm_ListBox : Form
    4. {
    5. public Frm_ListBox()
    6. {
    7. InitializeComponent();
    8. }
    9. #region 公共变量
    10. FrmClass Cla_FrmClass = new();
    11. #endregion
    12. private void Frm_ListBox_Load(object sender, EventArgs e)
    13. {
    14. Left = FrmClass.Frm_Play_Left;
    15. Top = FrmClass.Frm_Play_Top + FrmClass.Frm_Play_Height;
    16. FrmClass.FrmInitialize(this);
    17. }
    18. private void Panel1_MouseDown(object sender, MouseEventArgs e)
    19. {
    20. FrmClass.CPoint = new Point(-e.X, -e.Y);
    21. }
    22. private void Panel1_MouseMove(object sender, MouseEventArgs e)
    23. {
    24. FrmClass.Is_TwoAssitForm_AdhereTo = false;
    25. FrmClass.Is_Frm_List_AdhereTo = false;
    26. FrmClass.MoveForm(this, e);
    27. }
    28. private void Panel1_MouseUp(object sender, MouseEventArgs e)
    29. {
    30. FrmClass.FrmPlace(this);
    31. }
    32. }
    33. }

    2.Frm_ListBox.Designer.cs

    1. namespace _185
    2. {
    3. partial class Frm_ListBox
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. panel1 = new Panel();
    29. pictureBox1 = new PictureBox();
    30. ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
    31. SuspendLayout();
    32. //
    33. // panel1
    34. //
    35. panel1.BackgroundImage = Properties.Resources._5;
    36. panel1.Dock = DockStyle.Top;
    37. panel1.Location = new Point(0, 0);
    38. panel1.Name = "panel1";
    39. panel1.Size = new Size(290, 31);
    40. panel1.TabIndex = 0;
    41. panel1.MouseDown += Panel1_MouseDown;
    42. panel1.MouseMove += Panel1_MouseMove;
    43. panel1.MouseUp += Panel1_MouseUp;
    44. //
    45. // pictureBox1
    46. //
    47. pictureBox1.BackgroundImage = Properties.Resources._02;
    48. pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
    49. pictureBox1.Dock = DockStyle.Fill;
    50. pictureBox1.Location = new Point(0, 31);
    51. pictureBox1.Name = "pictureBox1";
    52. pictureBox1.Size = new Size(290, 89);
    53. pictureBox1.TabIndex = 1;
    54. pictureBox1.TabStop = false;
    55. //
    56. // Frm_ListBox
    57. //
    58. AutoScaleDimensions = new SizeF(7F, 17F);
    59. AutoScaleMode = AutoScaleMode.Font;
    60. ClientSize = new Size(290, 120);
    61. Controls.Add(pictureBox1);
    62. Controls.Add(panel1);
    63. FormBorderStyle = FormBorderStyle.None;
    64. Name = "Frm_ListBox";
    65. Text = "Form1";
    66. Load += Frm_ListBox_Load;
    67. ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
    68. ResumeLayout(false);
    69. }
    70. #endregion
    71. private Panel panel1;
    72. private PictureBox pictureBox1;
    73. }
    74. }

    (5)子窗体2

    1.Frm_Libretto.cs

    1. namespace _185
    2. {
    3. public partial class Frm_Libretto : Form
    4. {
    5. public Frm_Libretto()
    6. {
    7. InitializeComponent();
    8. }
    9. #region 公共变量
    10. FrmClass Cla_FrmClass = new();
    11. #endregion
    12. private void Frm_Libretto_Load(object sender, EventArgs e)
    13. {
    14. Left = FrmClass.Frm_Play_Left;
    15. Top = FrmClass.Frm_Play_Top + FrmClass.Frm_Play_Height;
    16. FrmClass.FrmInitialize(this);
    17. }
    18. private void Panel1_MouseDown(object sender, MouseEventArgs e)
    19. {
    20. FrmClass.CPoint = new Point(-e.X, -e.Y);
    21. }
    22. private void Panel1_MouseMove(object sender, MouseEventArgs e)
    23. {
    24. FrmClass.Is_TwoAssitForm_AdhereTo = false;
    25. FrmClass.Is_Frm_List_AdhereTo = false;
    26. FrmClass.MoveForm(this, e);
    27. }
    28. private void Panel1_MouseUp(object sender, MouseEventArgs e)
    29. {
    30. FrmClass.FrmPlace(this);
    31. }
    32. }
    33. }

    2.Frm_Libretto.Designer.cs

    1. namespace _185
    2. {
    3. partial class Frm_Libretto
    4. {
    5. ///
    6. /// Required designer variable.
    7. ///
    8. private System.ComponentModel.IContainer components = null;
    9. ///
    10. /// Clean up any resources being used.
    11. ///
    12. /// true if managed resources should be disposed; otherwise, false.
    13. protected override void Dispose(bool disposing)
    14. {
    15. if (disposing && (components != null))
    16. {
    17. components.Dispose();
    18. }
    19. base.Dispose(disposing);
    20. }
    21. #region Windows Form Designer generated code
    22. ///
    23. /// Required method for Designer support - do not modify
    24. /// the contents of this method with the code editor.
    25. ///
    26. private void InitializeComponent()
    27. {
    28. panel1 = new Panel();
    29. pictureBox1 = new PictureBox();
    30. ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
    31. SuspendLayout();
    32. //
    33. // panel1
    34. //
    35. panel1.BackgroundImage = Properties.Resources._5;
    36. panel1.BackgroundImageLayout = ImageLayout.Stretch;
    37. panel1.Dock = DockStyle.Top;
    38. panel1.Location = new Point(0, 0);
    39. panel1.Name = "panel1";
    40. panel1.Size = new Size(290, 31);
    41. panel1.TabIndex = 0;
    42. panel1.MouseDown += Panel1_MouseDown;
    43. panel1.MouseMove += Panel1_MouseMove;
    44. panel1.MouseUp += Panel1_MouseUp;
    45. //
    46. // pictureBox1
    47. //
    48. pictureBox1.BackgroundImage = Properties.Resources._03;
    49. pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
    50. pictureBox1.Dock = DockStyle.Fill;
    51. pictureBox1.Location = new Point(0, 31);
    52. pictureBox1.Name = "pictureBox1";
    53. pictureBox1.Size = new Size(290, 209);
    54. pictureBox1.TabIndex = 1;
    55. pictureBox1.TabStop = false;
    56. //
    57. // Frm_Libretto
    58. //
    59. AutoScaleDimensions = new SizeF(7F, 17F);
    60. AutoScaleMode = AutoScaleMode.Font;
    61. ClientSize = new Size(290, 240);
    62. Controls.Add(pictureBox1);
    63. Controls.Add(panel1);
    64. FormBorderStyle = FormBorderStyle.None;
    65. Name = "Frm_Libretto";
    66. Text = "Form2";
    67. Load += Frm_Libretto_Load;
    68. ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
    69. ResumeLayout(false);
    70. }
    71. #endregion
    72. private Panel panel1;
    73. private PictureBox pictureBox1;
    74. }
    75. }

    (6)生成效果

             三个窗体吸引在一起

             三个窗体被拖动分开

     

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

  • 相关阅读:
    在mybatis的xml中使用枚举来做判断条件
    Linux上部署zabbix 6.x
    计算机毕业设计ssm基于ssm的牧场管理系统6ui1j系统+程序+源码+lw+远程部署
    Node编写更新用户头像接口
    java计算机毕业设计教务排课系统源码+mysql数据库+系统+lw文档+部署
    nodejs处理图片的几种方法,使用sharp,jimp,webconvert
    测试管理并不好做,做好以下几点可直上云霄!
    电信保温杯笔记——《统计学习方法(第二版)——李航》第18章 概率潜在语义分析
    深入理解计算机系统——第九章 Virtual Memory
    两两交换链表中的节点
  • 原文地址:https://blog.csdn.net/wenchm/article/details/137841168