C#:控制WinForm界面的顯示


控制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
{
    /// <summary>
    /// 彈出自方向   右上、右下、左上、左下
    /// </summary>
    internal enum PopUp
    {
        LeftUp,     //左上
        LeftDown,   //左下
        RightUp,    //右上
        RightDown   //右下
    }

    /// <summary>
    /// 顯示窗體
    /// </summary>
    public class ShowForm
    {
        #region 字段 屬性 

        private int screenWidth;    //除任務欄外的屏幕的寬
        private int screenHeight;   //除任務欄外的屏幕的高

        private Timer timer = new Timer();  //計時器

        private Form _form; 
        /// <summary>
        /// 顯示的窗體
        /// </summary>
        public Form TheForm
        {
            set 
            {
                this._form = value;
                SetLocation();
            }
            get { return this._form; }
        }

        private System.Drawing.Point locationPoint;
        /// <summary>
        /// 窗體顯示位置
        /// </summary>
        public System.Drawing.Point LocationPoint
        {
            set
            {
                this.locationPoint = value;
                LimitShowArea();
            }
            get { return this.locationPoint; }
        }

        private int time;
        /// <summary>
        /// 設置彈出窗體過程的整個時間
        /// </summary>
        public int Time
        {
            set { this.time = value; }
            get { return this.time; }
        }


        #endregion

        #region 構造函數

        /// <summary>
        /// 構造函數
        /// </summary>
        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
        
        /// <summary>
        /// 設置窗體動態化參數
        /// </summary>
        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;


        }

        /// <summary>
        /// 計時器間隔執行函數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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();
                }
            }
        }

        /// <summary>
        /// 開始顯示動態窗體
        /// </summary>
        public void Start()
        {
            this.timer.Enabled = true;
            this.timer.Start();
            this._form.Opacity = 0;
            this._form.Show();

        }

        /// <summary>
        /// 關閉顯示的窗體
        /// </summary>
        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 窗體顯示 右上、右下、左上、左下

        /// <summary>
        /// 窗體中心點
        /// </summary>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 屏幕中心點
        /// </summary>
        /// <returns></returns>
        private Point ScreenCentre()
        {
            Point screenCentre = new Point();
            screenCentre.X = this.screenWidth / 2;
            screenCentre.Y = this.screenHeight / 2;
            return screenCentre;
        }

        /// <summary>
        /// 判斷窗體顯示自的方向
        /// </summary>
        /// <returns></returns>
        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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM