C# 实现磁性窗体
- using System;
- using System.Drawing;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace TinyBook
- {
- public enum MagneticLocation
- {
- Left = 0,
- Right = 1,
- Top = 2,
- Bottom = 3
- }
- public enum MagneticState
- {
- Adsorbent, // 吸附
- Separation // 分离
- }
- public class MagneticManager
- {
- public class ChildFormInfo
- {
- public Form Child { get; set; }
- public MagneticLocation Location { get; set; }
- public MagneticState State { get; set; }
- public bool CutstomSetLocation { get; set; }
- }
- public int Step { get; set; }
- private Form m_mainForm = null;
- private List<ChildFormInfo> m_childs= new List<ChildFormInfo>();
- public MagneticManager(Form form)
- {
- m_mainForm = form;
- form.LocationChanged += MainForm_LocationChanged;
- form.SizeChanged += MainForm_SizeChanged;
- form.FormClosed += MainForm_FormClosed;
- Step = 20;
- }
- public void addChild(Form childForm, MagneticLocation loc)
- {
- foreach (ChildFormInfo info in m_childs)
- {
- if (info.Child == childForm)
- {
- return;
- }
- }
- ChildFormInfo childInfo = new ChildFormInfo();
- childInfo.Child = childForm;
- childInfo.Location = loc;
- childInfo.State = MagneticState.Adsorbent;
- childInfo.CutstomSetLocation = false;
- childForm.LocationChanged += ChildForm_LocationChanged;
- childForm.SizeChanged += ChildForm_SizeChanged;
- childForm.FormClosed += ChildForm_FormClosed;
- m_childs.Add(childInfo);
- adsorbentChild(childInfo);
- }
- private ChildFormInfo getInfo(Form form)
- {
- if (form == null)
- {
- return null;
- }
- foreach (ChildFormInfo info in m_childs)
- {
- if (info.Child == form)
- {
- return info;
- }
- }
- return null;
- }
- private Point getLocation(ChildFormInfo info)
- {
- Point pos = Point.Empty;
- switch (info.Location)
- {
- case MagneticLocation.Left:
- pos = new Point(m_mainForm.Left - info.Child.Width + 14, m_mainForm.Top);
- break;
- case MagneticLocation.Right:
- pos = new Point(m_mainForm.Right - 14, m_mainForm.Top);
- break;
- case MagneticLocation.Top:
- pos = new Point(m_mainForm.Left, m_mainForm.Top - info.Child.Height);
- break;
- case MagneticLocation.Bottom:
- pos = new Point(m_mainForm.Left, m_mainForm.Bottom);
- break;
- default:
- break;
- }
- return pos;
- }
- private void setChildLocation(ChildFormInfo info, Point location)
- {
- if (info.Child == null)
- {
- return;
- }
- info.CutstomSetLocation = true;
- info.Child.Location = location;
- info.CutstomSetLocation = false;
- }
- private void setChildLocation(ChildFormInfo info, int x, int y)
- {
- setChildLocation(info, new Point(x, y));
- }
- private void resetChildLocation(ChildFormInfo info)
- {
- if (info.Child == null)
- {
- return;
- }
- Point pos = getLocation(info);
- setChildLocation(info, pos);
- }
- private void adsorbentChild(ChildFormInfo info)
- {
- info.State = MagneticState.Adsorbent;
- resetChildLocation(info);
- }
- private void separationChild(ChildFormInfo info)
- {
- info.State = MagneticState.Separation;
- }
- private void MainForm_LocationChanged(object sender, EventArgs e)
- {
- foreach (ChildFormInfo info in m_childs)
- {
- if (info.State == MagneticState.Adsorbent)
- {
- resetChildLocation(info);
- }
- }
- }
- private void MainForm_SizeChanged(object sender, EventArgs e)
- {
- foreach (ChildFormInfo info in m_childs)
- {
- if (info.State == MagneticState.Adsorbent)
- {
- resetChildLocation(info);
- }
- }
- }
- private void MainForm_FormClosed(object sender, EventArgs e)
- {
- }
- private void ChildForm_LocationChanged(object sender, EventArgs e)
- {
- ChildFormInfo info = getInfo(sender as Form);
- if (info == null)
- {
- return;
- }
- if (info.CutstomSetLocation == true)
- {
- return;
- }
- Point location = getLocation(info);
- if (info.Child.Left > location.X && info.Location == MagneticLocation.Right)
- {
- if (info.Child.Left - location.X > Step)
- {
- separationChild(info);
- }
- else
- {
- adsorbentChild(info);
- }
- }
- else if (info.Child.Left < location.X && info.Location == MagneticLocation.Left)
- {
- if (info.Child.Left - location.X < -Step)
- {
- separationChild(info);
- }
- else
- {
- adsorbentChild(info);
- }
- }
- if (info.Child.Top > location.Y && info.Location == MagneticLocation.Bottom)
- {
- if (info.Child.Top - location.Y > Step)
- {
- separationChild(info);
- }
- else
- {
- adsorbentChild(info);
- }
- }
- else if (info.Child.Top < location.Y && info.Location == MagneticLocation.Top)
- {
- if (info.Child.Top - location.Y < -Step)
- {
- separationChild(info);
- }
- else
- {
- adsorbentChild(info);
- }
- }
- }
- private void ChildForm_SizeChanged(object sender, EventArgs e)
- {
- ChildFormInfo info = getInfo(sender as Form);
- if (info != null && info.State == MagneticState.Adsorbent)
- {
- resetChildLocation(info);
- }
- }
- private void ChildForm_FormClosed(object sender, EventArgs e)
- {
- ChildFormInfo info = getInfo(sender as Form);
- if (info != null)
- {
- m_childs.Remove(info);
- }
- }
- }
- }
版权声明:本文为博主原创文章,欢迎转载,转载请注明出处。
C# 实现磁性窗体的更多相关文章
- 磁性窗体设计C#(二)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- C#磁性窗体设计
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- c#轻松实现磁性窗口
/// <summary>/// 磁性窗体函数/// </summary>/// <param name="form">窗体控件(一般传this ...
- MP3文件ID3信息编辑器代码开源 - 开源研究系列文章
上次把磁性窗体的源码开源了,这次就开源另一个程序源码:MP3文件ID3信息编辑器.这个源码也比较简单,关键在于获取和写入MP3文件的这个ID3的信息即可. 这个操作信息编辑的就封装在MP3ID3.ba ...
- 198个经典C#WinForm实例源码(超赞) 里面的例子 .sln 目录
\-窗体技巧\QQ窗体\QQFrm.sln; \-窗体技巧\仿XP系统的任务栏菜单\仿XP系统的任务栏菜单.sln; \-窗体技巧\向窗体中拖放图片并显示\向窗体中拖放图片并显示.sln; \-窗体技 ...
- 《Visual Basic开发实战1200例》包括第I卷、第II卷共计1200个例子,本书是第I卷,共计600个例子。
本书以开发人员在项目开发中经常遇到的问题和必须掌握的技术为中心,介绍了应用Visual Basic进行程序开发各个方面的知识和技巧.主要包括基础知识.窗体界面设计.控件应用等.全书分6篇20章,共计6 ...
- JavaScript动画-磁性吸附
▓▓▓▓▓▓ 大致介绍 磁性吸附是以模拟拖拽为基础添加一个拖拽时范围的限定而来的一个效果,如果对模拟拖拽有疑问的同学请移步模拟拖拽. 源代码.效果:点这里 ▓▓▓▓▓▓ 范围限定(可视区) 先来看一个 ...
- javascript动画系列第二篇——磁性吸附
× 目录 [1]范围限定 [2]拖拽范围 [3]磁性吸附 前面的话 上一篇,我们介绍了元素拖拽的实现.但在实际应用中,常常需要为拖拽的元素限定范围.而通过限定范围,再增加一些辅助的措施,就可以实现磁性 ...
- VB.NET设置控件和窗体的显示级别
前言:在用VB.NET开发射频检测系统ADS时,当激活已存在的目标MDI子窗体时,被其他子窗体遮住了,导致目标MDI子窗体不能显示. 这个问题怎么解决呢?网上看到一篇帖子VB.NET设置控件和窗体的显 ...
随机推荐
- HTML5全屏API
现在大多数浏览器都有全屏功能,允许用户来设置或操作.但HTML5的全屏API与之不同,HTML5的全屏API允许web开发工程师在程序中调用. 这样,web开发工程师就可以再网站中设计一个按钮,当该按 ...
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
index.jsp: <body> <s:fielderror /> <form action="${pageContext.request.contextPa ...
- D. DZY Loves Modification
D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 9款极具创意的HTML5/CSS3进度条动画(免积分下载)
尊重原创,原文地址:http://www.cnblogs.com/html5tricks/p/3622918.html 免积分打包下载地址:http://download.csdn.net/detai ...
- windows 环境怎样恢复 (oracle 11g grid) ocr voting 损坏的集群
windows 环境怎样恢复 (oracle 11g grid) ocr voting 损坏的集群 oracle 11g 以后 ocr 能够放到 asm 磁盘上,而ASM的启动依赖于ocr和vo ...
- bootstrapvalidator之API学习
最近项目用到了bootstrap框架,其中前端用的校验,采用的是bootstrapvalidator插件,也是非常强大的一款插件.我这里用的是0.5.2版本.下面记录一下使用中学习到的相关API,不定 ...
- utf8+bom格式保存php curl乱码问题
今天开发遇到一个php curl取数据乱码问题 不是gzip也不是编码设置问题 最后有一同事判断为utf8+bom保存数据原因,懒得深入了解utf8+bom,仅做记录 [root@centos5 ~] ...
- 20151225--easyUI
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Devpexpress 打印预览问题
devexpress 12 之前报表打印: XtraReports rp1 = new XtraReports(); rp1.ShowPreview(): 即可预览报表: devexpress 13 ...
- QF——对不同尺寸屏幕的适配(自动布局:AutoLayout)
对不同尺寸设备UI的适配: 很多时候,我们的App可能运行在不同尺寸的设备上,或者横竖屏时,呈现方法应该也不一样.这样便要求UI里各控件的位置和大小不能写死. 对于不同尺寸UI的适配,一般有三种对策: ...