I can't let this one go by without offering my two cents. This code has been kicking around the forums for at least as long as I've been here and that's ten years. But there's a hole in it you could drive a truck through. It looks fine and dandy until you go to change the text to something different. Then you realize that the hollow brush won't erase what was there before and everything turns to crap. Here's the code with a 2-second timer added that will change the text. See what happens when the timer fires:
Code: Select all
Procedure WCB(WindowID, Message, wParam, lParam)
Select Message
Case #WM_CTLCOLORSTATIC
Select lparam
Case GadgetID(1)
SetBkMode_(wparam, #TRANSPARENT)
SetTextColor_(wparam, RGB(255, 0, 0))
ProcedureReturn GetStockObject_(#HOLLOW_BRUSH)
EndSelect
EndSelect
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
If OpenWindow(0, 0, 0, 500, 250, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(1, 10, 10, 400, 20, "Transparenter Text", 0)
AddWindowTimer(0,1,2000)
SetWindowColor(0, RGB(255, 255, 0))
SetWindowCallback(@WCB(), 0)
Repeat
ev = WaitWindowEvent()
If ev = #PB_Event_Timer
SetGadgetText(1, "Hello World!")
EndIf
Until ev = #PB_Event_CloseWindow
EndIf
So how to fix it? The problem is the hollow brush. A null brush is no better and so this is the way I approach it when I need transparency:
1. Create a background brush for the window. It can be a solid color, pattern or image. Apply it to the main window.
2. Create a background brush for the gadget from the image used for the window.
3. Return your gadget background from #WM_CTLCOLORSTATIC instead of the hollow or null brush.
4. Update your gadget background brush any time the gadget is moved or sized.
Here is a sample with the above approach applied:
Code: Select all
Global imgWindowBackground, hBrWindowBackground0, hBrGadgetBackground1
Procedure CreateWindowBackground(windownumber)
imgWindowBackground = CreateImage(#PB_Any, WindowWidth(windownumber), WindowHeight(windownumber), 24)
StartDrawing(ImageOutput(imgWindowBackground))
DrawingMode(#PB_2DDrawing_Gradient)
FrontColor(#Black)
BackColor(#White)
LinearGradient(0, 0, ImageWidth(imgWindowBackground), ImageHeight(imgWindowBackground))
Box(0,0,ImageWidth(imgWindowBackground), ImageHeight(imgWindowBackground))
StopDrawing()
hBrWindowBackground0 = CreatePatternBrush_(ImageID(imgWindowBackground))
EndProcedure
Procedure GetGadgetBackground(gadgetnumber)
this_image = GrabImage(imgWindowBackground, #PB_Any, GadgetX(gadgetnumber), GadgetY(gadgetnumber), GadgetWidth(gadgetnumber), GadgetHeight(gadgetnumber))
If hBrGadgetBackground1
DeleteObject_(hBrGadgetBackground1)
EndIf
hBrGadgetBackground1 = CreatePatternBrush_(ImageID(this_image))
FreeImage(this_image)
EndProcedure
Procedure WCB(WindowID, Message, wParam, lParam)
Select Message
Case #WM_CTLCOLORSTATIC
Select lparam
Case GadgetID(1)
SetBkMode_(wparam, #TRANSPARENT)
SetTextColor_(wparam, RGB(255, 0, 0))
ProcedureReturn hBrGadgetBackground1
EndSelect
EndSelect
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
If OpenWindow(0, 0, 0, 500, 250, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CreateWindowBackground(0)
SetClassLongPtr_(WindowID(0), #GCL_HBRBACKGROUND, hBrWindowBackground0)
InvalidateRect_(WindowID(0),0,1)
TextGadget(1, 200, 100, 200, 20, "Transparenter Text")
GetGadgetBackground(1)
SetWindowCallback(@WCB(), 0)
InvalidateRect_(GadgetID(1),0,1)
AddWindowTimer(0,1,1000)
Repeat
ev = WaitWindowEvent()
If ev = #PB_Event_Timer
ResizeGadget(1, Random(300), Random(200), #PB_Ignore, #PB_Ignore)
GetGadgetBackground(1)
SetGadgetText(1, "Hello World! "+Str(Random(100000)))
EndIf
Until ev = #PB_Event_CloseWindow
EndIf
DeleteObject_(hBrWindowBackground0)
DeleteObject_(hBrGadgetBackground1)
Now it's gonna work and you've got total control.