How to make Win32 controls transparent?

  • Thread starter Thread starter Leo
  • Start date Start date
L

Leo

Hi,
I found that checkbox, label and groupbox have
painting problem with their background. I create simple
frame with background picture and add checkbox, label and
groupbox on it. I set transparent background on controls
to see whole picture. Yes, it works. But, if I set
controls style to System (to get correct XP Visual Style):

checkbox and label background lost transparency!!!
groupbox doesn't repaint background (group box is like
window throw the frame)

So, I think that these three controls in not implemented
correctly in .NET. So I start to implement my own Win32
controls. I create the "static" control (label is based
on it). But the problem is the same: "static" control is
not transparent.

So, question is: "How to make Win32 controls transparent?"
I think, that it could be done successfuly, because
checkbox and radiobutton is the same Win32 class, and
radiobutton is painted correctly, but checkbox is not.

Thanks for any advice.
Leo
 
Thanks for advice. I know that example. The controls provide own
painting method using UxTheme API, little bit diffrent from the system
controls. I'm looking for workaround without overriding painting. I
think, that it could be done. Leo
 
This is not satisfying me. Controls GroupBox, CheckBox
and Label has own custom paint method (btw which is only
similar to real Win32 controls). Is there chance to use
real Win32 paint method?

Leo
 
YES! GroupBox is working fine. I needn't overwrite the painting methods :-).
I'm sending You the final code:





public class XGroupBox : System.Windows.Forms.GroupBox

{

public XGroupBox()

{

this.FlatStyle = FlatStyle.System;

}



[Browsable(true), DefaultValue(FlatStyle.System)]

public new FlatStyle FlatStyle

{

get { return base.FlatStyle; }

set

{

base.FlatStyle = value;

if (value == FlatStyle.System && UxTheme.IsThemeActive)

{

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

this.BackColor = Color.Transparent;

}

}

}



protected override void WndProc(ref Message m)

{

if (m.Msg == Win32.WM_ERASEBKGND && FlatStyle == FlatStyle.System &&
UxTheme.IsThemeActive)

{

Graphics g = Graphics.FromHdc(m.WParam);

this.OnPaintBackground(new PaintEventArgs(g,this.ClientRectangle));

g.Dispose();

return;

}

base.WndProc(ref m);

}

}





Thank You very much!!! And what about the rest? I mean CheckBox and Label
controls? I found, that under some visual styles (for example jumjibar) the
CheckBox (in .NET apps) is displayed correctly! I don't have explanation for
this behavior.

Kind Regards

Leo
 
Hello Leo,
I'm glad to know you get the GroupBox work, As For the the CheckBox &
Label control, their look and behavior are implemented by the system, so I
think partly visual styles support maybe the common control's problem, If
you want to fix these problems I think you need handle the OnPaint method
on your own , I agree with the author of
http://www.codeproject.com/cs/miscctrl/themedtabpage.asp
you may read the "More trouble... GroupBox and CheckBox" chapter for more
information,
I didn't find a better solution on this problem, if you have any ideas ,
please let me know, Thanks!

Kind regards,

Ying-Shen Yu [MSFT]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.
--------------------
| From: "Leo" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: How to make Win32 controls transparent?
| Date: Fri, 22 Aug 2003 13:26:58 +0200
| Lines: 90
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: mail.softcase.cz 195.250.137.131
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:50756
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| YES! GroupBox is working fine. I needn't overwrite the painting methods
:-).
| I'm sending You the final code:
|
|
|
|
|
| public class XGroupBox : System.Windows.Forms.GroupBox
|
| {
|
| public XGroupBox()
|
| {
|
| this.FlatStyle = FlatStyle.System;
|
| }
|
|
|
| [Browsable(true), DefaultValue(FlatStyle.System)]
|
| public new FlatStyle FlatStyle
|
| {
|
| get { return base.FlatStyle; }
|
| set
|
| {
|
| base.FlatStyle = value;
|
| if (value == FlatStyle.System && UxTheme.IsThemeActive)
|
| {
|
| this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
|
| this.BackColor = Color.Transparent;
|
| }
|
| }
|
| }
|
|
|
| protected override void WndProc(ref Message m)
|
| {
|
| if (m.Msg == Win32.WM_ERASEBKGND && FlatStyle == FlatStyle.System &&
| UxTheme.IsThemeActive)
|
| {
|
| Graphics g = Graphics.FromHdc(m.WParam);
|
| this.OnPaintBackground(new PaintEventArgs(g,this.ClientRectangle));
|
| g.Dispose();
|
| return;
|
| }
|
| base.WndProc(ref m);
|
| }
|
| }
|
|
|
|
|
| Thank You very much!!! And what about the rest? I mean CheckBox and Label
| controls? I found, that under some visual styles (for example jumjibar)
the
| CheckBox (in .NET apps) is displayed correctly! I don't have explanation
for
| this behavior.
|
| Kind Regards
|
| Leo
|
|
|
 
Back
Top