• WinForm窗体屏幕边缘停靠


    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;
                }
            
        }
    }
     
  • 相关阅读:
    C++经典书籍:游戏编程
    云计算学习笔记Hadoop简介,hadoop实现原理,NoSQL介绍...与传统关系型数据库对应关系,云计算面临的挑战
    A win for the Nokia N8 is a win for Qt
    Qt 为中国移动音乐客户端提供多平台支持
    诺基亚力邀App开发员加入Ovi以对抗苹果
    MeeGo手机或将跳票至2011年
    TinyXML:一个优秀的C++ XML解析器
    企业开发中Qt和.Net小谈
    Qt 的昨天,今天,明天
    学机械的看看吧,一般看不见的机械原理——全动画图解
  • 原文地址:https://www.cnblogs.com/s115/p/4129605.html
Copyright © 2020-2023  润新知