DropShadow - revisited

In a Canvas Paint event, I am drawing a rectangular shape at coordinates x,y with a height of w,h
the image being draw is a precalculated picture object
what I would like to have is a nice shadow under this… kind of like what you see in the Xojo IDE when displaying a window

g.forecolor = background_color
g.fillrect 0,0,g.width,g.height

w=pic.width
h=pic.height
x=(g.width-w)/2
y=(g.height-h)/2

// draw shadow bigger than (x,y)-(w,h)


g.drawpicture pic,x,y

Which OS do you wanna support?

macOS

Sub Paint(g As Graphics, areas() As REALbasic.Rect) DropShadow(g, &cF4A80800, 8, -8, 10) g.FillRect(50, 50, 100, 100) End Sub

Structure NSSize Width As CGFloat Height As CGFloat End Structure

[code]Private Sub DropShadow(g As Graphics, shadowColor As Color, x As Double, y As Double, blur As Double)
Declare Sub CGContextSetShadow Lib “CoreGraphics” (context As Integer, offset As NSSize, blur As CGFloat)
Declare Sub CGContextSetShadowWithColor Lib “CoreGraphics” (context As Integer, offset As NSSize, blur As CGFloat, CGColorRef As Ptr)

Declare Function CGColorSpaceCreateWithName Lib “CoreGraphics” (name As CFStringRef) As Ptr
Declare Function CGColorCreate Lib “CoreGraphics” (space As Ptr, components As Ptr) As Ptr

Dim context As Integer = g.Handle(g.HandleTypeCGContextRef)
Dim s As NSSize

s.Width = x
s.Height = y

Dim cspace, colorref As Ptr
Dim components As New MemoryBlock(4 * If(Target64Bit, 8, 4))

#If Target32Bit
components.SingleValue(0 * 4) = shadowColor.Red / 255
components.SingleValue(1 * 4) = shadowColor.Green / 255
components.SingleValue(2 * 4) = shadowColor.Blue / 255
components.SingleValue(3 * 4) = 100 / 100
#ElseIf Target64Bit
components.DoubleValue(0 * 8) = shadowColor.Red / 255
components.DoubleValue(1 * 8) = shadowColor.Green / 255
components.DoubleValue(2 * 8) = shadowColor.Blue / 255
components.DoubleValue(3 * 8) = 100 / 100
#Endif

cspace = CGColorSpaceCreateWithName(“kCGColorSpaceGenericRGB”)
colorref = CGColorCreate(cspace, components)

CGContextSetShadowWithColor(context, s, blur, colorref)
End Sub[/code]

I by myself looking for native declarations to do the same on Windows and Linux.

Perfect!
I had to tweak it a bit , but got it to look almost exactly as I want…

 DropShadow(g, &c40404000, 8, -50, 50)

FYI… you need to turn it “off” if you are drawing other items after that do not have a shadow

I don’t know if this is the “right” or “best” way, but seems to work

DropShadow(g,&c0, 0, 0, 0)

[quote=410136:@Dave S]FYI… you need to turn it “off” if you are drawing other items after that do not have a shadow
I don’t know if this is the “right” or “best” way, but seems to work[/quote]

[code]Private Sub ClearShadow(g As Graphics)
Declare Sub CGContextSetShadowWithColor Lib “CoreGraphics” (context As Integer, offset As NSSize, blur As CGFloat, CGColorRef As Ptr)

Dim s As NSSize
CGContextSetShadowWithColor(g.Handle(Graphics.HandleTypeCGContextRef), s, 0, Nil)
End Sub[/code]

Thanks!