We help IT Professionals succeed at work.

Prevent drawing form background

AlexFM
AlexFM asked
on
Medium Priority
563 Views
Last Modified: 2008-01-09
I have Form-derived class which is used as MDI child. I want to prevent form to redraw it's background. To do this I added the following code:

        private void FormImage_Load(object sender, EventArgs e)
        {
            this.SetStyle(ControlStyles.DoubleBuffer |
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint,
                true);
            this.UpdateStyles();
        }

Now I expect to see transparent form which does't redraw it's background. But form still redraws background according to it's BackColor property. What else should I do?
Comment
Watch Question

e1v

Commented:
What happens if you add ControlStyles.Opaque ? (But I don't really have a clue)

Author

Commented:
Even with Opaque style form redraws itself.
From my VC++ experience, if window doesn't draw anything in WM_ERASEBKGND handler, it remains transparent and shows all garbage which is under it. I expect to see the same in C#, because I want to draw image in Paint handler and don't want flickering.
Commented:
override Form's OnPaintBackGround event and suppress base call.

protected override void OnPaintBackground(PaintEventArgs pevent)
{
      // base.OnPaintBackground (pevent);                //comment it out
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Tried this, form still draws background.
From MSDN: If you do not want the default Windows behavior, you must set the event's Handled property to true.

I didn't find any Handled property.

Commented:
I just tried this with a very basic winApp, and it works. may be you're doing something else too.

Author

Commented:
OK, it didn't work with UserPaint and AllPaintingInWmPaint styles. I still don't know what is better - using styles or overriding OnPaintBackground, because there is no actual drawing code yet. But I will keep this solution for future use, thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.