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;
                }
            }
        }
 }


    









本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1209084,如需转载请自行联系原作者
目录
相关文章
|
5月前
|
Android开发
在安卓手机上,软键盘弹起来之后,文本框被遮挡
在安卓手机上,软键盘弹起来之后,文本框被遮挡
31 1
|
9月前
|
存储 C++ Python
C++复刻:[流光按钮]+[悬浮波纹按钮]
[流光按钮]+[悬浮波纹按钮]
111 0
|
12月前
7-83 九宫格输入法
7-83 九宫格输入法
86 0
|
Android开发 UED
完美解决android软键盘挡住输入框方法,还不顶标题栏
完美解决android软键盘挡住输入框方法,还不顶标题栏
1153 0
完美解决android软键盘挡住输入框方法,还不顶标题栏
|
Windows
Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?(上)
Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?(上)
559 0
Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?(上)
|
Windows
Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?(下)
Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?(下)
283 0
Win系统 - 全屏看视频时任务栏没有自动隐藏怎么办?(下)
|
C# Windows
C# WPF QQ新消息托盘悬浮窗效果实现
原文:C# WPF QQ新消息托盘悬浮窗效果实现 今天在做一个项目的时候需要这么一个效果,但是网上找了一会发现并没有现成的给我参考(复制),但是呢,我千(到)辛(处)万(抄)苦(袭)想(复)破(制)头(粘)脑(贴)终于还是给做出来了~嘿嘿嘿 QQ新消息悬浮窗即:QQ有新消息时托盘图标会闪动,此时移动鼠标到托盘图标上就会显示一个弹框了,那么呢我把这个弹框称为“QQ新消息托盘悬浮窗”。
2559 0
|
JavaScript Web App开发 前端开发