[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

How to draw fast a random shaped controls ?

Bachus

1/13/2005 7:51:00 PM

Hi,
I have a form on which I want to place elliptic shaped controls. Next I want
to move these controls. Everything works fine when there is less than ten
controls but when I have a lot of them (about 100) my controls are redrawing
very slow. But if i use normal shaped control it works fine.
I created shaped control by overriding Paint method, I also added some
styles to accelerate drawing but still it is too slow:

public class ShapedControl : System.Windows.Forms.UserControl
{.....
public ShapedControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
BackColor = Color.Black;
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
System.Drawing.Drawing2D.GraphicsPath path = new
System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(0, 0, this.Width, this.Height);
Region = new Region(path);
}
....
}

To make a comparision I created a standard shape control and draw an ellipse
on it in Paint method:
public NonShapedControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
BackColor = Color.White;
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillEllipse(Brushes.Black, 0, 0, this.Width, this.Height);
}

If you put 100 NonShapedControls and move them all for example on button
click you will see that it works fine.
But if you put 100 ShapedControls it will redraw very slow.

Is there any way to accelerate drawing random shaped controls ?

Thanks,
Adam


4 Answers

Bob Powell

1/13/2005 10:46:00 PM

0

Using controls for graphics applications is a big mistake.

Controls have their own windows and all the baggage that comes with it.

You need a retained mode graphics system.

http://www.bobpowell.net/ani...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"Bachus" <bachus@polbox.com> wrote in message
news:cs6jd7$t8r$1@opal.icpnet.pl...
> Hi,
> I have a form on which I want to place elliptic shaped controls. Next I
> want
> to move these controls. Everything works fine when there is less than ten
> controls but when I have a lot of them (about 100) my controls are
> redrawing
> very slow. But if i use normal shaped control it works fine.
> I created shaped control by overriding Paint method, I also added some
> styles to accelerate drawing but still it is too slow:
>
> public class ShapedControl : System.Windows.Forms.UserControl
> {.....
> public ShapedControl()
> {
> // This call is required by the Windows.Forms Form Designer.
> InitializeComponent();
> // TODO: Add any initialization after the InitializeComponent call
> BackColor = Color.Black;
> this.SetStyle(
> ControlStyles.AllPaintingInWmPaint |
> ControlStyles.UserPaint |
> ControlStyles.DoubleBuffer,
> true);
> }
>
> protected override void OnPaint(PaintEventArgs e)
> {
> base.OnPaint (e);
> System.Drawing.Drawing2D.GraphicsPath path = new
> System.Drawing.Drawing2D.GraphicsPath();
> path.AddEllipse(0, 0, this.Width, this.Height);
> Region = new Region(path);
> }
> ...
> }
>
> To make a comparision I created a standard shape control and draw an
> ellipse
> on it in Paint method:
> public NonShapedControl()
> {
> // This call is required by the Windows.Forms Form Designer.
> InitializeComponent();
> // TODO: Add any initialization after the InitializeComponent call
> BackColor = Color.White;
> this.SetStyle(
> ControlStyles.AllPaintingInWmPaint |
> ControlStyles.UserPaint |
> ControlStyles.DoubleBuffer,
> true);
> }
> protected override void OnPaint(PaintEventArgs e)
> {
> base.OnPaint(e);
> e.Graphics.FillEllipse(Brushes.Black, 0, 0, this.Width, this.Height);
> }
>
> If you put 100 NonShapedControls and move them all for example on button
> click you will see that it works fine.
> But if you put 100 ShapedControls it will redraw very slow.
>
> Is there any way to accelerate drawing random shaped controls ?
>
> Thanks,
> Adam
>
>


Bachus

1/14/2005 1:23:00 AM

0

I know that using controls for graphics applications is a big mistake. But I
am not creating graphics application, in my example i wrote that when i put
about 100 controls it works slow but it was just for testing purposes. With
twenty controls it still works slow. And I don't think putting and moving
twenty controls is graphics application. Besides I need my controls capture
mouse events and some other functions. Lets say i want to put 20 elliptic
buttons and for some reason move them on form.
What I want to know is:
Why changing drawing region dramatically slows down drawing of control ? And
how i can avoid that ?
And one more thing:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Region = new Region(new Rectangle(0,0,this.Width,this.Height);
}
This code I suppose set drawing region to the size of control, so the effect
will be exactly the same if we did not override OnPaint method but of course
it works slower.

