Page 1 of 1

Transparent canvas but no events on Windows

Posted: Fri Aug 30, 2024 4:25 pm
by pjsmith67
Bear with me... I develop on MacOS but trying to get one of my modules to work under Windows...

I have the following code to make a canvas transparent in Windows but after doing this, the canvas no longer receives any events.

Code: Select all

      
      Define hwCanvas = GadgetID(canvas)
      Define transparentColor = RGB(255, 255, 255) ;white transparent
      Define opacity = 0
      
      SetWindowLong_(hwCanvas, #GWL_EXSTYLE, GetWindowLong_(hwCanvas, #GWL_EXSTYLE) | #WS_EX_LAYERED)
      SetLayeredWindowAttributes_(hwCanvas, transparentColor, opacity, #LWA_COLORKEY)
Am I missing something? Or am I pretty much stuck here?

Thanks!

Phil

Re: Transparent canvas but no events on Windows

Posted: Mon Sep 02, 2024 12:51 am
by idle
No idea why it doesn't work without example but what is the return of

Code: Select all

SetWindowLong_(hwCanvas, #GWL_EXSTYLE, GetWindowLong_(hwCanvas, #GWL_EXSTYLE) | #WS_EX_LAYERED)

Re: Transparent canvas but no events on Windows

Posted: Mon Sep 02, 2024 3:10 pm
by Axolotl
First, please use

Code: Select all

SetWindowLongPtr_() ; because SetWindowLong_() does not work on 64 bit
See MSDN for further information.

Second,
they say that windows implement the rule that a window that is transparent to the user's eye is transparent to the mouse as well.
This is the best I could make so far......

Code: Select all

SetWindowLongPtr_(hwCanvas, #GWL_EXSTYLE, GetWindowLongPtr_(hwCanvas, #GWL_EXSTYLE) | #WS_EX_LAYERED)
SetLayeredWindowAttributes_(hwCanvas, #White, 100, #LWA_ALPHA)  ; <-- 0 < 100 < 255 


My Test-App (borrowed from Help and modified a little bit)

Code: Select all

Macro SetWindowExStyle(HWnd, ExStyle) 
  SetWindowLongPtr_(HWnd, #GWL_EXSTYLE, GetWindowLongPtr_(HWnd, #GWL_EXSTYLE) | ExStyle) 
EndMacro 


If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 100)
  TextGadget(1, 10, 80, 200, 100, "Text for test"+#LF$+"Second Line of Text "+#LF$+"Third Line of Text ")
    
  SetWindowExStyle(GadgetID(0), #WS_EX_LAYERED) 
  SetLayeredWindowAttributes_(GadgetID(0), #White, 100, #LWA_ALPHA)
  
  Repeat
    Event = WaitWindowEvent()
        
    If Event = #PB_Event_Gadget And EventGadget() = 0 
      If EventType() = #PB_EventType_LeftButtonDown Or (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
        If StartDrawing(CanvasOutput(0))
          x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
          y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
          Circle(x, y, 4, RGB(Random(255), Random(255), Random(255)))
          StopDrawing() 
        EndIf 
      ElseIf EventType() = #PB_EventType_MouseLeave      
        If StartDrawing(CanvasOutput(0))
          Box(0, 0, OutputWidth(), OutputHeight(), #White) 
          StopDrawing()
        EndIf
      EndIf
    EndIf    
  Until Event = #PB_Event_CloseWindow
EndIf