显示模态对话框时淡化背景

时间:2011-07-01 10:53:33

标签: c# c++ vb.net winforms

关闭Windows XP系统时,它会显示模式对话框,同时背景会渐变为灰度。我想在标签列表中的任何编程语言中实现相同的效果。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:7)

使用Winforms非常容易。您需要一个带有灰色背景的无边框最大化窗口,其中的Opacity随计时器而变化。淡入淡出完成后,您可以显示无边框的对话框,并使用TransparencyKey使其背景透明。以下是实现此目的的示例主要表单:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
        this.BackColor = Color.FromArgb(50, 50, 50);
        this.Opacity = 0;
        fadeTimer = new Timer { Interval = 15, Enabled = true };
        fadeTimer.Tick += new EventHandler(fadeTimer_Tick);
    }

    void fadeTimer_Tick(object sender, EventArgs e) {
        this.Opacity += 0.02;
        if (this.Opacity >= 0.70) {
            fadeTimer.Enabled = false;
            // Fade done, display the overlay
            using (var overlay = new Form2()) {
                overlay.ShowDialog(this);
                this.Close();
            }
        }
    }
    Timer fadeTimer;
}

对话框:

public partial class Form2 : Form {
    public Form2() {
        InitializeComponent();
        FormBorderStyle = FormBorderStyle.None;
        this.TransparencyKey = this.BackColor = Color.Fuchsia;
        this.StartPosition = FormStartPosition.Manual;
    }
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        this.Location = new Point((this.Owner.Width - this.Width) / 2, (this.Owner.Height - this.Height) / 2);
    }
    private void button1_Click(object sender, EventArgs e) {
        this.DialogResult = DialogResult.OK;
    }
}
相关问题