Why must I paint the form background for an owner-draw control?

M

Mick Doherty

Are your buttons sitting on a form that has a BackgroundImage?
You'll find the same behaviour with 40 standard buttons.

I just placed 60 buttons on a form and it loaded immediately on my 2Ghz
machine.
 
T

Tim Crews

Are your buttons sitting on a form that has a BackgroundImage?
You'll find the same behaviour with 40 standard buttons.

I just placed 60 buttons on a form and it loaded immediately on my 2Ghz
machine.

Mick:

(I apologize for calling you Mike in an earlier post.)

My buttons are indeed sitting on a form that has a BackgroundImage.

By your second sentence, are you saying that even standard buttons will draw
slowly over a form with a BackgroundImage? That's not what I'm seeing. If I
change the FancyButtons in my form to System.Windows.Forms.Buttons, the form
goes from taking two seconds to draw, to drawing in one or two tenths of a
second. Standard buttons do not draw slowly over a form with a background
image. But that also seems to be what you are saying in your third sentence.

With my FancyButtons, if I remove the BackgroundImage from the parent form, I
am back to acceptable performance, i.e. the form displays in one or two tenths
of a second. It's not so fast that I can't see the screen being drawn, but
it's probably fast enough.

But the customer really did want that background image. Really, this is just
not a limitation that I would have expected from Windows Forms.

Are you suspicious that I have an implementation problem that is causing the
bad performance? I am still willing to plug your button code in and see how it
does.

Tim Crews
GECO, Inc.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Tim,

As the other suggested I got this work simply by turning Opaque to false.
However you need to set the transparent back color in order to get rid of
the BackColor in the background. Don't worry about seting
SupportTransparentBackColor. it looks like it is set by default for buttons.
Don't call the base OnPaint and you get rid of the button adornments

In you constructor you hould have
SetStyle(ControlStyles.Opaque, false);
this.BackColor = Color.FromArgb(0,Color.Green);

When you have background image and the better part of the button is
transparent this approach has some funky side effects when you move button
arround.

So if I were you I'd go with the Button's region.
 
M

Mick Doherty

I didn't notice that you called me Mike, but it's OK that you did. I have
been called much worse ;o)

I placed 60 fancyButtons on a form with no BackgroundImage and it displayed
immediately.

I hadn't tested with standard buttons, but I have now and it's not quite as
bad but there is still a delay in painting.

I haven't tried it, but you may get better results by setting the region
instead of using transparency. Your rounded edges won't look so good though.

Your best bet would probably be to create a Button Component, rather than
Control, which monitors it's Parent for paint and mouse events and calls
it's own Draw and HitTest Methods.

Frank Hilemans VG.Net is excellent and you may wish to consider using that.
 
F

Frank Hileman

Your best bet would probably be to create a Button Component, rather than
Control, which monitors it's Parent for paint and mouse events and calls
it's own Draw and HitTest Methods.

You stated this more clearly. If you don't want to use VG.net there is an
example somewhere by Shawn Burke called Lite Buttons, that has a very basic
implementation of a Button component. I believe it came with a sample
regarding code serialization of custom components. Look at the
windowsforms.com articles section for Shawn Burke articles.

- Frank
 
T

Tim Crews

Mick Doherty says... said:
I haven't tried it, but you may get better results by setting the region
instead of using transparency. Your rounded edges won't look so good though.


It seems unlikely to me that setting the region will result in better
performance. With the current implementation, the alpha channel of my owner-
drawn button is being used to mask the form background around my button, right?
This would seem to be a faster operation than dynamically drawing and filling a
region based on a data structure containing the outlines of that region.

Tim Crews
GECO, Inc.
 
M

Mick Doherty

I just tried a basic shaped button and there is no performance gain. It's
the initial loading of the button that is slow just as it is with the
transparent background. So it looks like a Component based button (Lite
Button) is the way to go.
 
M

Mick Doherty

If this is still an issue I have a workaround that may be satisfactory.

Inherit from Picturebox and give it the ScrollableControlDesigner Attribute.
You can then Dock this to the fill the form and place the buttons in that
instead.

[System.ComponentModel.Designer(typeof(System.Windows.Forms.Design.ScrollableControlDesigner))]
public class PictureBoxContainer : System.Windows.Forms.PictureBox
{
//Standard code ommitted...
}

note: you'll need a reference to System.Design.dll

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


"Mick Doherty"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top