Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Transparent control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Olo - 30 Nov 2004 21:14 GMT
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
RBischoff - 30 Nov 2004 21:25 GMT
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
Olo - 01 Dec 2004 01:02 GMT
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.
> Hello Olo,
>
[quoted text clipped - 83 lines]
> O> Regards,
> O> Olo

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.