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;
                }
            
        }
    }
     
  • 相关阅读:
    基于Dapper的开源Lambda扩展,且支持分库分表自动生成实体基础
    用SignalR和Layui搭建自己的web聊天网站
    MySQL查看、修改字符集及Collation
    ASP.NET MVC中有四种过滤器类型
    一个简单的大转盘抽奖程序(附.NetCore Demo源码)
    网站统计中的访问信息收集的前端实现
    微信小程序实现按首字母检索城市列表
    微信小程序异步处理
    微信小程序下拉框
    node.js上传文件
  • 原文地址:https://www.cnblogs.com/s115/p/4129605.html
Copyright © 2011-2022 走看看