This fragment is about to be reported (you'll remain on this page): You can enter a comment to clarify the mistake if you would like to: |
Is IS possible to create a TRichEdit control with transparent background in Delphi.
Yesterday I've stumbled upon an interesting demo of a transparent RichEdit control made for VisualC++ at The Code Project (called «See through Rich Edit control»). I'm currently writing an app in Delph 7 which uses TRichEdit as a log/console window and looking at that demo with cute Mario face I thought how cool would it make plain console window look like that too.
I went digging and discovered that to create a transparent RichEdit you simply
and merely need to create its window with WS_EX_TRANSPARENT style – and also supply
an WM_ERASEBKGND handler to make sure no junk is left from previous redraws.
Adding a custom style seemed a trivial task in Delphi – simply override TWinControl'sCreateParams method and add your style to ExStyle. However, this didn't work
out for some reason (the control was behaving like it known nothing of WS_EX_TRANSPARENT))
but resorting to this method:
pascalprocedure TTransparentRichEdit.CreateWnd; begin inherited; SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TRANSPARENT); end;
Hurray, now we only need to hook WM_ERASEBKGND – no surprises here so you can proceed to downloading the component without furner ado from my side :)
Download TTransparentRichEdit control (it has no external dependencies).
If you haven't installed any custom component yet simply go to Component → Install component menu. Note that this class was written and tested on Delphi of 7th version only.
What you see here is actually a TTransparentRichEdit control with Nagato’s portrait PNG loaded (the Background property), below which lies a standard TImage with a JPEG picture shining through the RichEdit’s background.
However, use such tricks with overlaying controls on your risk because it might result in glitches – for example, TImage seems to be painted after the RichEdit receives WM_ERASEBKGND and thus it fully overlays the latter when window is resized.
It works fine when there’s nothing below RichEdit, though.
14 March 2013
Tropic
When I edit the text in the Richedit and have a background image either a image behind the richedit or a background image from the components property, the text gets scrambled and overlaps. Any way to fix this behavior? I have a onchange event, what could I use in this event to prevent overlapping text?
14 March 2013
Proger_XP
If I understand your problem this happens because RichEdit no more has any background and thus can’t clear itself and when it’s repainted old bits are left and new view is painted over them. The result is garbage. I haven’t looked into this much but as I understand it’s a fundamental problem, you’ll have to either have a normal background for RichEdit to erase with or clear its old canvas yourself some other way on WM_ERASEBKGND.
15 December 2012
Anonymous
Hi, this is very nice but I can’t do the same thing with TMemo or TEdit. any idea?
15 December 2012
Proger_XP
I’ve checked it but looks like this trick won’t work and I couldn’t find a quick way of making it so. Although TRichEdit inherits from TCustomMemo either Windows treats this control differently or there’s something else in VCL processing background that TRichEdit disables.
21 August 2012
gian55
works great on DELPHI 2010… I've just compiled with success!!! thank to the author
25 December 2012
FromRusWithLove
On delphi 2006 work good. if use «pngimage» you can load png files.
21 August 2012
Proger_XP
Hey, had no idea it would work in recent Delphi versions… Congrats.