C# 实现磁性窗体

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

 


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

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

原文地址:https://www.cnblogs.com/Percy_Lee/p/4827029.html