zoukankan      html  css  js  c++  java
  • 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;
                }
            
        }
    }
     
  • 相关阅读:
    现在不知道为什么安装pip包总是失败,只能用清华源
    linux 下 svn配置;以及多仓库配置
    谷歌浏览器安装json格式化插件
    RESTful API的理解
    mysql5.6 rpm安装配置
    linux,apache,php,mysql常用的查看版本信息的方法
    mysql允许别人通过ip访问本机mysql数据
    直接取PHP二维数组里面的值
    mysql优化
    self this
  • 原文地址:https://www.cnblogs.com/s115/p/4129605.html
Copyright © 2011-2022 走看看