C#實現WinForm窗體逐漸顯示效果


C#實現WinForm窗體逐漸顯示效果,這個博客園里面已經有其它人已經實現了,原理很簡單,就是通過定時改變窗體的透明度(從0到1,即透明度從完全透明到不透明),我這里也是按照這個思路來實現的,但是我做的這個窗體是可復用的,即其它窗體繼承自它后,就能實現漸顯效果,代碼如下:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TEMS.Forms
{
    public partial class FormBase : Form
    {
        private Timer formTimer = null;

        /// <summary>
        /// 獲取Opacity屬性
        /// </summary>
        [DefaultValue(0)]
        [Browsable(false)]
        public new double Opacity
        {
            get { return base.Opacity; }
            set { base.Opacity = 0; }
        }

        public FormBase()
        {
            InitializeComponent();
            formTimer = new Timer() { Interval = 100 };
            formTimer.Tick += new EventHandler(formTimer_Tick);
            base.Opacity = 0;
        }

        private void formTimer_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
            {
                formTimer.Stop();
            }
            else
            {
                base.Opacity += 0.2;
            }
        }

        private void FormBase_Shown(object sender, EventArgs e)
        {
            formTimer.Start();
        }
    }
}

以下是自動生成的代碼:

namespace TEMS.Forms
{
    partial class FormBase
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // FormBase
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "FormBase";
            this.Text = "FormBase";
            this.Shown += new System.EventHandler(this.FormBase_Shown);
            this.ResumeLayout(false);

        }

        #endregion
    }
}
View Code

代碼中我用NEW關鍵字覆蓋了FORM類中的Opacity屬性,使其只讀並且不可編輯,有人可能會說這個屬性的只讀代碼寫得不規范,應該是去掉SET訪問器或將SET設為私有,沒錯,標准的是應該這樣做,而我為何不這樣做呢?原因就是如果真正將屬性設為私有,那么在其它窗體繼承它的時候,由於我們一般都是先建一個標准窗體,標准窗體在創建時窗體的屬性若有默認值的會自動生成初始化默認值,標准窗體創建后才將基類改為FormBase類,這樣就會造成報錯:Opacity是只讀的,不能賦值,所以我們只可以讓其外面看到是可讀寫,但實際子窗體的賦值不會生效,起到只讀效果,當然了,如果你不覺得麻煩的話,你可以按標准屬性設置,然后每建立一個窗體后,請先將Opacity的代碼清除,然后再更改繼承類,這樣也是可以的。

使用就很簡單了,與正常的窗體相同,在這里就不敘述了,大家可將以上代碼復制到自己的項目中,便可直接使用。

其實通過以上代碼的思路,我們可以設計通用的百葉窗切換效果的窗體基類,有空我會試着去實現這些功能,希望大家能支持,謝謝!

 


免責聲明!

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



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