C#實(shí)現(xiàn)WinForm窗體逐漸顯示效果

字號:


    C#實(shí)現(xiàn)WinForm窗體逐漸顯示效果,這個(gè)博客園里面已經(jīng)有其它人已經(jīng)實(shí)現(xiàn)了,原理很簡單,就是通過定時(shí)改變窗體的透明度(從0到1,即透明度從完全透明到不透明),我這里也是按照這個(gè)思路來實(shí)現(xiàn)的,但是我做的這個(gè)窗體是可復(fù)用的,即其它窗體繼承自它后,就能實(shí)現(xiàn)漸顯效果,代碼如下:
    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();
    }
    }
    }
    以下是自動(dòng)生成的代碼:
    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
    }
    }
    代碼中我用NEW關(guān)鍵字覆蓋了FORM類中的Opacity屬性,使其只讀并且不可編輯,有人可能會說這個(gè)屬性的只讀代碼寫得不規(guī)范,應(yīng)該是去掉SET訪問器或?qū)ET設(shè)為私有,沒錯(cuò),標(biāo)準(zhǔn)的是應(yīng)該這樣做,而我為何不這樣做呢?原因就是如果真正將屬性設(shè)為私有,那么在其它窗體繼承它的時(shí)候,由于我們一般都是先建一個(gè)標(biāo)準(zhǔn)窗體,標(biāo)準(zhǔn)窗體在創(chuàng)建時(shí)窗體的屬性若有默認(rèn)值的會自動(dòng)生成初始化默認(rèn)值,標(biāo)準(zhǔn)窗體創(chuàng)建后才將基類改為FormBase類,這樣就會造成報(bào)錯(cuò):Opacity是只讀的,不能賦值,所以我們只可以讓其外面看到是可讀寫,但實(shí)際子窗體的賦值不會生效,起到只讀效果,當(dāng)然了,如果你不覺得麻煩的話,你可以按標(biāo)準(zhǔn)屬性設(shè)置,然后每建立一個(gè)窗體后,請先將Opacity的代碼清除,然后再更改繼承類,這樣也是可以的。
    使用就很簡單了,與正常的窗體相同,在這里就不敘述了,大家可將以上代碼復(fù)制到自己的項(xiàng)目中,便可直接使用。
    其實(shí)通過以上代碼的思路,我們可以設(shè)計(jì)通用的百葉窗切換效果的窗體基類,有空我會試著去實(shí)現(xiàn)這些功能,希望大家能支持,謝謝!