文章出處

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的代碼清除,然后再更改繼承類,這樣也是可以的。

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

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

 


文章列表


不含病毒。www.avast.com
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()