Page 1 of 1

Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 6:12 am
by mestnyi
Can you confirm that this is a bug in windows?
In the compiler settings with DPI enabled.

Code: Select all

Procedure   Canvas(gadget)
   Protected color = RGB( Random(255), Random(255), Random(255) )
   CanvasGadget(gadget, 0, 0, 200, 200)    
   StartDrawing(CanvasOutput(gadget))
   DrawingMode(#PB_2DDrawing_Default)
   Box(0,0,OutputWidth(), OutputHeight(), color)
   DrawText(10,10, Str(gadget))
   StopDrawing()
EndProcedure

OpenWindow(1, 110, 110, 200, 200, "", #PB_Window_BorderLess)
Canvas(1)    

Repeat 
   event = WaitWindowEvent()
   If event = #PB_Event_Gadget
      If EventType() = #PB_EventType_LeftButtonDown
         mouse_x = DesktopMouseX()
         ResizeWindow(EventWindow(), mouse_x, #PB_Ignore, #PB_Ignore, #PB_Ignore)
         Debug "mouse_x = "+mouse_x +" gadget_x = "+ GadgetX(EventGadget(), #PB_Gadget_ScreenCoordinate)
         
      EndIf
   EndIf
Until event = #PB_Event_CloseWindow

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 7:13 am
by mestnyi
here is another example.

Code: Select all

Procedure   Canvas(gadget)
   Protected color = RGB( Random(255), Random(255), Random(255) )
   CanvasGadget(gadget, 0, 0, 200, 200)    
   StartDrawing(CanvasOutput(gadget))
   DrawingMode(#PB_2DDrawing_Default)
   Box(0,0,OutputWidth(), OutputHeight(), color)
   DrawText(10,10, Str(gadget))
   StopDrawing()
EndProcedure

OpenWindow(1, 0, 0, 500, 500, "", #PB_Window_BorderLess)
Canvas(1)  

Global _dpiScaleFactorX.d
Global _dpiScaleFactorY.d

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
   _dpiScaleFactorX = GetDeviceCaps_(GetDC_(0),#LOGPIXELSX) / 96
   _dpiScaleFactorY = GetDeviceCaps_(GetDC_(0),#LOGPIXELSY) / 96
CompilerEndIf

Repeat 
   event = WaitWindowEvent()
   If event = #PB_Event_Gadget
      If EventType() = #PB_EventType_LeftButtonDown
         mouse_x = DesktopMouseX()
         ResizeGadget(EventGadget(), mouse_x / _dpiScaleFactorX, #PB_Ignore, #PB_Ignore, #PB_Ignore)
         Debug "mouse_x = "+mouse_x +" gadget_x = "+ Str(GadgetX(EventGadget(), #PB_Gadget_ScreenCoordinate) * _dpiScaleFactorX)
         GetWindowRect_(GadgetID(EventGadget()), @rc.RECT)
         Debug rc\left
      EndIf
   EndIf
Until event = #PB_Event_CloseWindow

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 9:57 am
by TI-994A
mestnyi wrote: Sat Sep 28, 2024 6:12 amCan you confirm that this is a bug in windows?
What bug?

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 11:05 am
by mestnyi
TI-994A wrote: Sat Sep 28, 2024 9:57 am
mestnyi wrote: Sat Sep 28, 2024 6:12 amCan you confirm that this is a bug in windows?
What bug?
When you click on the canvas, the window moves to the mouse position and we get a different value in the debugger window.
That is, shouldn't the mouse position and the canvas position be the same?

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 11:45 am
by TI-994A
mestnyi wrote: Sat Sep 28, 2024 11:05 am...shouldn't the mouse position and the canvas position be the same?
I'm getting the same values on Windows 8.1 and 10, running PureBasic v5.73 and v6.12, with and without the DPI aware option, and regardless of the display scaling.

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 11:58 am
by mestnyi
TI-994A wrote: Sat Sep 28, 2024 11:45 am
mestnyi wrote: Sat Sep 28, 2024 11:05 am...shouldn't the mouse position and the canvas position be the same?
I'm getting the same values on Windows 8.1 and 10, running PureBasic v5.73 and v6.12, with and without the DPI aware option, and regardless of the display scaling.
Interestingly, however, I use windows 11 and purebasic 5.73 Scale 125

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 2:45 pm
by TI-994A
mestnyi wrote: Sat Sep 28, 2024 11:58 am...I use windows 11 and purebasic 6.12 Scale 125
Same results with Windows 11 running PureBasic v6.12, scaled to 125%.

Re: Get gadget (x&y) with DPI

Posted: Sat Sep 28, 2024 3:49 pm
by mestnyi
TI-994A wrote: Sat Sep 28, 2024 2:45 pm
mestnyi wrote: Sat Sep 28, 2024 11:58 am...I use windows 11 and purebasic 6.12 Scale 125
Same results with Windows 11 running PureBasic v6.12, scaled to 125%.
How can this be?
What can you pay attention to, then what can it be related to?
Can anyone else check?

Re: Get gadget (x&y) with DPI

Posted: Sun Sep 29, 2024 10:11 am
by breeze4me
Just in case, check the compatibility section of the PB IDE icon on your desktop.
viewtopic.php?p=620932&hilit=properties#p620932

Re: Get gadget (x&y) with DPI

Posted: Thu Oct 03, 2024 12:22 pm
by mestnyi
It turns out that I got something mixed up, in fact there was a bug in purebasic 5.73, fixed in purebasic 6.12.
Here is a more correct example, when clicking in the window, the canvas should move to the mouse position.

Code: Select all

Procedure   Canvas(gadget)
   Protected color = RGB( Random(255), Random(255), Random(255) )
   CanvasGadget(gadget, 0, 0, 200, 200)    
   StartDrawing(CanvasOutput(gadget))
   DrawingMode(#PB_2DDrawing_Default)
   Box(0,0,OutputWidth(), OutputHeight(), color)
   DrawText(10,10, Str(gadget))
   StopDrawing()
EndProcedure

OpenWindow(1, 0, 0, 500, 100, "", #PB_Window_BorderLess|#PB_Window_ScreenCentered)
Canvas(1)  

Repeat 
   event = WaitWindowEvent()
   If event = #PB_Event_LeftClick
      mouse_x = DesktopMouseX()
      
      ; ResizeGadget(1, WindowMouseX(EventWindow()), #PB_Ignore, #PB_Ignore, #PB_Ignore)
      ResizeGadget(1, DesktopUnscaledX(WindowMouseX(EventWindow())), #PB_Ignore, #PB_Ignore, #PB_Ignore)
      
      GetWindowRect_(GadgetID(1), @rc.RECT)
      Debug "mouse_x = "+mouse_x +" gadget_x = "+ Str(GadgetX(1, #PB_Gadget_ScreenCoordinate) ) +" - "+ rc\left
   EndIf
Until event = #PB_Event_CloseWindow