C# winform 漸變效果


在用到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這個配合一下,就可以做出來了。
小心得,和大家分享一下。很希望和大家交個朋友。
大家記得聯系我啊!


免責聲明!

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



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