c#仿QQ顶置自动隐藏的效果

来源:互联网 发布:网络直播概念股 编辑:程序博客网 时间:2024/04/30 01:09

不用说,大家都用过QQ,QQ有个顶置之后自动隐藏的效果。

本人写WinForm的时候需要这样的特效,上网查了一番,然后自己动手实现了。现共享之。

并将此段代码封装成dll文件,在其他项目中用的时候直接引入这个dll就行了。不用每次都写一遍了。

代码如下:

using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Windows.Forms;namespace ClassLibrary1{    /// <summary>    /// 吸附窗体    /// </summary>    public class Class1    {        private Form frm1;        private Timer timer1;        /// <summary>        /// 吸附窗体        /// </summary>        /// <param name="frm">要吸附的窗体</param>        /// <param name="timer">计时器对象</param>        public Class1(Form frm, Timer timer)        {            this.frm1 = frm;            this.timer1 = timer;            this.frm1.LocationChanged += new EventHandler(frm1_LocationChanged);            this.frm1.TopMost = true;            this.timer1.Enabled = true;            this.timer1.Interval = 100;            this.timer1.Tick += new EventHandler(timer1_Tick);            this.timer1.Start();        }        internal AnchorStyles stopAnchor = AnchorStyles.None;        /// <summary>        /// 计时器控制函数        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        public void timer1_Tick(object sender, EventArgs e)        {            if (frm1.Bounds.Contains(Cursor.Position))            {                switch (this.stopAnchor)                {                    case AnchorStyles.Top:                        frm1.Location = new Point(frm1.Location.X, 0);                        break;                    case AnchorStyles.Left:                        frm1.Location = new Point(0, frm1.Location.Y);                        break;                    case AnchorStyles.Right:                        frm1.Location = new Point(Screen.PrimaryScreen.Bounds.Width - frm1.Width, frm1.Location.Y);                        break;                    case AnchorStyles.Bottom:                        frm1.Location = new Point(frm1.Location.X, Screen.PrimaryScreen.Bounds.Height - frm1.Height);                        break;                }            }            else            {                switch (this.stopAnchor)                {                    case AnchorStyles.Top:                        frm1.Location = new Point(frm1.Location.X, (frm1.Height - 2) * (-1));                        break;                    case AnchorStyles.Left:                        frm1.Location = new Point((-1) * (frm1.Width - 2), frm1.Location.Y);                        break;                    case AnchorStyles.Right:                        frm1.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, frm1.Location.Y);                        break;                    case AnchorStyles.Bottom:                        frm1.Location = new Point(frm1.Location.X, (Screen.PrimaryScreen.Bounds.Height - 2));                        break;                }            }        }              private void mStopAnchor()        {            if (frm1.Top <= 0 && frm1.Left <= 0)            {                stopAnchor = AnchorStyles.None;            }            else if (frm1.Top <= 0)            {                stopAnchor = AnchorStyles.Top;            }            else if (frm1.Left <= 0)            {                stopAnchor = AnchorStyles.Left;            }            else if (frm1.Left >= Screen.PrimaryScreen.Bounds.Width - frm1.Width)            {                stopAnchor = AnchorStyles.Right;            }            else if (frm1.Top >= Screen.PrimaryScreen.Bounds.Height - frm1.Height)            {                stopAnchor = AnchorStyles.Bottom;            }            else            {                stopAnchor = AnchorStyles.None;            }        }        /// <summary>        /// 窗体移动触发函数        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void frm1_LocationChanged(object sender, EventArgs e)        {            this.mStopAnchor();        }    }}


下面来看在其他项目中调用此dll的方法。

代码很简单,只有一句话。

ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1(this, timer1);

前提是你必须有timer1这个对象。


如果想要这个dll的话,点击下面连接进行下载就OK了!

http://download.csdn.net/detail/crazy1235/4966939

原创粉丝点击