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

字號(hào):


    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屬性,使其只讀并且不可編輯,有人可能會(huì)說這個(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)值的會(huì)自動(dòng)生成初始化默認(rèn)值,標(biāo)準(zhǔn)窗體創(chuàng)建后才將基類改為formbase類,這樣就會(huì)造成報(bào)錯(cuò):opacity是只讀的,不能賦值,所以我們只可以讓其外面看到是可讀寫,但實(shí)際子窗體的賦值不會(huì)生效,起到只讀效果,當(dāng)然了,如果你不覺得麻煩的話,你可以按標(biāo)準(zhǔn)屬性設(shè)置,然后每建立一個(gè)窗體后,請(qǐng)先將opacity的代碼清除,然后再更改繼承類,這樣也是可以的。
    使用就很簡單了,與正常的窗體相同,在這里就不敘述了,大家可將以上代碼復(fù)制到自己的項(xiàng)目中,便可直接使用。
    其實(shí)通過以上代碼的思路,我們可以設(shè)計(jì)通用的百葉窗切換效果的窗體基類,有空我會(huì)試著去實(shí)現(xiàn)這些功能,希望大家能支持,謝謝!