C# 实现磁性窗口(附源码和程序)

实现并封装了磁性窗口类MagneticMagnager,实现磁性窗口仅仅需要调用一行代码:
MagneticMagnager test2 = new MagneticMagnager(this, fm2, MagneticPosition.Top);

插图:

具体操作:

1.新建winform项目MagneticForm,并添加磁性窗口操作类MagneticMagnager:

  1. using System;  
  2. using System.Drawing;  
  3.   
  4. namespace System.Windows.Forms  
  5. {  
  6.     public class MagneticMagnager  
  7.     {  
  8.         MagneticPosition Pos;//位置属性  
  9.         Form MainForm, ChildForm;  
  10.         bool IsFirstPos;//是否第一次定位ChildForm子窗体  
  11.         public int step;//磁性子窗体ChildForm移动步长  
  12.         public Point LocationPt;//定位点  
  13.         delegate void LocationDel();//移动子窗体的委托  
  14.         public MagneticMagnager(Form _MainForm, Form _ChildForm, MagneticPosition _pos)  
  15.         {  
  16.             IsFirstPos = false;  
  17.             step = 20;  
  18.             MainForm = _MainForm;  
  19.             ChildForm = _ChildForm;  
  20.             Pos = _pos;  
  21.             MainForm.LocationChanged += new EventHandler(MainForm_LocationChanged);  
  22.             ChildForm.LocationChanged += new EventHandler(ChildForm_LocationChanged);  
  23.             MainForm.SizeChanged += new EventHandler(MainForm_SizeChanged);  
  24.             ChildForm.SizeChanged += new EventHandler(ChildForm_SizeChanged);  
  25.             ChildForm.Load+=new EventHandler(ChildForm_Load);  
  26.             MainForm.Load+=new EventHandler(MainForm_Load);  
  27.         }  
  28.         void ChildForm_Load(object sender, EventArgs e)  
  29.         {  
  30.             GetLocation();  
  31.         }  
  32.         void MainForm_Load(object sender, EventArgs e)  
  33.         {  
  34.             GetLocation();  
  35.         }  
  36.         void MainForm_LocationChanged(object sender, EventArgs e)  
  37.         {  
  38.             GetLocation();  
  39.         }  
  40.         void MainForm_SizeChanged(object sender, EventArgs e)  
  41.         {  
  42.             GetLocation();  
  43.         }  
  44.         void ChildForm_SizeChanged(object sender, EventArgs e)  
  45.         {  
  46.             GetLocation();  
  47.         }  
  48.         void GetLocation()//定位子窗体  
  49.         {  
  50.             if (ChildForm == null)  
  51.                 return;  
  52.             if (Pos == MagneticPosition.Left)  
  53.                 LocationPt = new Point(MainForm.Left - ChildForm.Width, MainForm.Top);  
  54.             else if (Pos == MagneticPosition.Top)  
  55.                 LocationPt = new Point(MainForm.Left, MainForm.Top - ChildForm.Height);  
  56.             else if (Pos == MagneticPosition.Right)  
  57.                 LocationPt = new Point(MainForm.Right, MainForm.Top);  
  58.             else if (Pos == MagneticPosition.Bottom)  
  59.                 LocationPt = new Point(MainForm.Left, MainForm.Bottom);  
  60.             ChildForm.Location = LocationPt;  
  61.         }  
  62.         void ChildForm_LocationChanged(object sender, EventArgs e)//当窗体位置移动后  
  63.         {  
  64.             if (!IsFirstPos)  
  65.             {  
  66.                 IsFirstPos = true;  
  67.                 return;  
  68.             }  
  69.             LocationDel del = new LocationDel(OnMove);//委托  
  70.             MainForm.BeginInvoke(del);//调用  
  71.         }  
  72.   
  73.         void OnMove()//移动子窗体  
  74.         {  
  75.             if (ChildForm.Left > LocationPt.X)  
  76.                 if (ChildForm.Left - LocationPt.X > step)  
  77.                     ChildForm.Left -= step;  
  78.                 else  
  79.                     ChildForm.Left = LocationPt.X;  
  80.             else if (ChildForm.Left < LocationPt.X)  
  81.                 if (ChildForm.Left - LocationPt.X < -step)  
  82.                     ChildForm.Left += step;  
  83.                 else  
  84.                     ChildForm.Left = LocationPt.X;  
  85.             if (ChildForm.Top > LocationPt.Y)  
  86.                 if (ChildForm.Top - LocationPt.Y > step)  
  87.                     ChildForm.Top -= step;  
  88.                 else  
  89.                     ChildForm.Top = LocationPt.Y;  
  90.   
  91.             else if (ChildForm.Top < LocationPt.Y)  
  92.                 if (ChildForm.Top - LocationPt.Y < -step)  
  93.                     ChildForm.Top += step;  
  94.                 else  
  95.                     ChildForm.Top = LocationPt.Y;  
  96.         }  
  97.     }  
  98.     public enum MagneticPosition//磁性窗体的位置属性  
  99.     {  
  100.         Left = 0,  
  101.         Top = 1,  
  102.         Right = 2,  
  103.         Bottom = 3  
  104.     }  
  105. }  

 

