Dimming the background.

I've received a question from the customer the other day asking me how he can implement functionality of "dimming" background effect when a message box is shown in his Windows Mobile application. In fact it should be pretty easy to do utilizing the DrawAlpha method from the GraphicsExtender class that I showed you in this webcast. Take a look at the following code snippet:

protected override void OnPaint(PaintEventArgs e)

{

      // Create a temp bitmap

      Bitmap dimBackGround = new Bitmap(this.Width, this.Height);

      Graphics gxTemp = Graphics.FromImage(dimBackGround);

      // Color it black

      gxTemp.Clear(Color.Black);

      // Draw it with alpha transparency

      e.Graphics.DrawAlpha(dimBackGround, 100, 0, 0);

      // Clean up

      gxTemp.Dispose();

      dimBackGround.Dispose();

 }

The code above shows OnPaint override method in the BackgroundForm class that I've added to the demo project. I've set the FormBorderStyle to None and WindowState to Maximized for this from to make it full screen.

So, now before displaying a message box you will need to show the BackroundForm:

  BackgroundForm form = new BackgroundForm();

  form.Show();

  MessageBox.Show(txtMessage.Text);

  form.Close();

And here's the result:

Download the sample code from here.

DimmingBackgroundTest.zip

Comments

  • Anonymous
    April 03, 2009
    Hi Alex, Thanks for this great sample. I was looking for such a thing. I think there's a small bug in the code, when the messagebox is shown and you try to move it, it leaves a copy of it on the background.. it happens every time you do move the messagebox, I think the background form should be redrawn? Thanks, a.

  • Anonymous
    April 30, 2009
    Thanks for the fantastic article. I am trying to gradually dim the background by decreasing transparency of black backround image in several steps in OnPaint. while also drawing  sliding menu poping up from bottom but it makes code very slow and I donot get smooth and slick movment for sliding menu. Any idea.

  • Anonymous
    May 02, 2009
    You may need to use double-buffering to reduce flickering.

  • Anonymous
    May 07, 2009
    The comment has been removed

  • Anonymous
    December 16, 2009
    The comment has been removed

  • Anonymous
    January 08, 2010
    Greate information, many thanks. I've tried to change your sample code to VisualBasic.NET. The method DrawAlpha seems to be missing in VisualBasic.NET. Is that really missing? I don't find it as a method from e.Graphics. Any idea on how to manage background dimming in VB.NET CF?

  • Anonymous
    January 10, 2010
    The DrawAlpha is an extension method coming from the GraphicsExtender class. -Alex

  • Anonymous
    July 04, 2010
    why did you use two different graphics objects? does it matter?  one time you used gxTemp, next time was e.graphics.      gxTemp.Clear(Color.Black);      e.Graphics.DrawAlpha(dimBackGround, 100, 0, 0);

  • Anonymous
    December 19, 2010
    The comment has been removed