Tested in Windows XP+7+10, Mac (El Capitan), and Linux Mint, but it'd be good to check if it works on all KDE/Gnome/Xfce Linuxii because there had been problems in the past in regards to this (but using a different api to the one im calling) http://www.purebasic.fr/english/viewtop ... 04&start=2
This demo simply has a Canvas gadget which defaults to flat white and doesn't seem to natively support transparency, so it changes that to the default window background and writes a DrawText() with that background color to achieve transparency like a simple Label/Text gadget would
Code: Select all
Procedure GetWindowBackgroundColor(hwnd=0) ;hwnd only used in Linux, ignored in Win/Mac
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
Protected color = GetSysColor_(#COLOR_WINDOW)
If color = $FFFFFF Or color=0: color = GetSysColor_(#COLOR_BTNFACE): EndIf
ProcedureReturn color
CompilerCase #PB_OS_Linux ;thanks to uwekel http://www.purebasic.fr/english/viewtopic.php?p=405822
Protected *style.GtkStyle, *color.GdkColor
*style = gtk_widget_get_style_(hwnd) ;GadgetID(Gadget))
*color = *style\bg[0] ;0=#GtkStateNormal
ProcedureReturn RGB(*color\red >> 8, *color\green >> 8, *color\blue >> 8)
CompilerCase #PB_OS_MacOS ;thanks to wilbert http://purebasic.fr/english/viewtopic.php?f=19&t=55719&p=497009
Protected.i color, Rect.NSRect, Image, NSColor = CocoaMessage(#Null, #Null, "NSColor windowBackgroundColor")
If NSColor
Rect\size\width = 1
Rect\size\height = 1
Image = CreateImage(#PB_Any, 1, 1)
StartDrawing(ImageOutput(Image))
CocoaMessage(#Null, NSColor, "drawSwatchInRect:@", @Rect)
color = Point(0, 0)
StopDrawing()
FreeImage(Image)
ProcedureReturn color
Else
ProcedureReturn -1
EndIf
CompilerEndSelect
EndProcedure
;##### SIMPLE DEMO #####
#Dlg1 = 0
#Canvas1 = 1
OpenWindow(#Dlg1, 0, 0, 400, 100, "Window Background Color", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)
CanvasGadget(#Canvas1, 20, 24, 360, 60, #PB_Canvas_Border)
Define bgcolor = GetWindowBackgroundColor(WindowID(#Dlg1))
HideWindow(#Dlg1, 0) ;(proof that it also works with hidden window, as we would hope!)
SetWindowTitle(#Dlg1, GetWindowTitle(#Dlg1) + " = " + Hex(bgcolor,#PB_Long))
If StartDrawing(CanvasOutput(#Canvas1))
Box(0,0,GadgetWidth(#Canvas1), GadgetHeight(#Canvas1), bgcolor) ;fill the canvas with the background color
DrawText(2,10, "Background should be same color as outer", RGB(0,0,0), bgcolor) ;text should now have 'transparent' backround
StopDrawing()
EndIf
Repeat
Define event.i = WaitWindowEvent()
Until event = #PB_Event_CloseWindow