Advertisement

06.23.2008 at 07:56AM PDT, ID: 23507795
[x]
Attachment Details

Opacity of a Child Form has OnPaintBackGround issue - Parent painting last!

Asked by clintcarter1999 in .Net Editors & IDEs, Microsoft Visual C#.Net, Microsoft Development

Tags: C#, Firefox 2.0

I am creating a "Sheet" which overlays a WinForm and provides a MessageBox like functionality...actually, it's much more interactive...but rather than come up in another window...it comes up inside the WinForm that invokes it...

An important part of the "Sheet" functionality is that it also provides a translucent (Opacity = 50%) covering of the first (Parent) form which provides two key functions:

1.  To "effectively" disable the Parent WinForm's controls by overlaying the "Sheet" form on top of the WinForm.
2.  The Opacity set to 50% gives a visual clue to the user that the controls underneath the "Sheet" are disabled.

Important Notes:
1.  The Sheet cannot use the stock Opacity property...because in order to add this Form (Sheet) to the Parent WinForm, you have to first disable it as a TopLevel Form.

     Sheet.TopLevel = false;

2.  You cannot do Controls.Add to the Sheet Form without setting TopLevel = false;

3.  Microsoft, in their GUI wisdom, decided to disable the Opacity property of all Forms with TopLevel = false;

So my real goal right now is to create child form which can have an Opacity property AND handles window invalidations properly.

I have achieved that goal to some extent by making the child form transparent and then overriding the OnPaintBackGround method and filling the background using an Alpha channel.  

Here is the code which is based on:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2363164&SiteID=1

       protected override CreateParams CreateParams

        {

            get

            {

                CreateParams cp = base.CreateParams;

                cp.ExStyle |= 0x00000020;//WS_EX_TRANSPARENT

                return cp;

            }

        }

 

        private int opacity;
        public int Opacity

        {
            get { return opacity; }

            set {

                opacity = value;

                this.InvalidateEx();

            }

        }

 
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Color bk = Color.FromArgb(Opacity, this.BackColor);

            e.Graphics.FillRectangle(new SolidBrush(bk), e.ClipRectangle);
        }

        protected void InvalidateEx()
        {

            if (Parent == null)

                return;

            Rectangle rc = new Rectangle(this.Location, this.Size);

            Parent.Invalidate(rc, true);

        }


This actually works great on the first draw...my problem occurs whenever a portion of the Sheet and WinForm Parent are invalidated by another window.  

The problem is that the Sheet gets the OnPaintBackGround event BEFORE the Parent and any other controls on the Parent WinForm.  

PROBLEM: What that means is that the Parent form is drawn last which effectively hides my "Sheet".


I am confused why the child control (Sheet) is handling his OnPaintBackGround before it's parent...this seems very wrong...I know this is the case because I put Debug.Print statements in both the Parent and the child (Sheet)'s OnPaintBackGround methods to see who get's called first.  Further, I tried to make my Sheet WinForm be at the top of the Zorder in two ways:

            parentForm.Controls.Add(this);
       
            parentForm.Controls.SetChildIndex(this, 0);  <-- make sure he is at the top of the Zorder
            sheet.TopMost = true; <-- force him to be TopMost


NOTE:  The OnPaintBackGround is called in the correct order upon the first showing of the WinForm (Parent, other controls lower in the zorder, then the Sheet Form).  The problem only seems to happen when a region is invalidated...


I would be happy to post the project somewhere for you guys to play with and see if you can figure it out...


Any ideas?
Start Free Trial
 
Loading Advertisement...
 
[+][-]06.23.2008 at 08:10AM PDT, ID: 21847034

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .Net Editors & IDEs, Microsoft Visual C#.Net, Microsoft Development
Tags: C#, Firefox 2.0
Sign Up Now!
Solution Provided By: clintcarter1999
Participating Experts: 0
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628