A modal dialog that fades the background to gray-scale imitating the XP shutdown screen

DimmerDialog is a .NET class that shows a modal dialog which grays out the rest of the background, just like the Windows XP Shutdown dialog. This can be used when your application needs to show a very important message box or form that requires immediate user attention.

Introduction

DimmerDialog is a .NET class that shows a modal dialog which grays out the rest of the background, just like the Windows XP Shutdown dialog. This can be used when your application needs to show a very important message box or form that requires immediate user attention. The class allows you to show either a message box (where you can set the text, title and icon), or a Form instance.

How the class works

The XP Shutdown dialog shows a modal form, while the background fades to gray-scale. I've tried to simulate this, though my fading is not as smooth or impressive (I've used regular GDI stuff whereas the XP Shutdown dialog may have used more powerful techniques like DirectX). I create a new desktop (named with a GUID), switch to this desktop, show the background, fade it using a timer, and all this while the modal form or message box is kept on top. Here's an animated gif that shows the fading effect (quality is poor due to GIF color depth limitations).

Using the class

Here's some sample code that shows how you can show a message-box.

privatevoid button2_Click(object sender, EventArgs e)
{
DimmerDialog dimmer = new DimmerDialog();
dimmer.ShowMessageBox(
"You can show either a Form or a MessageBox here.",
"Fatal Error has occurred", MessageBoxIcon.Stop);
}

When you show a form, make sure that your form does not start any process (as a button click action for instance), because that process will run on your primary desktop and not on this desktop. It's best to keep things simple (like the XP shutdown dialog has) and to merely use the form to collect some data.

Class implementation

The class was written in mixed mode C++/CLI, but here's what the public interface looks like in C# (simulated - actual code is not in C#).

Here's the fade function I wrote (it's C++), where the final gray-scaling code is a rip out of one of Christian's GDI+ articles. The fading algorithm is one I finalized on after some trial and error. It's far from perfect, but I thought this will do, considering the utility we get out of the fading effect.

Don't even ask me why I used those fractions, I just got them after various attempts. Perhaps by modifying them, and by adding more steps (currently I use a 4-step fading), we might be able to get a smoother effect, but I didn't think it worth the effort.

Share

About the Author

Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 16 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (14 consecutive awards as of 2015).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.