迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
靠边隐藏效果:当程序移动到显示的边界时,程序自动隐藏起来,留一条小边在外面,很酷的效果吧. 建一个自定义控件,然后记得建一个Timer,并设置Tick事件
using System;
using System.ComponentModel; using System.Diagnostics; using System.Text; using System.Windows.Forms; using System.Drawing; namespace QQClient.QQ.QQBuddy.Components { [Description("窗体自动靠边隐藏组件 @Author: Red_angelX")] public partial class AutoDockManage : Component { private Form _form; public AutoDockManage() { InitializeComponent(); } public AutoDockManage(IContainer container) { container.Add(this); InitializeComponent(); } [Description("用于控制要自动Dock的窗体")] public Form DockForm { get { return _form; } set { _form = value; if (_form != null) { _form.LocationChanged += new EventHandler(_form_LocationChanged); _form.SizeChanged += new EventHandler(_form_SizeChanged); _form.TopMost = true; } } } private bool IsOrg = false; private Rectangle lastBoard; private const int DOCKING = 0; private const int PRE_DOCKING = 1; private const int OFF = 2; private int status = 2; private void CheckPosTimer_Tick(object sender, EventArgs e) { if (DesignMode) { return; } if (_form == null || IsOrg == false ) { return; } if (_form.Bounds.Contains(Cursor.Position)) { /* * 该死的.Net在移动时候不会发生该代码,必须在鼠标离开后才会执行 if (dockSide == AnchorStyles.None && status == OFF && IsOrg == true) { if (_form.Bounds.Width == lastBoard.Width && _form.Bounds.Height == lastBoard.Height) { return; } _form.Size = new Size(lastBoard.Width, lastBoard.Height); return; } */ switch (dockSide) { case AnchorStyles.Top: if (status == DOCKING) _form.Location = new Point(_form.Location.X, 0); break; case AnchorStyles.Right: if (status == DOCKING) _form.Location = new Point(Screen.PrimaryScreen.Bounds.Width - _form.Width, 1); break; case AnchorStyles.Left: if (status == DOCKING) _form.Location = new Point(0, 1); break; } } else { switch (dockSide) { case AnchorStyles.Top: _form.Location = new Point(_form.Location.X, (_form.Height - 4) * (-1)); break; case AnchorStyles.Right: _form.Size = new Size(_form.Width, Screen.PrimaryScreen.WorkingArea.Height); _form.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 4, 1); break; case AnchorStyles.Left: _form.Size = new Size(_form.Width, Screen.PrimaryScreen.WorkingArea.Height); _form.Location = new Point((-1) * (_form.Width - 4), 1); break; case AnchorStyles.None: if (IsOrg == true && status == OFF) { if (_form.Bounds.Width != lastBoard.Width || _form.Bounds.Height != lastBoard.Height) { _form.Size = new Size(lastBoard.Width, lastBoard.Height); } } break; } } } internal AnchorStyles dockSide = AnchorStyles.None; private void GetDockSide() { if (_form.Top <= 0) { dockSide = AnchorStyles.Top; if (_form.Bounds.Contains(Cursor.Position)) status = PRE_DOCKING; else status = DOCKING; } else if (_form.Left <= 0) { dockSide = AnchorStyles.Left; if (_form.Bounds.Contains(Cursor.Position)) status = PRE_DOCKING; else status = DOCKING; } else if (_form.Left >= Screen.PrimaryScreen.Bounds.Width - _form.Width) { dockSide = AnchorStyles.Right; if (_form.Bounds.Contains(Cursor.Position)) status = PRE_DOCKING; else status = DOCKING; } else { dockSide = AnchorStyles.None; status = OFF; } } private void _form_LocationChanged(object sender, EventArgs e) { GetDockSide(); if (IsOrg == false) { lastBoard = _form.Bounds; IsOrg = true; } } private void _form_SizeChanged(object sender, EventArgs e) { if (IsOrg == true && status == OFF) { lastBoard = _form.Bounds; } } } } 转载请写上出处就行,代码可以随意改动,但要保持版权完整性,现在已知还有个小问题, .Net机制中的窗体大小改变事件必须在鼠标离开窗体后才能执行,所以想要完美的隐藏效果,最好是自己画个标题栏 不过总体来说还算另我满意,VS 2005 编译没问题,2003似乎命名空间不能和类名相同?可能需要改少许代码 |
请发表评论