C# 实现QQ那样靠边停靠自动隐藏

    关于C#窗体如何实现QQ的靠边停靠的方法,有好多人问到,在这里我把ta写下,希望能帮到一些学弟学妹做UI的时候,想加一些效果的。当时我是这样写的,还有比我的代码质量更好方法,大家不嫌麻烦的话,感兴趣的可以去尽情的搜索,毕竟360搜索起来了0.0

    下面试代码,大家可以先看看,不懂的留言,或者你有更好的给我发个地址,我改正一下,谢谢大家!

    代码:

 



窗体中添加定时器控件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
            Timer checkDockTimer = new Timer();
            checkDockTimer.Tick += new EventHandler(Timer1_Tick);
            checkDockTimer.Interval = 100;
            checkDockTimer.Enabled = true;
        }
        internal AnchorStyles StopAanhor = AnchorStyles.None; 
        private void Timer1_Tick(object sender, EventArgs e)
        {

            if (this.Bounds.Contains(Cursor.Position))
            {
                switch (this.StopAanhor)
                {
                    case AnchorStyles.Top: this.Location = new Point(this.Location.X, 0);
                        break;
                    case AnchorStyles.Left: this.Location = new Point(0, this.Location.Y);
                        break;
                    case AnchorStyles.Right: this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, this.Location.Y);
                        break;
                }
            }
            else
            {
                switch (this.StopAanhor)
                {
                    case AnchorStyles.Top: this.Location = new Point(this.Location.X, (this.Height - 2) * (-1)); break;
                    case AnchorStyles.Left: this.Location = new Point((-1) * (this.Width - 2), this.Location.Y); break;
                    case AnchorStyles.Right: this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, this.Location.Y); break;
                }
            }
        }
        private void mStopAnhor() 
        { 
            if (this.Top <= 0) 
            { 
                StopAanhor = AnchorStyles.Top; 
            } 
            else if (this.Left <= 0) 
            { 
                StopAanhor = AnchorStyles.Left; 
            } 
            else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width) 
            { 
                StopAanhor = AnchorStyles.Right; 
            } 
            else 
            { 
                StopAanhor = AnchorStyles.None;
            } 
        }

        private void Form1_LocationChanged(object sender, EventArgs e)
        { 
            this.mStopAnhor(); 
        }
    }
}

新建类FormAutoDock:

public class FormAutoDock
    {
        public static void SideHideOrShow(Form DockableForm, ref int DockFormHeight, Timer _dockTimer)
        {
            if (DockableForm.WindowState != FormWindowState.Minimized)
            {
                _dockTimer.Interval = 1500;
                if (Cursor.Position.X > DockableForm.Left - 1 && Cursor.Position.X < DockableForm.Right && Cursor.Position.Y > DockableForm.Top - 1 && Cursor.Position.Y < DockableForm.Bottom)
                {
                    if (DockableForm.Top <= 0 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
                    {
                        DockableForm.Top = 0;
                    }
                    else if (DockableForm.Left <= 0)
                    {
                        DockableForm.Left = 0;
                    }
                    else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
                    {
                        DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width;
                    }
                    else
                    {
                        if (DockFormHeight > 0)
                        {
                            DockableForm.Height = DockFormHeight; DockFormHeight = 0;
                        }
                    }
                }
                else
                {
                    if (DockFormHeight < 1)
                    {
                        DockFormHeight = DockableForm.Height;
                    }
                    if (DockableForm.Top <= 4 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
                    {
                        DockableForm.Top = 3 - DockableForm.Height;
                        if (DockableForm.Left <= 4)
                        {
                            DockableForm.Left = -5;
                        }
                        else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
                        {
                            DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width + 5;
                        }
                    }
                    else if (DockableForm.Left <= 4)
                    {
                        DockableForm.Left = 3 - DockableForm.Width;
                    }
                    else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
                    {
                        DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - 3;
                    }
                    _dockTimer.Interval = 200;
                }
            }
        }
 }


    希望能帮到你0.0,写下来省得以后找不到忘记了,就 


你可能感兴趣的:(qq,C#,靠边停靠)