马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Winform中实现窗体停靠隐藏-模拟QQ

[复制链接]
查看5526 | 回复2 | 2012-12-14 14:39:33 | 显示全部楼层 |阅读模式
QQ大家都用过,当你把QQ当窗体放在屏幕靠边的时候,窗体能自动隐藏
当你鼠标在放上去以后,他会自动的出现
那么在C#中 ,我们如何实现这个效果呢?
附上类一个!

[C#] 纯文本查看 复制代码
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;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //this.StartPosition = FormStartPosition.Manual;
            //this.Location = new Point(0, 0);

        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
                 this.TopMost = true;
 
            this.timer1.Start();
            //int a  = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            //int b = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            //MessageBox.Show(a,b);
        }
        internal AnchorStyles StopAanhor = AnchorStyles.None;
        private void mStopAnhor()
 
        {
 
            if (this.Top <= 0 && this.Left <= 0)
 
            {
 
                StopAanhor = AnchorStyles.None;
 
            }
 
            else 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 if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height)
 
            {
 
                StopAanhor = AnchorStyles.Bottom;
 
            }
 
            else
 
            {
 
                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;
 
               case AnchorStyles.Bottom:
 
                    this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
 
                    break;
 
            }
 
       }
 
       else
 
       {
 
            switch (this.StopAanhor)
 
            {
 
                 case AnchorStyles.Top:
 
                     this.Location = new Point(this.Location.X, (this.Height - 4) * (-1));
 
                     break;
 
                 case AnchorStyles.Left:
 
                    this.Location = new Point((-1) * (this.Width - 4), this.Location.Y);
 
                    break;
 
                 case AnchorStyles.Right:
 
                    this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 4, this.Location.Y);
 
                    break;
 
                 case AnchorStyles.Bottom:
 
                    this.Location = new Point(this.Location.X, (Screen.PrimaryScreen.Bounds.Height - 4));
 
                    break;
 
                }
 
            }
 
        }

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

        
        
       
      

    }
}


如果有问题,可以回复帖子。

相关帖子

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
chao2332601 | 2013-6-16 02:08:26 | 显示全部楼层
谢谢分享!!!
chao2332601 | 2013-6-16 04:54:54 | 显示全部楼层
谢谢分享!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则