using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;namespace testComponent
{    
    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))
            {
                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;
            }
        }
    }
}就是到屏幕的上边时窗体会自动隐藏,随后鼠标再移过去时又会出来。思路!! 1,自动隐藏,窗体的location值,form的locationchanged事件。上隐藏就是判断location.x的值<10时产生事件,将location的y值不变,x值变为-form.height+10 这样就可以在屏幕上角留下10(根据情况改变,不一定是10,最好大于0)的可视区。左右两边隐藏以此类推! 2。鼠标过去自动出现,在form的鼠标事件mouseenter中将窗体的location的x指变为0~10就是了。左右两边的出现同理。 3。为了看起来有渐变的效果,可以设置一个timer来递增x的值。如果使用频繁可以考虑写成一个组件这样以后的项目如果需要这种效果只需像引用Timer一样拖拽到窗体上,然后设置DockForm为需要效果的窗体就可以了。

解决方案 »

  1.   

    这个东西我试了,Location的值,设置不了负的,所以隐藏不了,想其它办法吧!
      

  2.   

    CSDN上的牛人呢??没人答了??
    如何实现像QQ一样,拖到屏幕边缘窗体就自动隐藏了呢?鼠标挪过去就又出现了
      

  3.   

    我试过了 很正常 
    只发现一个问题
    就是窗体的dockSide 从Left或者Right 直接移动到top状态 窗体的高度不调整回原大小 而是使用了最大高度