![](/img/trans.png)
[英]How to change opacity of Form window without changing opacity of text
[英]Window form opacity .. How to control?
我現在有一個表單窗口應用程序,我想在應用程序運行時更改form opacity
。 意味着當應用程序運行時,它將顯示low opacity
表單,隨着時間的推移,它將顯示完整的表單, 100 opacity
為100 opacity
。 那么如何做到這一點。 (我應該使用定時器控件來控制不透明度,如果是,那么如何????)
在表單的構造函數中,您可以編寫這樣的內容。
this.Opacity = .1;
timer.Interval = new TimeSpan(0, 0, intervalinminutes);
timer.Tick += ChangeOpacity;
timer.Start();
然后定義一個這樣的方法
void ChangeOpacity(object sender, EventArgs e)
{
this.Opacity += .10; //replace.10 with whatever you want
if(this.Opacity == 1)
timer.Stop();
}
要淡入淡出表單,我通常這樣做:
for(double opacity = 0.0; opacity <= 1.0; opacity += 0.2) {
DateTime start = DateTime.Now;
this.Opacity = opacity;
while(DateTime.Now.Subtract(start).TotalMilliseconds <= 30.0) {
Application.DoEvents();
}
}
如果您很少這樣做,這是一個不錯的簡單解決方案。 否則,我會建議使用線程。
對於退出降低不透明度動畫
///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void DecreaseOpacity(object sender, EventArgs e)
{
if (this.Opacity >= 0.1)
{
this.Opacity -= 0.04; //replace 0.04 with whatever you want
}
if (this.Opacity <= 0.0)
timer.Stop();
if (this.Opacity <= 0.1)
{
System.Environment.Exit(1);
Process.GetCurrentProcess().Kill();
}
}
private void Exit_Click(object sender, EventArgs e)
{
timer.Interval = 47; //replace 47 with whatever you want
timer.Tick += DecreaseOpacity;
timer.Start();
}
在構造函數中,啟動將在每個滴答聲中調用方法的計時器控件。
timer.Interval = 1000;
timer.Tick += new EventHandler(TimerEventProcessor);
timer.Start();
………………
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs)
{
if(this.Opacity < 1)
this.Opacity += .1;
else
timer.Stop();
}
在應用程序啟動時增加不透明度動畫
///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
void IncreaseOpacity(object sender, EventArgs e)
{
if (this.Opacity <= 1) //replace 0.88 with whatever you want
{
this.Opacity += 0.01; //replace 0.01 with whatever you want
}
if (this.Opacity == 1) //replace 0.88 with whatever you want
timer.Stop();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Opacity = .01;
timer.Interval = 10; //replace 10 with whatever you want
timer.Tick += IncreaseOpacity;
timer.Start();
}
在構造函數中,將不透明度設置為 0,並以 10 或 100 毫秒的小間隔啟動計時器。 在timer_Tick
事件中,你只需要運行this.Opacity += 0.01;
這將使不透明度從 0 開始,每幾毫秒增加 0.01,直到它為 1(不透明度是雙倍,當它達到 1 的值時,它是完全不透明的)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.