And again one more thing
Compare these two methods:

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
this.Region = new Region(new Rectangle(0,0,this.Width,this.Height));
this.Region.Exclude(new Rectangle(5,5,10,10));
e.Graphics.FillRectangle(Brushes.Yellow, new Rectangle(0, 0, this.Width,
this.Height));
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Region r = new Region(new Rectangle(0,0,this.Width,this.Height));
r.Exclude(new Rectangle(5,5,10,10));
this.Region = r;
e.Graphics.FillRectangle(Brushes.Yellow, new Rectangle(0, 0, this.Width,
this.Height));
}

I think these methods should work exactly the same way, but first method
draws big yellow rectangle and second method draws big yellow rectangle with
small excluded rectangle .
Can someone explain that ?

Thanks,
Adam


U¿ytkownik "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> napisa³ w
wiadomo¶ci news:eDdXjHc%23EHA.2180@TK2MSFTNGP12.phx.gbl...
> Using controls for graphics applications is a big mistake.
>
> Controls have their own windows and all the baggage that comes with it.
>
> You need a retained mode graphics system.
>
> http://www.bobpowell.net/ani...
>
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Find great Windows Forms articles in Windows Forms Tips and Tricks
> http://www.bobpowell.net/tips...
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/f...
>
> All new articles provide code in C# and VB.NET.
> Subscribe to the RSS feeds provided and never miss a new article.
>


Frank Hileman

1/14/2005 1:33:00 AM

0

Hello Bachus,

"Bachus" <bachus@polbox.com> wrote in message
news:cs76qf$ibf$1@opal.icpnet.pl...
>I know that using controls for graphics applications is a big mistake. But
>I
> am not creating graphics application, in my example i wrote that when i
> put
> about 100 controls it works slow but it was just for testing purposes.
> With
> twenty controls it still works slow. And I don't think putting and moving
> twenty controls is graphics application. Besides I need my controls
> capture
> mouse events and some other functions. Lets say i want to put 20 elliptic
> buttons and for some reason move them on form.

It is also possible to use a retained mode graphics system for catching
mouse events. The free run-time in my signature provides mouse events.

> What I want to know is:
> Why changing drawing region dramatically slows down drawing of control ?
> And
> how i can avoid that ?

I believe the clipping region is expanded out into a set of clipping
rectangles, which is not the most efficient way to do things internally.

> And one more thing:
> protected override void OnPaint(PaintEventArgs e)
> {
> base.OnPaint (e);
> Region = new Region(new Rectangle(0,0,this.Width,this.Height);
> }
> This code I suppose set drawing region to the size of control, so the
> effect
> will be exactly the same if we did not override OnPaint method but of
> course
> it works slower.

Best to change the Region only once, outside of OnPaint.

> And again one more thing
> Compare these two methods:
....
>
> I think these methods should work exactly the same way, but first method
> draws big yellow rectangle and second method draws big yellow rectangle
> with
> small excluded rectangle .
> Can someone explain that ?

The Region property is probably returning a copy on get.

Regards,
Frank Hileman

check out VG.net: http://www.vg...
Animated vector graphics system
Integrated Visual Studio .NET graphics editor


Bachus

1/14/2005 12:07:00 PM

0

Hello

Thanks for help.

U¿ytkownik "Frank Hileman" <frankhil@no.spamming.prodigesoftware.com>
napisa³ w wiadomo¶ci news:ee4Yrjd%23EHA.2700@TK2MSFTNGP14.phx.gbl...
>
> I believe the clipping region is expanded out into a set of clipping
> rectangles, which is not the most efficient way to do things internally.
> >
> > protected override void OnPaint(PaintEventArgs e)
> > {
> > base.OnPaint (e);
> > Region = new Region(new Rectangle(0,0,this.Width,this.Height);
> > }
>
> Best to change the Region only once, outside of OnPaint.
>

Well of course i tried to change the Region outside OnPaint, I did it once
in OnLoad, but it doesn't matter it works exactly the same way. And what I
think is strange the shape of Region also does not matter. It works slow
when shape of Region is for example elliptic, and also works slow when shape
of Region is Rectangle, and I think that if Region is Rectangle then set of
clipping rectangles is one element. I even tried to use Windows GDI
functions:
Region = Region.FromHrgn(CreateRectRgn(5,5,20,20));

And still it works slow.