博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#:控制WinForm界面的显示
阅读量:6166 次
发布时间:2019-06-21

本文共 8467 字,大约阅读时间需要 28 分钟。

控制WinForm界面在屏幕的四个角落显示,具体代码中有说明:

using System;using System.Collections.Generic;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Tooltip{    ///     /// 弹出自方向   右上、右下、左上、左下    ///     internal enum PopUp    {        LeftUp,     //左上        LeftDown,   //左下        RightUp,    //右上        RightDown   //右下    }    ///     /// 显示窗体    ///     public class ShowForm    {        #region 字段 属性         private int screenWidth;    //除任务栏外的屏幕的宽        private int screenHeight;   //除任务栏外的屏幕的高        private Timer timer = new Timer();  //计时器        private Form _form;         ///         /// 显示的窗体        ///         public Form TheForm        {            set             {                this._form = value;                SetLocation();            }            get { return this._form; }        }        private System.Drawing.Point locationPoint;        ///         /// 窗体显示位置        ///         public System.Drawing.Point LocationPoint        {            set            {                this.locationPoint = value;                LimitShowArea();            }            get { return this.locationPoint; }        }        private int time;        ///         /// 设置弹出窗体过程的整个时间        ///         public int Time        {            set { this.time = value; }            get { return this.time; }        }        #endregion        #region 构造函数        ///         /// 构造函数        ///         public ShowForm()        {            //this._form = form;            screenWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;            screenHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;            this.time = 3;            //this.timer.Interval = 100;            this.timer.Tick += new EventHandler(timer_Tick);            this.timer.Enabled = false;        }        #endregion        #region 动态化窗体 开始 结束            #region 窗体动态化参数                private int x_distance;     //运动时在X轴上的距离                private int y_distance;     //运动时在Y轴上的距离                private int tickCount;      //总运动次数                private int x_step;         //运动时在Y轴上的步长                private int y_step;         //运动时在Y轴上的步长                private int num = 0;        //运动到第几次                       private Point reachPoint = new Point();     //运动到的具体位置            #endregion                ///         /// 设置窗体动态化参数        ///         private void SetFormDynamicParms()        {            x_distance = this.locationPoint.X - this.screenWidth;            y_distance = this.locationPoint.Y - this.screenHeight;            tickCount = this.time * 1000 / this.timer.Interval;            PopUp popUpDirection = this.JudgeDirection();            switch (popUpDirection)            {                 case PopUp.LeftUp:                    x_distance = this.locationPoint.X + this._form.Width;       //x_distance = this.locationPoint.X;                    y_distance = this.locationPoint.Y + this._form.Height;      //y_distance = this.locationPoint.Y                     break;                case PopUp.LeftDown:                    x_distance = this.locationPoint.X + this._form.Width;       //x_distance = this.locationPoint.X;                    y_distance = this.locationPoint.Y - this.screenHeight;                          break;                case PopUp.RightUp:                    x_distance = this.locationPoint.X - this.screenWidth;                       y_distance = this.locationPoint.Y + this._form.Height;      //y_distance = this.locationPoint.Y;                    break;                default:                    break;            }            x_step = x_distance / tickCount;            y_step = y_distance / tickCount;        }        ///         /// 计时器间隔执行函数        ///         ///         ///         private void timer_Tick(object sender, EventArgs e)        {            if (this.num == 0)            {                SetFormDynamicParms();                num++;            }            else            {                this.reachPoint.X = this.locationPoint.X - x_distance + x_step * num;                this.reachPoint.Y = this.locationPoint.Y - y_distance + y_step * num;                if (this.num < this.tickCount)                {                    this._form.Location = this.reachPoint;                    this._form.Opacity = this.num * 100 / this.tickCount;                    this.num++;                }                else                {                    this._form.Location = this.locationPoint;                    this._form.Opacity = 100;                    this.num = 0;                    this.timer.Stop();                }            }        }        ///         /// 开始显示动态窗体        ///         public void Start()        {            this.timer.Enabled = true;            this.timer.Start();            this._form.Opacity = 0;            this._form.Show();        }        ///         /// 关闭显示的窗体        ///         public void Close()        {            this.timer.Stop();            this.timer.Enabled = false;            this._form.Close();        }        #endregion        #region 默认显示位置        private void SetLocation()        {            Point lct = new Point();            lct.X = screenWidth - this.TheForm.Width;            lct.Y = screenHeight - this.TheForm.Height;            this.LocationPoint = lct;        }        #endregion        #region 限制弹框的显示区域        private void LimitShowArea()        {            if (this.locationPoint.X < 0 )            {                this.locationPoint.X = 0;            }            if (this.locationPoint.Y < 0)            {                this.locationPoint.Y = 0;            }            int maxX = this.screenWidth - this._form.Width;   //Form 不溢出 X轴最大值            if (this.locationPoint.X > maxX)            {                this.locationPoint.X = maxX;            }            int maxY = this.screenHeight - this._form.Height;   //Form 不溢出 Y轴最大值            if (this.locationPoint.Y > maxY)            {                this.locationPoint.Y = maxY;            }        }        #endregion        #region 窗体显示 右上、右下、左上、左下        ///         /// 窗体中心点        ///         /// 
private Point FormCentre() { Point frmCentre = new Point(); frmCentre.X = this.locationPoint.X + this._form.Width / 2; frmCentre.Y = this.locationPoint.Y + this._form.Height / 2; return frmCentre; } /// /// 屏幕中心点 /// ///
private Point ScreenCentre() { Point screenCentre = new Point(); screenCentre.X = this.screenWidth / 2; screenCentre.Y = this.screenHeight / 2; return screenCentre; } /// /// 判断窗体显示自的方向 /// ///
private PopUp JudgeDirection() { PopUp popup = PopUp.RightDown; Point frmCentre = FormCentre(); Point screenCentre = ScreenCentre(); if (frmCentre.X < screenCentre.X) { if (frmCentre.Y < screenCentre.Y) { popup = PopUp.LeftUp; } else { popup = PopUp.LeftDown; } } else { if (frmCentre.Y < screenCentre.Y) { popup = PopUp.RightUp; } else { popup = PopUp.RightDown; } } return popup; } #endregion }}
View Code

 

参考:屏幕、任务栏 工作域大小

当前的屏幕除任务栏外的工作域大小    this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;    this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;当前的屏幕包括任务栏的工作域大小this.Width=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;this.Height=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;任务栏大小this.Width=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width-System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;this.Height=System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height-System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;winform实现全屏显示WinForm:this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;this.WindowState = System.Windows.Forms.FormWindowState.Maximized;this.TopMost = true;   winform获取屏幕区域Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
View Code

 

转载地址:http://kekba.baihongyu.com/

你可能感兴趣的文章
高仿UC浏览器弹出菜单效果
查看>>
Ubuntu忘记密码,进不了系统的解决方法
查看>>
[原创]白盒测试技术思维导图
查看>>
<<Information Store and Management>> 读书笔记 之八
查看>>
Windows 8 开发之设置合约
查看>>
闲说HeartBeat心跳包和TCP协议的KeepAlive机制
查看>>
MoSQL
查看>>
Hibernate多对一外键单向关联(Annotation配置)
查看>>
《CLR via C#》读书笔记 之 方法
查看>>
设计模式:组合模式(Composite Pattern)
查看>>
ContentValues 和HashTable区别
查看>>
LogicalDOC 6.6.2 发布,文档管理系统
查看>>
给PowerShell脚本传递参数
查看>>
实战2——Hadoop的日志分析
查看>>
利用FIFO进行文件拷贝一例
查看>>
Ecshop安装过程中的的问题:cls_image::gd_version()和不支持JPEG
查看>>
resmgr:cpu quantum等待事件
查看>>
一个屌丝程序猿的人生(六十六)
查看>>
Java 编码 UTF-8
查看>>
SpringMVC实战(注解)
查看>>