Get gadget (x&y) with DPI

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Get gadget (x&y) with DPI

Post 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
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Get gadget (x&y) with DPI

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Get gadget (x&y) with DPI

Post by TI-994A »

mestnyi wrote: Sat Sep 28, 2024 6:12 amCan you confirm that this is a bug in windows?
What bug?
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Get gadget (x&y) with DPI

Post 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?
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Get gadget (x&y) with DPI

Post 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.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Get gadget (x&y) with DPI

Post 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
Last edited by mestnyi on Thu Oct 03, 2024 12:09 pm, edited 2 times in total.
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Get gadget (x&y) with DPI

Post 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%.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Get gadget (x&y) with DPI

Post 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?
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Get gadget (x&y) with DPI

Post by breeze4me »

Just in case, check the compatibility section of the PB IDE icon on your desktop.
viewtopic.php?p=620932&hilit=properties#p620932
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Get gadget (x&y) with DPI

Post 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
Post Reply