可以实现窗体的 吸附 移动 分离

 
 
  1. using System;
  2. using System.Drawing;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. namespace TinyBook
  6. {
  7. public enum MagneticLocation
  8. {
  9. Left = 0,
  10. Right = 1,
  11. Top = 2,
  12. Bottom = 3
  13. }
  14. public enum MagneticState
  15. {
  16. Adsorbent, // 吸附
  17. Separation // 分离
  18. }
  19. public class MagneticManager
  20. {
  21. public class ChildFormInfo
  22. {
  23. public Form Child { get; set; }
  24. public MagneticLocation Location { get; set; }
  25. public MagneticState State { get; set; }
  26. public bool CutstomSetLocation { get; set; }
  27. }
  28. public int Step { get; set; }
  29. private Form m_mainForm = null;
  30. private List<ChildFormInfo> m_childs= new List<ChildFormInfo>();
  31. public MagneticManager(Form form)
  32. {
  33. m_mainForm = form;
  34. form.LocationChanged += MainForm_LocationChanged;
  35. form.SizeChanged += MainForm_SizeChanged;
  36. form.FormClosed += MainForm_FormClosed;
  37. Step = 20;
  38. }
  39. public void addChild(Form childForm, MagneticLocation loc)
  40. {
  41. foreach (ChildFormInfo info in m_childs)
  42. {
  43. if (info.Child == childForm)
  44. {
  45. return;
  46. }
  47. }
  48. ChildFormInfo childInfo = new ChildFormInfo();
  49. childInfo.Child = childForm;
  50. childInfo.Location = loc;
  51. childInfo.State = MagneticState.Adsorbent;
  52. childInfo.CutstomSetLocation = false;
  53. childForm.LocationChanged += ChildForm_LocationChanged;
  54. childForm.SizeChanged += ChildForm_SizeChanged;
  55. childForm.FormClosed += ChildForm_FormClosed;
  56. m_childs.Add(childInfo);
  57. adsorbentChild(childInfo);
  58. }
  59. private ChildFormInfo getInfo(Form form)
  60. {
  61. if (form == null)
  62. {
  63. return null;
  64. }
  65. foreach (ChildFormInfo info in m_childs)
  66. {
  67. if (info.Child == form)
  68. {
  69. return info;
  70. }
  71. }
  72. return null;
  73. }
  74. private Point getLocation(ChildFormInfo info)
  75. {
  76. Point pos = Point.Empty;
  77. switch (info.Location)
  78. {
  79. case MagneticLocation.Left:
  80. pos = new Point(m_mainForm.Left - info.Child.Width + 14, m_mainForm.Top);
  81. break;
  82. case MagneticLocation.Right:
  83. pos = new Point(m_mainForm.Right - 14, m_mainForm.Top);
  84. break;
  85. case MagneticLocation.Top:
  86. pos = new Point(m_mainForm.Left, m_mainForm.Top - info.Child.Height);
  87. break;
  88. case MagneticLocation.Bottom:
  89. pos = new Point(m_mainForm.Left, m_mainForm.Bottom);
  90. break;
  91. default:
  92. break;
  93. }
  94. return pos;
  95. }
  96. private void setChildLocation(ChildFormInfo info, Point location)
  97. {
  98. if (info.Child == null)
  99. {
  100. return;
  101. }
  102. info.CutstomSetLocation = true;
  103. info.Child.Location = location;
  104. info.CutstomSetLocation = false;
  105. }
  106. private void setChildLocation(ChildFormInfo info, int x, int y)
  107. {
  108. setChildLocation(info, new Point(x, y));
  109. }
  110. private void resetChildLocation(ChildFormInfo info)
  111. {
  112. if (info.Child == null)
  113. {
  114. return;
  115. }
  116. Point pos = getLocation(info);
  117. setChildLocation(info, pos);
  118. }
  119. private void adsorbentChild(ChildFormInfo info)
  120. {
  121. info.State = MagneticState.Adsorbent;
  122. resetChildLocation(info);
  123. }
  124. private void separationChild(ChildFormInfo info)
  125. {
  126. info.State = MagneticState.Separation;
  127. }
  128. private void MainForm_LocationChanged(object sender, EventArgs e)
  129. {
  130. foreach (ChildFormInfo info in m_childs)
  131. {
  132. if (info.State == MagneticState.Adsorbent)
  133. {
  134. resetChildLocation(info);
  135. }
  136. }
  137. }
  138. private void MainForm_SizeChanged(object sender, EventArgs e)
  139. {
  140. foreach (ChildFormInfo info in m_childs)
  141. {
  142. if (info.State == MagneticState.Adsorbent)
  143. {
  144. resetChildLocation(info);
  145. }
  146. }
  147. }
  148. private void MainForm_FormClosed(object sender, EventArgs e)
  149. {
  150. }
  151. private void ChildForm_LocationChanged(object sender, EventArgs e)
  152. {
  153. ChildFormInfo info = getInfo(sender as Form);
  154. if (info == null)
  155. {
  156. return;
  157. }
  158. if (info.CutstomSetLocation == true)
  159. {
  160. return;
  161. }
  162. Point location = getLocation(info);
  163. if (info.Child.Left > location.X && info.Location == MagneticLocation.Right)
  164. {
  165. if (info.Child.Left - location.X > Step)
  166. {
  167. separationChild(info);
  168. }
  169. else
  170. {
  171. adsorbentChild(info);
  172. }
  173. }
  174. else if (info.Child.Left < location.X && info.Location == MagneticLocation.Left)
  175. {
  176. if (info.Child.Left - location.X < -Step)
  177. {
  178. separationChild(info);
  179. }
  180. else
  181. {
  182. adsorbentChild(info);
  183. }
  184. }
  185. if (info.Child.Top > location.Y && info.Location == MagneticLocation.Bottom)
  186. {
  187. if (info.Child.Top - location.Y > Step)
  188. {
  189. separationChild(info);
  190. }
  191. else
  192. {
  193. adsorbentChild(info);
  194. }
  195. }
  196. else if (info.Child.Top < location.Y && info.Location == MagneticLocation.Top)
  197. {
  198. if (info.Child.Top - location.Y < -Step)
  199. {
  200. separationChild(info);
  201. }
  202. else
  203. {
  204. adsorbentChild(info);
  205. }
  206. }
  207. }
  208. private void ChildForm_SizeChanged(object sender, EventArgs e)
  209. {
  210. ChildFormInfo info = getInfo(sender as Form);
  211. if (info != null && info.State == MagneticState.Adsorbent)
  212. {
  213. resetChildLocation(info);
  214. }
  215. }
  216. private void ChildForm_FormClosed(object sender, EventArgs e)
  217. {
  218. ChildFormInfo info = getInfo(sender as Form);
  219. if (info != null)
  220. {
  221. m_childs.Remove(info);
  222. }
  223. }
  224. }
  225. }

