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


      // Draw it with alpha transparency

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

      // Clean up




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();




And here's the result:

Download the sample code from here.


Comments (9)

  1. aymanfm says:

    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?



  2. Vijay_Visana says:

    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.

  3. AlexYak says:

    You may need to use double-buffering to reduce flickering.

  4. ChristopherFairbairn says:

    Hi Alex,

    I know this is a kind of loaded question… but do you know of a way to resolve the issues caused by not repainting the background (i.e. the message box drag and drop issue highlighted by a previous commenter)?

    I’ve improved that situation in the past by having the background form take a screenshot of the device beforing becomming visible. Then rather than ignoring OnEraseBackground I’ve made it blit the stored screenshot and alpha blended it for the disabled look.

    This improves the issue, as popping up the start menu, or moving the message box doesn’t cause screen artifacts, but it is still less than ideal.

    For example it doesn’t fix the problem if the user rotates the screen (where the screenshot becomes the wrong shape etc).

    I’ve tried various techniques to detect screen orientation changes and then temporarily disable the dimmed form in order to take an updated screenshot. However all techniques I’ve thought of so far have lead to unsightly flicker which has lead me to drop the idea.

    I’d really love to work on finding a solution to this, given the current GDI / Win32 windowing constraints. Do you have any thoughts on possible approaches?

  5. ozkary says:

    Anybody knows how to handle the problem with the message box? It leaves copies in the background as the device changes orientation


  6. raftbone says:

    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?

  7. AlexYak says:

    The DrawAlpha is an extension method coming from the GraphicsExtender class.


  8. Hamish Ahern says:

    why did you use two different graphics objects? does it matter?  one time you used gxTemp, next time was e.graphics.


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

  9. Vikash says:

    Hi Alex,

    My requirement is that the user should not be able to click / perform any menu action on the background form if the message box displays. Like a modal dialog form.


Skip to main content