Transparent canvas but no events on Windows

Just starting out? Need help? Post your questions and find answers here.
pjsmith67
User
User
Posts: 48
Joined: Thu Apr 26, 2018 3:09 pm

Transparent canvas but no events on Windows

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Transparent canvas but no events on Windows

Post 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)
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Transparent canvas but no events on Windows

Post 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
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply