Home All Groups Group Topic Archive Search About
Author
30 Nov 2004 9:14 PM
Olo

Hi,

I have to write transparent control - simple red rectangle. When I mouse
  move control should change position(left and top). The problem is when
red rectangle is moving over other controls such Buttons, PictureBoxes
it flicker.

Below is code of rectangle control :


    public class TransRectangle : System.Windows.Forms.UserControl
    {

    public TransRectangle()
    {
        InitializeComponent();
    }

    protected override CreateParams CreateParams
    {
        get
        {
        CreateParams cp=base.CreateParams;
        cp.ExStyle|=0x00000020;                             //WS_EX_TRANSPARENT
        return cp;
        }
    }

        //Override the OnPaintBackground event. This is ecessary         //to
prevent the background to be painted.
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do nothing
    }

    protected void  InvalidateEx()
    {
        if(Parent==null)
            return;
        Rectangle rc=new Rectangle(this.Location,this.Size);
            Parent.Invalidate(rc,true);
    }


    protected override void OnMove(EventArgs e)
    {
        this.InvalidateEx();
        Graphics g = this.CreateGraphics();
        g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1,        
                this.Height -1);
        g.Dispose();

    }



    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawRectangle(Pens.Red, 0, 0, this.Width - 1,
                      this.Height -1);
        }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        this.Left += e.X;
        this.Top += e.Y;
        base.OnMouseMove (e);
    }

    public void pInvalidate()
    {
        this.InvalidateEx();
    }

}


Regards,
Olo
Author
30 Nov 2004 9:25 PM
RBischoff
Hello Olo,

This was a post I ran into on the web that might help you out.

Take a look at Control.SetStyle, and especially
ControlStyles.AllPaintingInWmPaint and ControlStyles.DoubleBuffer
If you set both of these styles to true for your control you'll get
double-buffering (painting to an off-screen bitmap) without having to write
additional code.

- Paul Harrington [Visual Studio .NET Performance Team]

This posting is provided "AS IS" with no warranties, and confers no rights.


Best of luck!

Your C# ally ,
RBischoff


O> Hi,
O>
O> I have to write transparent control - simple red rectangle. When I
O> mouse
O> move control should change position(left and top). The problem is
O> when
O> red rectangle is moving over other controls such Buttons,
O> PictureBoxes
O> it flicker.
O>
O> Below is code of rectangle control :
O>
O> public class TransRectangle : System.Windows.Forms.UserControl
O> {
O> public TransRectangle()
O> {
O> InitializeComponent();
O> }
O> protected override CreateParams CreateParams
O> {
O> get
O> {
O> CreateParams cp=base.CreateParams;
O> cp.ExStyle|=0x00000020;                             //WS_EX_TRANSPARENT
O> return cp;
O> }
O> }
O> //Override the OnPaintBackground event. This is ecessary         //to
O> prevent the background to be painted.
O> protected override void OnPaintBackground(PaintEventArgs e)
O> {
O> // do nothing
O> }
O> protected void  InvalidateEx()
O> {
O> if(Parent==null)
O> return;
O> Rectangle rc=new Rectangle(this.Location,this.Size);
O> Parent.Invalidate(rc,true);
O> }
O> protected override void OnMove(EventArgs e)
O> {
O> this.InvalidateEx();
O> Graphics g = this.CreateGraphics();
O> g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1,
O> this.Height -1);
O> g.Dispose();
O> }
O>
O> protected override void OnPaint(PaintEventArgs e)
O> {
O> e.Graphics.DrawRectangle(Pens.Red, 0, 0, this.Width - 1,
O> this.Height -1);
O> }
O> protected override void OnMouseMove(MouseEventArgs e)
O> {
O> this.Left += e.X;
O> this.Top += e.Y;
O> base.OnMouseMove (e);
O> }
O> public void pInvalidate()
O> {
O> this.InvalidateEx();
O> }
O> }
O>
O> Regards,
O> Olo
Are all your drivers up to date? click for free checkup

Author
1 Dec 2004 1:02 AM
Olo
Hi,

I tested SetStyle method but it not works for transparent control.
Beside when we set ControlStyles.AllPaintingInWmPaint flag, background
of control become black and still flicker.
this.SetStyle ( ControlStyles.DoubleBuffer |
        ControlStyles.AllPaintingInWmPaint |
        ControlStyles.UserPaint |
        ControlStyles.Opaque , true);

Regards,
Olo.
Show quoteHide quote
> Hello Olo,
>
> This was a post I ran into on the web that might help you out.
>
> Take a look at Control.SetStyle, and especially
> ControlStyles.AllPaintingInWmPaint and ControlStyles.DoubleBuffer
> If you set both of these styles to true for your control you'll get
> double-buffering (painting to an off-screen bitmap) without having to write
> additional code.
>
> - Paul Harrington [Visual Studio .NET Performance Team]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> Best of luck!
>
> Your C# ally ,
> RBischoff
>
>
> O> Hi,
> O>
> O> I have to write transparent control - simple red rectangle. When I
> O> mouse
> O> move control should change position(left and top). The problem is
> O> when
> O> red rectangle is moving over other controls such Buttons,
> O> PictureBoxes
> O> it flicker.
> O>
> O> Below is code of rectangle control :
> O>
> O> public class TransRectangle : System.Windows.Forms.UserControl
> O> {
> O> public TransRectangle()
> O> {
> O> InitializeComponent();
> O> }
> O> protected override CreateParams CreateParams
> O> {
> O> get
> O> {
> O> CreateParams cp=base.CreateParams;
> O> cp.ExStyle|=0x00000020;                             //WS_EX_TRANSPARENT
> O> return cp;
> O> }
> O> }
> O> //Override the OnPaintBackground event. This is ecessary         //to
> O> prevent the background to be painted.
> O> protected override void OnPaintBackground(PaintEventArgs e)
> O> {
> O> // do nothing
> O> }
> O> protected void  InvalidateEx()
> O> {
> O> if(Parent==null)
> O> return;
> O> Rectangle rc=new Rectangle(this.Location,this.Size);
> O> Parent.Invalidate(rc,true);
> O> }
> O> protected override void OnMove(EventArgs e)
> O> {
> O> this.InvalidateEx();
> O> Graphics g = this.CreateGraphics();
> O> g.DrawRectangle(Pens.Red, 0, 0, this.Width - 1,
> O> this.Height -1);
> O> g.Dispose();
> O> }
> O>
> O> protected override void OnPaint(PaintEventArgs e)
> O> {
> O> e.Graphics.DrawRectangle(Pens.Red, 0, 0, this.Width - 1,
> O> this.Height -1);
> O> }
> O> protected override void OnMouseMove(MouseEventArgs e)
> O> {
> O> this.Left += e.X;
> O> this.Top += e.Y;
> O> base.OnMouseMove (e);
> O> }
> O> public void pInvalidate()
> O> {
> O> this.InvalidateEx();
> O> }
> O> }
> O>
> O> Regards,
> O> Olo
>

Bookmark and Share