Page 1 of 1

ResizeGadget() with DPI Aware Compiler Option

Posted: Wed Mar 22, 2023 6:43 pm
by urland523
The following snippet works as expected with DPI Aware option unchecked. With DPI checked ResizeGadget() does not seem to be working properly.

Tested with Windows 10, 11
PB 6.00 and 6.01.

Code: Select all

If OpenWindow(0,0,0,800,600,"",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   ButtonGadget(0,20,20,80,20,"Test")
   While WaitWindowEvent() <> #PB_Event_CloseWindow
      ResizeGadget(0,WindowMouseX(0),WindowMouseY(0),#PB_Ignore,#PB_Ignore)
   Wend
EndIf
End

Re: ResizeGadget() with DPI Aware Compiler Option

Posted: Wed Mar 22, 2023 7:01 pm
by Mindphazer
I see no difference, no matter if DPI aware if checked or not. The behaviour is the same, the Test button follows the mouse.
What do you mean by "doesn't seem to be working properly" ?

Re: ResizeGadget() with DPI Aware Compiler Option

Posted: Wed Mar 22, 2023 7:31 pm
by mk-soft
Welcome ...

Your Event Management is wrong.
Here a base code with resize gadgets.

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButtonOk
  #MainButtonCancel
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx - 10, dy - 45)
  ResizeGadget(#MainButtonok, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonok, 10, dy - 35, 120, 30, "Ok")
    ButtonGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30, "Abbruch")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButtonOk
              ;
            Case #MainButtonCancel
              ;
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: ResizeGadget() with DPI Aware Compiler Option

Posted: Wed Mar 22, 2023 8:30 pm
by urland523
Apologies, guess it works as meant to. The gadget floats out away from the mouse pointer as I move the pointer down in the window on my monitor. 4K set at 150% scaling. Thank you for the replies.

Re: ResizeGadget() with DPI Aware Compiler Option

Posted: Wed Mar 22, 2023 8:59 pm
by mk-soft
No bug, but the DPI scale functions must be used.

Example Update:

Code: Select all

Procedure GetHandleUnderMouse()
  Protected handle
  GetCursorPos_(@pt)
  handle = WindowFromPoint_(pt)
  ProcedureReturn handle
EndProcedure

Macro DpiWindowMouseX(Window)
  DesktopUnscaledX(WindowMouseX(Window))
EndMacro

Macro DpiWindowMouseY(Window)
  DesktopUnscaledY(WindowMouseY(Window))
EndMacro

If OpenWindow(0,0,0,800,600,"Only Windows! Move button with left mouse!",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  hButton0 = ButtonGadget(0,20,20,120,25,"Button 0")
  hButton1 = ButtonGadget(1,20,60,120,25,"Button 1")
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            
          Case 1
            
        EndSelect
        
      Case #WM_LBUTTONDOWN ; Window API Constant
        Select GetHandleUnderMouse()
          Case hButton0
            movebutton0 = #True
            dx = DpiWindowMouseX(0) - GadgetX(0)
            dy = DpiWindowMouseY(0) - GadgetY(0)
          Case hButton1
            movebutton1 = #True
            dx = DpiWindowMouseX(0) - GadgetX(1)
            dy = DpiWindowMouseY(0) - GadgetY(1)
        EndSelect    
        
      Case #WM_LBUTTONUP ; Window API Constant
        movebutton0 = #False
        movebutton1 = #False
      
      Case #WM_MOUSEMOVE ; Window API Constant
        If movebutton0
          ResizeGadget(0, DpiWindowMouseX(0) - dx, DpiWindowMouseY(0) - dy, #PB_Ignore,#PB_Ignore)
        ElseIf movebutton1
          ResizeGadget(1, DpiWindowMouseX(0) - dx, DpiWindowMouseY(0) - dy, #PB_Ignore,#PB_Ignore)
        EndIf
        
    EndSelect
  ForEver
EndIf
End

Re: ResizeGadget() with DPI Aware Compiler Option

Posted: Wed Mar 22, 2023 9:00 pm
by AZJIO
urland523
You calculate the DPI, then you multiply the coordinates by the DPI. Didn't check for mouse, but when drawing on buttons or canvas, you just apply this factor and everything is drawn correctly. I use this method in several of my programs (Fifteen, CalendarAlarm, Balloons, Launcher). They are in my signature.

Code: Select all

Global DPI.d = 1
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Define hDC0 = GetWindowDC_(0)
DPI.d = GetDeviceCaps_(hDC0, #LOGPIXELSY)/96
ReleaseDC_(0, hDC0)
CompilerEndIf

WindowMouseX(0) * DPI