Transparent window in linux
Posted: Fri Aug 10, 2018 4:11 pm
Hi,
I'm using a universal splash routine in my Windows and macOS apps and I'd like to extend it to cover Linux versions as well but I don't have a clue how to make the magic work.
The code below works for rectangular images (without transparency) but I tend to use circular .png images on a transparent background.
The sphere.png is like this:
Any ideas?
Dave
I'm using a universal splash routine in my Windows and macOS apps and I'd like to extend it to cover Linux versions as well but I don't have a clue how to make the magic work.
The code below works for rectangular images (without transparency) but I tend to use circular .png images on a transparent background.
Code: Select all
ImportC "-no-pie" : EndImport
UsePNGImageDecoder() ; setup system to use a .png image
; premultiply image (required for Windows splash screen but does no harm for OSX)
Procedure PreMultiply(image)
Define i, j, colour
StartDrawing(ImageOutput(image))
DrawingMode(#PB_2DDrawing_AllChannels)
For j=0 To ImageHeight(image)-1
For i=0 To ImageWidth(image)-1
colour = Point(i,j)
Plot(i,j, RGBA(Red(colour) & $FF * Alpha(colour) & $FF / $FF,
Green(colour) & $FF * Alpha(colour) & $FF / $FF,
Blue(colour) & $FF * Alpha(colour) & $FF / $FF,
Alpha(colour)))
Next
Next
StopDrawing()
EndProcedure
; this splash-screen business is a bit of a faff but has the advantage that it works even if the
; image has transparent areas. e.g., a round image on a transparent background.
Procedure showSplash(s, image)
ExamineDesktops()
#SPLASH_IMAGE = 0
#SPLASH_WINDOW = 1
CatchImage(#SPLASH_IMAGE, image) ; load the image into memory
w.l = ImageWidth(#SPLASH_IMAGE) ; splash image width
h.l = ImageHeight(#SPLASH_IMAGE) ; splash image height
x.l = (DesktopWidth(0)-w)/2 ; x position
y.l = (DesktopHeight(0)-h)/2 ; y position
StartTime.q = ElapsedMilliseconds() ; access the millisecond clock
PreMultiply(#SPLASH_IMAGE)
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS ; if this is an OSX compiler ...
OpenWindow(#SPLASH_WINDOW, x, y, w, h, "", #PB_Window_BorderLess|#PB_Window_Invisible)
CopyImage(#SPLASH_IMAGE, #SPLASH_WINDOW)
CocoaMessage(0, WindowID(#SPLASH_WINDOW), "setBackgroundColor:", CocoaMessage(0, 0, "NSColor colorWithPatternImage:", ImageID(#SPLASH_WINDOW)))
CocoaMessage(0,WindowID(#SPLASH_WINDOW),"setHasShadow:",#YES)
CocoaMessage(0, WindowID(#SPLASH_WINDOW), "setOpaque:", #NO)
CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows ; else if this is a Windows compiler ...
OpenWindow(#SPLASH_WINDOW, x, y, w, h,"",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
SetWindowLongPtr_(WindowID(#SPLASH_WINDOW), #GWL_EXSTYLE, GetWindowLongPtr_(WindowID(#SPLASH_WINDOW), #GWL_EXSTYLE)|#WS_EX_LAYERED|#WS_EX_TOOLWINDOW)
hDC = StartDrawing(ImageOutput(#SPLASH_IMAGE))
With sz.SIZE
\cx = ImageWidth(#SPLASH_IMAGE)
\cy = ImageHeight(#SPLASH_IMAGE)
EndWith
With BlendMode.BLENDFUNCTION
\SourceConstantAlpha = 255
\AlphaFormat = 1
EndWith
UpdateLayeredWindow_(WindowID(#SPLASH_WINDOW),0,0,@sz,hDC,@ContextOffset.POINT,0,@BlendMode,2)
StopDrawing()
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux ; else if this is an linux compiler ...
OpenWindow(#SPLASH_WINDOW, x, y, w, h, "", #PB_Window_BorderLess|#PB_Window_Invisible)
can = CanvasGadget(#PB_Any,0, 0, w, h)
If StartDrawing(CanvasOutput(can))
DrawingMode(#PB_2DDrawing_Default)
DrawAlphaImage(ImageID(#SPLASH_IMAGE), 0, 0)
StopDrawing()
EndIf
CompilerElse
ProcedureReturn
CompilerEndIf
HideWindow(#SPLASH_WINDOW, #False)
While ElapsedMilliseconds() - StartTime < s*1000 ; wait for 's' seconds showing splash screen
WaitWindowEvent(1)
Wend
CloseWindow(#SPLASH_WINDOW)
EndProcedure
showSplash(3,?SplashImage)
End
DataSection
SplashImage:
IncludeBinary "sphere.png"
EndDataSection
Any ideas?
Dave