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,寫下來省得以後找不到忘記了,就 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章