2.添加MainForm主窗体,打开Program.cs文件,修改为如下 :

[c-sharp]  view plain copy
  1. static void Main()  
  2. {  
  3.     Application.EnableVisualStyles();  
  4.     Application.SetCompatibleTextRenderingDefault(false);  
  5.     Application.Run(new MainForm());  
  6. }  

3.添加ChildForm子窗体,不用写任何代码,当然实际中根据你自己的设计决定

4.在MainForm中添加四个button,分别设置text属性为:"左磁性" "上磁性" "右磁性" 下磁性" ,并分别添加按钮点击事件,具体代码如下:

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class MainForm : Form  
  13.     {  
  14.         public MainForm()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.         ChildForm fm1, fm2, fm3, fm4;  
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             //左磁性  
  22.             if (fm1 == null)  
  23.             {  
  24.                 fm1 = new ChildForm();  
  25.                 MagneticMagnager test1 = new MagneticMagnager(this, fm1, MagneticPosition.Left);  
  26.                 fm1.Show();  
  27.             }  
  28.             else  
  29.             {  
  30.                 fm1.Close();  
  31.                 fm1 = null;  
  32.             }  
  33.         }  
  34.   
  35.         private void button4_Click(object sender, EventArgs e)  
  36.         {  
  37.             //上磁性  
  38.             if (fm2 == null)  
  39.             {  
  40.                 fm2 = new ChildForm();  
  41.                 MagneticMagnager test2 = new MagneticMagnager(this, fm2, MagneticPosition.Top);  
  42.                 fm2.Show();  
  43.             }  
  44.             else  
  45.             {  
  46.                 fm2.Close();  
  47.                 fm2 = null;  
  48.             }  
  49.         }  
  50.   
  51.         private void button3_Click(object sender, EventArgs e)  
  52.         {  
  53.             //右磁性  
  54.             if (fm3 == null)  
  55.             {  
  56.                 fm3 = new ChildForm();  
  57.                 MagneticMagnager test3 = new MagneticMagnager(this, fm3, MagneticPosition.Right);  
  58.                 fm3.Show();  
  59.             }  
  60.             else  
  61.             {  
  62.                 fm3.Close();  
  63.                 fm3 = null;  
  64.             }  
  65.         }  
  66.   
  67.         private void button2_Click(object sender, EventArgs e)  
  68.         {  
  69.             //下磁性  
  70.             if (fm4 == null)  
  71.             {  
  72.                 fm4 = new ChildForm();  
  73.                 MagneticMagnager test4 = new MagneticMagnager(this, fm4, MagneticPosition.Bottom);  
  74.                 fm4.Show();  
  75.             }  
  76.             else  
  77.             {  
  78.                 fm4.Close();  
  79.                 fm4 = null;  
  80.             }  
  81.         }  
  82.     }  
  83. }  

源码下载: C# 实现磁性窗口(附源码和程序)

转自:http://blog.csdn.net/mngzilin/article/details/5549119

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值