C# winform 漸層效果

來源:互聯網
上載者:User

標籤:winform   style   blog   http   color   io   os   ar   for   

在用到vs的興奮過程中,想給程式做個啟動畫面,我採用了顯示Aform,過一段時間,隱藏這個Aform,showdialog下一個Bform,closeAForm這個方法來做了,不知道大家有沒有跟好的辦法。
設定程式叢Aform啟動:

 

 

[c-sharp] view plaincopy
  1. static void Main()  
  2.       {  
  3.           Application.EnableVisualStyles();  
  4.           Application.SetCompatibleTextRenderingDefault(false);  
  5.           Application.Run(new Aform());  
  6.       }  

 

AForm中定義如下timer

StartWindowShowTime    HideWindowStart    HideWindowSpeed   ShowWindowStart

定義了他們的屬性  StartWindowShowTime(顯示Aform的時間長度) Enabled=True Interval=5000 (100=1秒)
HideWindowStart (開始隱藏Aform的過程) Enabled=True Interval=4500
HideWindowSpeed (隱藏Aform的漸層間隔) Enabled=False Interval=10
ShowWindowStart  (顯示AForm的漸層間隔) Enabled=True Interval=10

Ok,   下面開始定義這些timer的Tick 在Events裡面可以直接填寫,timer就這一個,也可以後台寫,不過我覺得在這裡填寫比較方便,而且可以自動產生方法的聲明,不用找了。偷懶一下。
StartWindowShowTime Tick:ShowMainwindow
HideWindowStart  Tick:HideWindow
HideWindowSpeed  Tick:HideWindowSpeedStart
ShowWindowStart Tick:ShowWindow
好了,到這裡我要說Windows Form 實現透明效果,漸層效果,淡入淡出效果的實現最重要一員了,那就是Form屬性裡的Opacity,用的就是這個。我考證過,只有2000以上的系統支援這個屬性。
 我們先將Aform的Opacity設定成0,好了開始寫Aform的代碼

[c-sharp] view plaincopy
  1. public partial class Aform: Form  
  2.    {  
  3.        public Form()  
  4.        {  
  5.            InitializeComponent();   
  6.              
  7.              
  8.        }  
  9.   
  10.        private void Start_Load(object sender, EventArgs e)  
  11.        {  
  12.            StartWindowShowTime.Start();  
  13.            HideWindowStart.Start();  
  14.        }  
  15.   
  16.        private void ShowMainwindow(object sender, EventArgs e)  
  17.        {  
  18.            Bform showmainwindows = new Bform();              
  19.            this.Hide();  
  20.            StartWindowShowTime.Stop();  
  21.            HideWindowStart.Stop();  
  22.            HideWindowSpeed.Stop();  
  23.            showmainwindows.ShowDialog();  
  24.            this.Close();  
  25.        }  
  26.   
  27.        private void HideWindow(object sender, EventArgs e)  
  28.        {  
  29.            HideWindowSpeed.Start();  
  30.        }  
  31.   
  32.        private void HideWindowSpeedStart(object sender, EventArgs e)  
  33.        {  
  34.            this.Opacity = this.Opacity - 0.02;  
  35.        }  
  36.   
  37.        private void ShowWindow(object sender, EventArgs e)  
  38.        {  
  39.            if (this.Opacity == 1)  
  40.            {  
  41.                ShowWindowStart.Stop();  
  42.            }  
  43.            else  
  44.            {  
  45.                this.Opacity = this.Opacity + 0.02;  
  46.            }  
  47.        }  
  48.   
  49.    }  

 

好了,這個時候大家運行看看,嘿嘿淡入淡出。
我本來把Opacity每次更改的數值設定成了0.1,可是發現如果那樣的話淡入淡出不是很潤,所以縮小了數值和間隔時間。這樣看起來就潤多了。自我感覺不錯。
如果大家的程式只需要透明,那麼只用設定Opacity這個就可以了。
漸層和淡入淡出照貓畫虎用timer和Opacity這個配合一下,就可以做出來了。
小心得,和大家分享一下。很希望和大家交個朋友。
大家記得聯絡我啊!

C# winform 漸層效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.