开发者

Fading the background while modal dialog is shown

When shutting down a Windows XP system, it displays a modal dialog box while the background fades to a grayscale. I wi开发者_Python百科ll like to achieve the same effect in any of the programming languages in the tag list. Can anyone help?


This is pretty easy to do with Winforms. You need a borderless maximized window with a gray background whose Opacity you change with a timer. When the fade is done, you can display a dialog that is borderless and uses the TransparencyKey to make its background transparent. Here's a sample main form that implements this:

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;
}

And the dialog:

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