Custom Windows Form Control

E

Erdem

Hi everyone

I am developing a custom control (inherit from control) that just
draws
rectangle border.
Then i insert more than 50 instance of this control in a panel.


The thing i am trying to do is : to draw Selection Rectangle in panel.
User
would be able to select instances of my custom controls in panel ,
which are
in selection rectangle.


The problem is;


While i am drawing selection rectangle, my custom controls (that are
in
panel) blinks (they repaint themselves). There are also labels in
panel but
they dont get repaint. this shows i am making a mistake with my
custom
control paint event.


When i set -- >SetStyle(ControlStyles.Opaque, true); , blinking
decreases
but this time control doesnt draw its background (because of style it
is
meant to be :) ) so it is not usefull.

Any Suggestions for decreasing blinking of my custom control (not to
make it, repaint itself )


Thanks in advance


Erdem


this is my custom control's constructor and Paint event


public myPanel()
{
InitializeComponent();
SetStyle(ControlStyles.ContainerControl, false);
SetStyle(ControlStyles.DoubleBuffer,true);
SetStyle(ControlStyles.AllPaintingInWmPaint,false);
SetStyle(ControlStyles.ResizeRedraw,true);
SetStyle(ControlStyles.UserPaint,true);

SetStyle(ControlStyles.SupportsTransparentBackColor,true);


}


protected override void OnPaint(PaintEventArgs e)
{
Color borderColor = _borderDrawColor;
if (Selected)
borderColor = BorderSelectedColor;


Graphics g = e.Graphics;
ControlPaint.DrawBorder(
g,
new Rectangle(0, 0, this.Width, this.Height),
borderColor,
_borderWidth,
ButtonBorderStyle.Solid,
borderColor,
_borderWidth,
ButtonBorderStyle.Solid,
borderColor,
_borderWidth,
ButtonBorderStyle.Solid,
borderColor,
_borderWidth,
ButtonBorderStyle.Solid);


g.Dispose();


}
 
P

Peter Duniho

Erdem said:
[...]
Any Suggestions for decreasing blinking of my custom control (not to
make it, repaint itself )

The first thing I would try is to set the DoubleBuffered property to
true in the constructor.

I don't know how things work now, but in the "olden days", the focus
rectangle for controls was drawn using an XOR drawing mode. The benefit
of this was that nothing else had to be drawn to change the focus
rectangle; draw it once, it's there...draw it again, it's gone. The
downside of course was it was easy to have a bug where a half-obscured
control wound up with half a focus rectangle, whether it had focus or
not. :)

Maybe the current controls still use this mechanism, and that's why you
don't see flicker with the built-in ones. But .NET doesn't offer a
managed-code way to do XOR drawing. If you want to stick to .NET
methods, you'll have to solve flicker the more traditional way, with
double-buffering. In most cases, simply setting the DoubleBuffered
property for the control is sufficient, though in some cases you may
find you need to set DoubleBuffered for the entire form.

Pete
 

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