兩種極具實用價值的 WinForms 特效窗體:漸變窗體(透明度從 0 到 1 淡入)與 滑動通知提示框(從屏幕底部滑入,確認後滑出消失)。核心分別利用 Form.Opacity 與 Control.Location 配合 Timer 實現。
在 Load 設置 Opacity = 0,Timer 每 50ms 增加 0.05 直到達到 1。
private double opacity = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (opacity <= 1)
{
opacity += 0.05;
this.Opacity = opacity;
}
}
初始位置設在屏幕可視區外下方(Y = screenHeight),Timer 每次向上移動 5 像素直到完全顯示。點擊「確定」後設置標誌位,Timer 轉為向下移動直到完全移出屏幕。
private bool finished = false;
private void timer1_Tick(object sender, EventArgs e)
{
if (!finished)
{
if (Location.Y + Height >= screenHeight)
Location = new Point(Location.X, Location.Y - 5);
}
else
{
if (Location.Y < screenHeight)
Location = new Point(Location.X, Location.Y + 5);
}
}
private void btnOK_Click(object sender, EventArgs e) => finished = true;
完整可執行程序:CSDN 下載