zoukankan      html  css  js  c++  java
  • 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. }  

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

  • 相关阅读:
    Linux常用软件 分类: arm-linux-Ubuntu 2013-07-22 16:28 301人阅读 评论(0) 收藏
    Linux系统信息查看命令大全 分类: arm-linux-Ubuntu 2013-07-22 16:28 302人阅读 评论(0) 收藏
    解决VC6下调不出MSDN的问题! 分类: VC++ 2013-07-22 16:28 297人阅读 评论(0) 收藏
    Shell编程基础 分类: arm-linux-Ubuntu 2013-07-22 16:28 189人阅读 评论(0) 收藏
    linux内核体系结构 分类: arm-linux-Ubuntu 2013-07-22 16:28 359人阅读 评论(0) 收藏
    Linux分区和挂载(mount命令的学习) 分类: arm-linux-Ubuntu 2013-07-22 16:28 265人阅读 评论(0) 收藏
    重新编译Linux内核必要性及其准备工作 分类: arm-linux-Ubuntu 2013-07-22 16:28 288人阅读 评论(0) 收藏
    ubuntu11.04启动 及虚拟文件系统 分类: arm-linux-Ubuntu 2013-07-22 16:28 369人阅读 评论(0) 收藏
    VxWorks 任务 分类: vxWorks 2013-07-22 16:28 646人阅读 评论(0) 收藏
    vxWorks 命令 分类: vxWorks 2013-07-22 16:28 544人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/Percy_Lee/p/4827029.html
Copyright © 2011-2022 走看看