{"id":74,"date":"2007-05-12T13:36:43","date_gmt":"2007-05-12T11:36:43","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/transparent-wincontrols\/"},"modified":"2007-05-12T13:39:56","modified_gmt":"2007-05-12T11:39:56","slug":"transparent-wincontrols","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/transparent-wincontrols\/","title":{"rendered":"Transparent WinControls"},"content":{"rendered":"<p><em>In article <7kv799$\nefd8@forums.borland.com \n \n>, Robert wrote:<br \/>\n> WMEraseBkgnd message fills the TWincontrol with the brush color of the<br \/>\n> control, that makes the control opaque. Is it posible to make a<br \/>\n> TWincontrol transparent?<\/em><\/p>\n<p>Well, you have half the answer: since it is WM_ERASEBKGND that causes<br \/>\nthe background fill, trap the message and block it.<\/p>\n<p>Here is my standard reply on this transparent issue:<\/p>\n<p>> Please give me help on how to make a descendent class of TGroupBox<br \/>\n> transparent. Thanks in advance.<\/p>\n<p>Scott,<\/p>\n<p>a quick &#038; dirty method is<\/p>\n<p>[code lang=&#8221;delphi&#8221;]<br \/>\n  groupbox1.Brush.style := bsclear;<br \/>\n  groupbox1.handleneeded;<br \/>\n  setwindowlong( groupbox1.handle, GWL_EXSTYLE, WS_EX_TRANSPARENT );[\/code]<\/p>\n<p>in the forms OnCreate event handler.<\/p>\n<p>To do that kind of stuff right, create a new control derived from<br \/>\nTGroupbox, override its CreateParam method like this:<\/p>\n<p>[code lang=&#8221;delphi&#8221;]<br \/>\n private \/\/ in control declaration<br \/>\n Procedure CreateParams( Var params: TCreateParams ); override;<\/p>\n<p>Procedure TTransparentGroupbox.CreateParams( Var params: TCreateParams<br \/>\n);<br \/>\nbegin<br \/>\n  inherited CreateParams( params );<br \/>\n  params.ExStyle := params.ExStyle  or WS_EX_TRANSPARENT;<br \/>\nend;<\/p>\n<p>Add a handler for the WM_ERASEBKGND message:<\/p>\n<p>    Procedure WMEraseBkGnd( Var msg: TWMEraseBkGnd );<br \/>\n      message WM_ERASEBKGND;<\/p>\n<p>Procedure TTransparentGroupbox.WMEraseBkGnd( Var msg: TWMEraseBkGnd );<br \/>\nbegin<br \/>\n  SetBkMode( msg.DC, TRANSPARENT );<br \/>\n  msg.result := 1;<br \/>\nend;<br \/>\n[\/code]<\/p>\n<p>That is the basic frame for a TWinControl descendent. For a<br \/>\nTGraphicsControl you would drop the CreateParams (since only WinControls<br \/>\nhave that method) and override the create constructor. After calling the<br \/>\ninherited constructor you modify the ControlStyle of the control:<br \/>\n[code lang=&#8221;delphi&#8221;]<br \/>\n  ControlStyle := ControlStyle &#8211; [csOpaque];<br \/>\n  [\/code]<br \/>\nTransparency actually works better for TGraphicControls than for<br \/>\nTWinControls. The latter have problems if the control is moved or the<br \/>\nbackground needs to change. Delphi container controls (like form or<br \/>\npanel) are always created with the WS_CLIPCHILDREN style, which<br \/>\nautomatically excludes the area under child controls from updates, so<br \/>\nthe background will not be updated if required. Removing the<br \/>\nWS_CLIPCHILDREN style from a controls parent is possible with<br \/>\n[code lang=&#8221;delphi&#8221;]<br \/>\n  SetWindowLong( parent.handle, GWL_STYLE,<br \/>\n                 GetWIndowLong( parent.handle, GWL_STYLE )<br \/>\n                 and not WS_CLIPCHILDREN );<br \/>\n[\/code]<br \/>\nBut that may lead to accessive flicker on screen updates.               <\/p>\n<p>Peter Below (TeamB)<br \/>\n100113.1101@compuserve.com <\/p>\n<p>)<br \/>\nNo e-mail responses, please, unless explicitly requested!<\/p>\n<p>[tags]Delphi, Components[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In article , Robert wrote: > WMEraseBkgnd message fills the TWincontrol with the brush color of the > control, that makes the control&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[90,75],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-1c","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/74"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}