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
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.
private void button2_Click(object sender, EventArgs e)
DimmerDialog dimmer = new DimmerDialog();
"You can show either a Form or a MessageBox here.",
"Fatal Error has occurred", MessageBoxIcon.Stop);
And here's some code that shows how to show a form.
private void button1_Click(object sender, EventArgs e)
DimmerDialog dimmer = new DimmerDialog();
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.
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#).
public class DimmerDialog
public void ShowForm(Form form);
public void ShowMessageBox(string text, string title,
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.
if(m_ColorReductionStep == 0)
Bitmap^ imagecopy = (Bitmap^)m_Image->Clone();
BitmapData^ bmData = imagecopy->LockBits(
BYTE* p = (BYTE*)bmData->Scan0.ToPointer();
int nOffset = bmData->Stride - imagecopy->Width * 3;
BYTE red, green, blue;
for(int y=0; y < imagecopy->Height; y++)
for(int x=0; x < imagecopy->Width; x++)
blue = p;
green = p;
red = p;
p = (BYTE)(.001 * red + .286 * green + .713 * blue);
p = (BYTE)(.001 * red + .986 * green + .013 * blue);
p = (BYTE)(.899 * red + .087 * green + .013 * blue);
p = (BYTE)(.099 * red + .387 * green + .514 * blue);
p = (BYTE)(.099 * red + .887 * green + .014 * blue);
p = (BYTE)(.699 * red + .287 * green + .013 * blue);
p = (BYTE)(.199 * red + .487 * green + .314 * blue);
p = (BYTE)(.199 * red + .787 * green + .014 * blue);
p = (BYTE)(.499 * red + .487 * green + .014 * blue);
p = p = p = (BYTE)(.299 * red + .587 * green + .114 * blue);
p += 3;
p += nOffset;
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.
- Feb 23 2006 - Article first published.
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com
where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket
as well as a programming book – Extending MFC applications with the .NET Framework
Nish's latest book C++/CLI in Action
published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.