码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm窗体屏幕边缘停靠

时间:2014-11-29 00:06:58      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   io   ar   color   os   sp   

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WZDH
{
    public partial class Form1 : Form
    {
        internal AnchorStyles StopAanhor = AnchorStyles.None;
        public Form1()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 初始化窗体信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //设置窗体左边距离主显示器位置
            this.Width = 200;
            this.Left = Screen.PrimaryScreen.Bounds.Width - this.Width;
            //设置窗体顶边距离主显示器位置
            this.Top = 0;          
            //设置窗体宽度为主显示器宽度(不包括任务栏)
            this.Height = Screen.PrimaryScreen.WorkingArea.Height;
        }
 
        /// <summary>
        /// 淡入加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity += 0.1;
            if (this.Opacity >= 1)
                this.timer1.Enabled = false;
        }
 
        /// <summary>
        /// 退出程序菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        /// <summary>
        /// 当窗体的位置改变时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_LocationChanged(object sender, EventArgs e)
        {
            mStopAnhor();
        }
 
        private void timer2_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.Bottom:
                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - this.Height);
                        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 - 3) * (-1));
                        break;
                    case AnchorStyles.Bottom:
                        this.Location = new Point(this.Location.X, Screen.PrimaryScreen.Bounds.Height - 5);
                        break;
                    case AnchorStyles.Left:
                        this.Location = new Point((-1) * (this.Width - 3), 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;
            }
        
    }
}
 

WinForm窗体屏幕边缘停靠

标签:winform   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/s115/p/4129605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!