Announcement

Collapse
No announcement yet.

Move/Resize Window Using Keyboard

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Move/Resize Window Using Keyboard

    In some of my apps I use the code below to let my users move/resize the window using the keyboard. Some of my users have trouble dragging the mouse, but can use the keyboard well enough. Ctl-Arrow key to resize. Ctl-Shft-Array key to move.

    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    %Unicode=1
    #Include "win32api.inc"
    
    Enum Equates Singular
       IDM_Left = 500
       IDM_Right
       IDM_Up
       IDM_Down
       IDM_MLeft
       IDM_MRight
       IDM_MUp
       IDM_MDown
    End Enum
    
    Global hDlg As Dword
    
    Function PBMain() As Long
       Dialog New Pixels, 0, "Move/Resize Dialog",,,400,400, %WS_OverlappedWindow To hDlg
       Dialog Show Modal hDlg Call DlgProc
    End Function
    
    CallBack Function DlgProc() As Long
       Select Case Cb.Msg
          Case %WM_InitDialog
             BuildAcceleratorTable
          Case %WM_Command
             Select Case Cb.Ctl
                Case %IDM_Left   : ResizeDialog(-20,0)
                Case %IDM_Right  : ResizeDialog(20,0)
                Case %IDM_Up     : ResizeDialog(0,-20)
                Case %IDM_Down   : ResizeDialog(0,20)
    
                Case %IDM_MLeft  : MoveDialog(-20,0)
                Case %IDM_MRight : MoveDialog(20,0)
                Case %IDM_MUp    : MoveDialog(0,-20)
                Case %IDM_MDown  : MoveDialog(0,20)
             End Select
       End Select
    End Function
    
    Sub BuildAcceleratorTable
       Local c As Long, ac() As ACCELAPI, hAccelerator As Dword
       Dim ac(7)
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL : ac(c).key   = %VK_Left   : ac(c).cmd   = %IDM_Left                  : Incr c   '0
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL : ac(c).key   = %VK_Right  : ac(c).cmd   = %IDM_Right                 : Incr c   '1
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL : ac(c).key   = %VK_Up     : ac(c).cmd   = %IDM_Up                    : Incr c   '2
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL : ac(c).key   = %VK_Down   : ac(c).cmd   = %IDM_Down                  : Incr c   '3
    
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL Or %FSHIFT : ac(c).key   = %VK_Left   : ac(c).cmd   = %IDM_MLeft      : Incr c   '4
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL Or %FSHIFT : ac(c).key   = %VK_Right  : ac(c).cmd   = %IDM_MRight     : Incr c   '5
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL Or %FSHIFT : ac(c).key   = %VK_Up     : ac(c).cmd   = %IDM_MUp        : Incr c   '6
       ac(c).fvirt = %FVIRTKEY Or %FCONTROL Or %FSHIFT : ac(c).key   = %VK_Down   : ac(c).cmd   = %IDM_MDown      : Incr c   '7
       Accel Attach hDlg, AC() To hAccelerator
    End Sub
    
    Sub ResizeDialog(x As Long, y As Long)
       Local w,h As Long
       Dialog Get Size hDlg To w,h
       If w+x < 300 Then Exit Sub   '300 is min width
       If h+y < 300 Then Exit Sub   '300 is min height
       Dialog Set Size hDlg, w+x,h+y
    End Sub
    
    Sub MoveDialog(ww As Long, hh As Long)
       Local x,y,w,h As Long
       Desktop Get Client To w,h
       Dialog Get Loc hDlg To x,y
       If x+ww > w Then Exit Sub
       If x+ww < 0 Then Exit Sub
       If y+hh < 0 Then Exit Sub
       If y+hh > h Then Exit Sub
       Dialog Set Loc hDlg, ww+x,hh+y
    End Sub
Working...
X