Round window?

Hi there

I am attempting to create a round window, and from what I have read, and found in threads here, this should work.

================

// Declare what we needDeclare Function NSClassFromString Lib “AppKit” (clsName As CFStringRef) As PtrDeclare Function sharedApplication Lib “AppKit” Selector “sharedApplication” (clsRef As Ptr) As PtrDeclare Function mainWindow Lib “AppKit” Selector “mainWindow” (appRef As Ptr) As PtrDeclare Function contentView Lib “AppKit” Selector “contentView” (windowRef As Ptr) As Ptr

Declare Function CGPathCreateWithEllipseInRect Lib “CoreGraphics” (ByRef rect As CGRect, transform As Ptr) As PtrDeclare Sub setWantsLayer Lib “AppKit” Selector “setWantsLayer:” (view As Ptr, flag As Boolean)Declare Function layer Lib “AppKit” Selector “layer” (view As Ptr) As PtrDeclare Sub setMask Lib “QuartzCore” Selector “setMask:” (layerRef As Ptr, maskLayer As Ptr)

Declare Function NSClassFromStringCA Lib “QuartzCore” Alias “NSClassFromString” (clsName As CFStringRef) As PtrDeclare Function layerWithPath Lib “QuartzCore” Selector “layerWithPath:” (classRef As Ptr, path As Ptr) As Ptr

// Get referencesVar winRef As Ptr = mainWindow(sharedApplication(NSClassFromString(“NSApplication”)))Var viewRef As Ptr = contentView(winRef)

// Enable CoreAnimationsetWantsLayer(viewRef, True)Var mainLayer As Ptr = layer(viewRef)

// Make a circle path the size of the windowVar r As CGRectr.x = 0r.y = 0r.width = Self.Widthr.height = Self.Height

Var circlePath As Ptr = CGPathCreateWithEllipseInRect(r, Nil)

// Build a mask layer from that pathVar shapeLayerClass As Ptr = NSClassFromStringCA(“CAShapeLayer”)Var maskLayer As Ptr = layerWithPath(shapeLayerClass, circlePath)

// Apply it as a masksetMask(mainLayer, maskLayer)

=====================

I have created a structure in the program, called CGRect. It is NOT a structure in the window.

I am also using a plain window with no title bar.

Sorry about the formatting but when I try to use the code item… it looks REALLY weird!

Any ideas?

Regards

Compilation ended with these errors:

I suppose your code looks like:

and:

In macOSLib, you will find how to create a CGRect:

Load macOSLib and look at the above entry/entries…

Try…

// Declare what we need
Declare Function NSClassFromString Lib "AppKit" (clsName As CFStringRef) As Ptr
Declare Function contentView Lib "AppKit" Selector "contentView" (windowRef As Ptr) As Ptr
Declare Function CGPathCreateWithEllipseInRect Lib "CoreGraphics" (rect As CGRect, transform As Ptr) As Ptr
Declare Sub setWantsLayer Lib "AppKit" Selector "setWantsLayer:" (view As Ptr, flag As Boolean)
Declare Function layer Lib "AppKit" Selector "layer" (view As Ptr) As Ptr
Declare Sub setMask Lib "QuartzCore" Selector "setMask:" (layerRef As Ptr, maskLayer As Ptr)

// CAShapeLayer specific declares
Declare Function alloc Lib "QuartzCore" Selector "alloc" (classRef As Ptr) As Ptr
Declare Function init Lib "QuartzCore" Selector "init" (ref As Ptr) As Ptr
Declare Sub setPath Lib "QuartzCore" Selector "setPath:" (layerRef As Ptr, path As Ptr)
Declare Sub setBounds Lib "QuartzCore" Selector "setBounds:" (layerRef As Ptr, rect As CGRect)
Declare Sub setFrame Lib "QuartzCore" Selector "setFrame:" (layerRef As Ptr, rect As CGRect)

// Additional needed declares
Declare Function retain Lib "Foundation" Selector "retain" (obj As Ptr) As Ptr

// Window transparency declares
Declare Sub setOpaque Lib "AppKit" Selector "setOpaque:" (windowRef As Ptr, flag As Boolean)
Declare Sub setBackgroundColor Lib "AppKit" Selector "setBackgroundColor:" (windowRef As Ptr, color As Ptr)
Declare Function clearColor Lib "AppKit" Selector "clearColor" (classRef As Ptr) As Ptr

// Get window reference
Var winRef As Ptr = Self.Handle

// Make window transparent
setOpaque(winRef, False)
Var colorClass As Ptr = NSClassFromString("NSColor")
setBackgroundColor(winRef, clearColor(colorClass))

// Get content view and enable layer
Var viewRef As Ptr = contentView(winRef)
setWantsLayer(viewRef, True)
Var mainLayer As Ptr = layer(viewRef)

// Create rectangle for the bounds
Var r As CGRect
r.x = 0
r.y = 0
r.width = Self.Width
r.height = Self.Height

// Create circular path
Var circlePath As Ptr = CGPathCreateWithEllipseInRect(r, Nil)

// Create a CAShapeLayer
Var shapeLayerClass As Ptr = NSClassFromString("CAShapeLayer")
Var maskLayer As Ptr = alloc(shapeLayerClass)
maskLayer = init(maskLayer)
maskLayer = retain(maskLayer)  // Ensure it's retained

// Set the bounds and frame of the mask layer
setBounds(maskLayer, r)
setFrame(maskLayer, r)

// Set the path
setPath(maskLayer, circlePath)

// Apply it as a mask to the main layer
setMask(mainLayer, maskLayer)

Example: Round.xojo_binary_project

I’ll make it a little easier
with position and size changes

my round window-test.xojo_binary_project.zip (4.6 KB)

2 Likes

Odd… I cannot find the declares in this project?

Hi Emile.

I created a structure for CGRect within the window.

Is that wrong?

Regards

Hi Travis.

Thank you for the reply.

I’ll take a look and see what I am doing wrong.

Regards

There aren’t any. It’s a square window with a transparent background color.

@Tim and all:

OK: Long version:

1. I copied the code, pasted it into TextEdit, and set the text to size 14 so it could be read. I edited the text (replaced the curved apostrophes, added Returns where they were missing). I copied the code to the clipboard.

2. I launched Xojo 2025r2.1, opened a project.

3. Pasted the code into a button.

4. Run the project (Error: CGRect: is unknown).

5. I see the error: CGRect is unknown.

6. I open the project “macoslib.xojo_project” (after locating it on my hard drive).

7. I search for CGRect: more than 100 occurrences…

8. I take a screenshot of its position in the project.

9. I provide the necessary information.

If I had known that CGRect was declared in the project, I wouldn’t have wasted my time; I would have gone out to sunbathe.

Sorry for only giving the essentials. Translated by “translate.google” for clarity: my learning English (at school) only lasted 2 years… the rest was accumulated over the last 50 years…

@RudolfJ This is great. I didn’t realize this could be done so simply.