在用到vs的興奮過程中,想給程序做個啟動畫面,我采用了顯示Aform,過一段時間,隱藏這個Aform,showdialog下一個Bform,closeAForm這個方法來做了,不知道大家有沒有跟好的辦法。
設定程序叢Aform啟動:
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Aform());
- }
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的代碼
- public partial class Aform: Form
- {
- public Form()
- {
- InitializeComponent();
- }
- private void Start_Load(object sender, EventArgs e)
- {
- StartWindowShowTime.Start();
- HideWindowStart.Start();
- }
- private void ShowMainwindow(object sender, EventArgs e)
- {
- Bform showmainwindows = new Bform();
- this.Hide();
- StartWindowShowTime.Stop();
- HideWindowStart.Stop();
- HideWindowSpeed.Stop();
- showmainwindows.ShowDialog();
- this.Close();
- }
- private void HideWindow(object sender, EventArgs e)
- {
- HideWindowSpeed.Start();
- }
- private void HideWindowSpeedStart(object sender, EventArgs e)
- {
- this.Opacity = this.Opacity - 0.02;
- }
- private void ShowWindow(object sender, EventArgs e)
- {
- if (this.Opacity == 1)
- {
- ShowWindowStart.Stop();
- }
- else
- {
- this.Opacity = this.Opacity + 0.02;
- }
- }
- }
好了,這個時候大家運行看看,嘿嘿淡入淡出。
我本來把Opacity每次更改的數值設置成了0.1,可是發現如果那樣的話淡入淡出不是很潤,所以縮小了數值和間隔時間。這樣看起來就潤多了。自我感覺不錯。
如果大家的程序只需要透明,那么只用設置Opacity這個就可以了。
漸變和淡入淡出照貓畫虎用timer和Opacity這個配合一下,就可以做出來了。
小心得,和大家分享一下。很希望和大家交個朋友。
大家記得聯系我啊!