Mac-os transparent canvas gadget

Share your advanced PureBasic knowledge/code with the community.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Mac-os transparent canvas gadget

Post by mestnyi »

Code: Select all

Procedure TransparentCanvas( canvas )
   If StartDrawing(CanvasOutput(canvas))
      FillMemory( DrawingBuffer(), DrawingBufferPitch() * OutputHeight())
      StopDrawing()
   EndIf
EndProcedure

If OpenWindow(0, 0, 0, 220, 220, "Canvas container example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   
   SetWindowColor(0, $FFC8ECF0)
   
   CanvasGadget(0, 10, 10, 200, 200, #PB_Canvas_Container)
   ButtonGadget(1, 10, 10, 80, 30, "Clean up")
   CloseGadgetList()
   
   TransparentCanvas(0)
   
   Repeat
      Event = WaitWindowEvent()
      
      If Event = #PB_Event_Gadget
         Select EventGadget() 
            Case 0
               If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
                  If StartDrawing(CanvasOutput(0))
                     DrawingMode(#PB_2DDrawing_AllChannels)
                     x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
                     y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
                     Circle(x, y, 10, RGBA(Random(255), Random(255), Random(255), Random(255)))
                     StopDrawing()
                  EndIf
               EndIf
               
            Case 1
               TransparentCanvas(0)
               
         EndSelect
      EndIf
      
   Until Event = #PB_Event_CloseWindow
EndIf

for windows

Code: Select all

Procedure Clip( gadget )
   GadgetID = GadgetID(gadget)
   SetWindowLongPtr_( GadgetID, #GWL_STYLE, GetWindowLongPtr_( GadgetID, #GWL_STYLE ) | #WS_CLIPSIBLINGS | #WS_CLIPCHILDREN )
   SetWindowPos_( GadgetID, #HWND_TOP, 0,0,0,0, #SWP_NOMOVE | #SWP_NOSIZE )
EndProcedure

Procedure TransparentCanvas( canvas )
   RedrawWindow_(GetAncestor_( GadgetID(canvas), #GA_ROOT ), #Null, #Null, #RDW_INVALIDATE | #RDW_ERASENOW | #RDW_ALLCHILDREN | #RDW_UPDATENOW)
EndProcedure

If OpenWindow(0, 0, 0, 220, 220, "Canvas container example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   CanvasGadget(0, 0, 0, 220, 220)
   ;Clip(0)
   SetWindowLongPtr_(GadgetID(0), #GWL_EXSTYLE, GetWindowLongPtr_(GadgetID(0), #GWL_EXSTYLE) | #WS_EX_LAYERED) 
   SetLayeredWindowAttributes_(GadgetID(0), #White, 1, #LWA_ALPHA)
   
   ;\\
   gadget = TextGadget(-1, 0, 0, 220, 220, "")
   Clip(gadget)
   
   ;SetWindowColor(0, $C8ECF0)
   SetGadgetColor(gadget, #PB_Gadget_BackColor, $C8ECF0)
   
   ButtonGadget(1, 10, 10, 80, 30, "Clean up")
   Clip(1)
   
   TransparentCanvas(0)
   
   Repeat
      Event = WaitWindowEvent()
      
      If Event = #PB_Event_Gadget
         Select EventGadget() 
            Case 0
               If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
                  
                  If StartDrawing(WindowOutput(0))
                     DrawingMode(#PB_2DDrawing_AllChannels)
                     x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
                     y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
                     Circle(x, y, 10, RGBA(Random(255), Random(255), Random(255), Random(255)))
                     StopDrawing()
                  EndIf
                  
               EndIf
               
            Case 1
               TransparentCanvas(0)
               
         EndSelect
      EndIf
      
   Until Event = #PB_Event_CloseWindow
EndIf
Last edited by mestnyi on Tue Oct 08, 2024 8:33 pm, edited 2 times in total.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Mac-os transparent canvas gadget

Post by Piero »

What OS do you have? Doesn't work in Monterey (I tested M1 and Intel)
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: Mac-os transparent canvas gadget

Post by mestnyi »

mac os BIG SUR
Post Reply