Transparent Label on a Form

S

Scott Eguires

I have a form that I am drawing text onto in the top left
corner. The problem is when I drop a label or even a
button and make the background color transparent it shows
the text that I have drawn in the label even if the label
is somewhere else on the screen. It appears it draws the
background onto the label starting at 0,0 but what I
really want is for the label to display what is directly
behind it and not include the drawn text from the form if
it is not directly below it.

Thanks

Scott Eguires
 
Y

Ying-Shen Yu[MSFT]

Hi Scott,

Thanks for your post.
I'm not very clear about your problem,

From my understanding now, you have drawn some text on the form in its
Paint event, then you put a label on the from and set the back color of the
label to be transparent. The problem is the text drawn on the from can be
seen even if the label is not over the text.

Please correct me if I misunderstood your problem.

Could you give me a small sample to repro this problem and a pic to show
what you really want. I'll look into it and see if I can figure it out.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Y

Ying-Shen Yu[MSFT]

Hi Scott,

Thanks for your reply!

I can see the problem now, however I'm not able to repro it on my system as
your descrption. Here is my repro steps:
1) open a Winfrom application project
2) put a label on the center of the form , set the back color of the label
to "transparent"
3) override the OnPaintBackground method as follows
<code>
Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(pevent)
pevent.Graphics.DrawString("Hello world", Me.Font, Brushes.Red, 0,
0)
End Sub
</code>
4) run the program, on my machine the form shows properly, the lable didn't
have the text "Hello world" on its background.

You may try make a small app to repro this program, If you can repro it,
please send the repro sample to me, Probably I missed something important.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
S

Scott Eguires

I narrowed down my problem. I was doing some
transformations and did a resettransform which causes the
problem:

Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(pevent)
pevent.graphics.resettransform
pevent.Graphics.DrawString("Hello world",
Me.Font, Brushes.Red, 0, 0)
End Sub

I belive if I just save the graphics state before the
transform and restore it after I should be good. Can you
please give me insight on why a resettransform causes
this problem?

Thanks,

Scott Eguires


myGraphics.ResetTransform()
 
Y

Ying-Shen Yu[MSFT]

Hi Scott,

Thanks for your reply!

the Implementation of the transparency in .NET Framwork is simulated by
repainting the parent control. So on painting the label, the label will use
its graphic object to paint the background, it need first set the transform
offset before calling the parent control's OnPaintBackground method because
the graphic object is in it's client coordinate.
So if you called the ResetTransform in the parent control's
OnPaintBackground the Transform offset is reset to 0,0, then the DrawString
will draw the text in the label.
You may check it using this code snippet

<code>
Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
System.Diagnostics.Debug.WriteLine("OnPaintBackground Begin")
MyBase.OnPaintBackground(pevent)
System.Diagnostics.Debug.WriteLine(Label1.Location)

System.Diagnostics.Debug.WriteLine(pevent.Graphics.Transform.OffsetX)

System.Diagnostics.Debug.WriteLine(pevent.Graphics.Transform.OffsetY)
pevent.Graphics.ResetTransform()

System.Diagnostics.Debug.WriteLine(pevent.Graphics.Transform.OffsetX)

System.Diagnostics.Debug.WriteLine(pevent.Graphics.Transform.OffsetY)

pevent.Graphics.DrawString("Hello world", Me.Font, Brushes.Red, 0,
0)
System.Diagnostics.Debug.WriteLine("OnPaintBackground End")
End Sub
</code>

Thanks!




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
S

Scott Eguires

Thanks for the insight. I solved the problem. I saved
the graphics state before the transform and after the
transform I restored it. Thanks again for the
explanation of the ResetTransform.

Scott Eguires
-----Original Message-----
Hi Scott,

Thanks for your reply!

the Implementation of the transparency in .NET Framwork is simulated by
repainting the parent control. So on painting the label, the label will use
its graphic object to paint the background, it need first set the transform
offset before calling the parent control's
OnPaintBackground method because
the graphic object is in it's client coordinate.
So if you called the ResetTransform in the parent control's
OnPaintBackground the Transform offset is reset to 0,0, then the DrawString
will draw the text in the label.
You may check it using this code snippet

<code>
Protected Overrides Sub OnPaintBackground(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
System.Diagnostics.Debug.WriteLine ("OnPaintBackground Begin")
MyBase.OnPaintBackground(pevent)
System.Diagnostics.Debug.WriteLine (Label1.Location)
System.Diagnostics.Debug.WriteLine
(pevent.Graphics.Transform.OffsetX)
System.Diagnostics.Debug.WriteLine
(pevent.Graphics.Transform.OffsetY)
pevent.Graphics.ResetTransform()

System.Diagnostics.Debug.WriteLine
(pevent.Graphics.Transform.OffsetX)
System.Diagnostics.Debug.WriteLine
(pevent.Graphics.Transform.OffsetY)

pevent.Graphics.DrawString("Hello world", Me.Font, Brushes.Red, 0,
0)
System.Diagnostics.Debug.WriteLine ("OnPaintBackground End")
End Sub
</code>

Thanks!




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.

.
 

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