版权声明:本文为博主原创文章,欢迎转载,转载请注明出处。

C# 实现磁性窗体的更多相关文章

  1. 磁性窗体设计C#(二)

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  2. C#磁性窗体设计

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. c#轻松实现磁性窗口

    /// <summary>/// 磁性窗体函数/// </summary>/// <param name="form">窗体控件(一般传this ...

  4. MP3文件ID3信息编辑器代码开源 - 开源研究系列文章

    上次把磁性窗体的源码开源了,这次就开源另一个程序源码:MP3文件ID3信息编辑器.这个源码也比较简单,关键在于获取和写入MP3文件的这个ID3的信息即可. 这个操作信息编辑的就封装在MP3ID3.ba ...

  5. 198个经典C#WinForm实例源码(超赞) 里面的例子 .sln 目录

    \-窗体技巧\QQ窗体\QQFrm.sln; \-窗体技巧\仿XP系统的任务栏菜单\仿XP系统的任务栏菜单.sln; \-窗体技巧\向窗体中拖放图片并显示\向窗体中拖放图片并显示.sln; \-窗体技 ...

  6. 《Visual Basic开发实战1200例》包括第I卷、第II卷共计1200个例子,本书是第I卷,共计600个例子。

    本书以开发人员在项目开发中经常遇到的问题和必须掌握的技术为中心,介绍了应用Visual Basic进行程序开发各个方面的知识和技巧.主要包括基础知识.窗体界面设计.控件应用等.全书分6篇20章,共计6 ...

  7. JavaScript动画-磁性吸附

    ▓▓▓▓▓▓ 大致介绍 磁性吸附是以模拟拖拽为基础添加一个拖拽时范围的限定而来的一个效果,如果对模拟拖拽有疑问的同学请移步模拟拖拽. 源代码.效果:点这里 ▓▓▓▓▓▓ 范围限定(可视区) 先来看一个 ...

  8. javascript动画系列第二篇——磁性吸附

    × 目录 [1]范围限定 [2]拖拽范围 [3]磁性吸附 前面的话 上一篇,我们介绍了元素拖拽的实现.但在实际应用中,常常需要为拖拽的元素限定范围.而通过限定范围,再增加一些辅助的措施,就可以实现磁性 ...

  9. VB.NET设置控件和窗体的显示级别

    前言:在用VB.NET开发射频检测系统ADS时,当激活已存在的目标MDI子窗体时,被其他子窗体遮住了,导致目标MDI子窗体不能显示. 这个问题怎么解决呢?网上看到一篇帖子VB.NET设置控件和窗体的显 ...

随机推荐

  1. 【APP UI 设计模式】(一)APP UI 设计原则和流程

    一.基本原则         1.用户体验原则UCD,以用户为中心去设计         2.设计模式是可重用的设计规范实现         3.反模式是糟糕设计的典型,极力避免使用         ...

  2. 树形dp-hdu-4714-Tree2cycle

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4714 题目意思: 给一棵树,去掉一条边和增加一条边的花费都为1,求最小的花费,使该树变成一个环. 解 ...

  3. Acess错误:"文件共享锁定数溢出"

    对于ACCESS数据库,如果通过大量的SQL来操作数据库或者直接操作大量的数据时,经常会出现这种错误: "文件共享锁定数溢出" 原因如下:     Access数据库,同时操作大量 ...

  4. 关于js中的类型内容总结(类型识别)

    JS 有7种数据类型: 6种原始类型:Boollean    String   Number    Null    Underfined   Symbol 引用类型:Object 类型识别主要有以下四 ...

  5. 不能以方法的方式使用不可调用的“system.web.httprequest.querystring”

    问题描述:不能以方法的方式使用不可调用的“system.web.httprequest.querystring”解决办法:Request.QueryString["GoodsID" ...

  6. hadoop笔记之hdfs shell操作

    HDFS命令行操作 HDFS命令行操作 (以下是hadoop 1.x 版本的命令使用) 装好hadoop之前首先要进行一个格式化 hadoop namenode -format 运行之后,可以将文件夹 ...

  7. HTTP协议中keep-alive

    一 . http协议是有连接的协议,这样每一个连接过来都要重新打开一个tcp的http socket,短期内同一个host 对服务器的请求就会很慢,若是能够保持住连接,就可以节省socket open ...

  8. 解决Qt程序发布时中文乱码问题(通过QApplication.addLibraryPath加载QTextCodec插件)

    Qt程序的文字编码,是通过插件来解决的,所以我们发布的时候需要把相应的插件也发布出去,在开发者电脑上程序会自动从插件目录加载到插件,但是如果发布给别的电脑使用,需要手动指定插件路径,如下所示: int ...

  9. 再来一个学历重要性讨论——QQ技术群聊

    ----------------------------------------------------------------------- 正方:学历重要 大专,刚毕业那会太坑爹了.太受学历限制. ...

  10. express中路由设置的坑-----1

    router.get('/commodities/sortable', utils.logged, function (req, res) { Commodity.find({force_top:tr ...