Moving or Resizing a Window or Control

Native LOCATE Command

Liberty BASIC includes a "LOCATE" command for moving and resizing controls. This command only works on controls and it only works in windows of type "WINDOW."
nomainwin
button #main.b, "Move Me",[moveMe],UL,10,10,100,30
open "Move a Button" for window as #main
#main "trapclose [quit]"
wait
 
[quit] close #main:end
 
[moveMe]
#main.b "!locate 50 100 200 50"
#main.b "I've been moved!"
#main "refresh"
wait
When the "LOCATE" command is sent to a control that can accept a new label or text, we must precede the command with a ! character as we did above, or the command string is simply displayed on the control and not acted on. Note the difference in the two lines below. The first line is a command that instructs the button to change its size and location. The second line changes the caption on the button.

#main.b "!locate 50 100 200 50"
#main.b "I've been moved!"

Don't forget to issue a "REFRESH" command to cause the window display to be updated after moving a control.

MoveWindow API

We can move and resize any window or control with the MoveWindow API call. The coordinates for the x and y locations of a window are relative to the upper left corner of the desktop. If the API call is used to move or resize a control, the coordinates are relative to the upper left corner of the window's workspace, which is called the Client Area.

Liberty BASIC does not currently have a native function for moving or resizing an open window. It also does not allow us to move or resize controls contained in windows that are not of type "WINDOW." We must use an API call to change the location and size of windows and of controls not in "WINDOW" type windows. \
To move or resize a window, use the handle of the window obtained with the HWND() function.

    hMain = HWND(#main)
 
    calldll #user32, "MoveWindow",_
    hMain as ulong, _       'window handle
    10 as long,_            'x location of window
    20 as long,_            'y location of window
    730 as long,_           'desired width of window
    590 as long,_           'desired height of window
    1 as long,_             'repaint flag,0=false,1=true
    ret as long             'nonzero=success

To move or resize a control, use the handle of the control obtained with the HWND() function.

hButton = HWND(#main.b)
 
calldll #user32, "MoveWindow",_
    hButton as ulong, _     'control handle
    50 as long,_            'x location of control
    150 as long,_            'y location of control
    200 as long,_           'desired width of control
    50 as long,_            'desired height of control
    1 as long,_             'repaint flag,0=false,1=true
    ret as long             'nonzero=success
 

Demo

nomainwin
button #main.b, "Move Me",[moveMe],UL,10,10,100,30
button #main.move, "Move Window",[moveWindow],UL,120,10,100,30
open "Move a Button" for window as #main
#main "trapclose [quit]"
wait
 
[quit] close #main:end
 
[moveMe]
#main.b "I've been moved!"
 
hButton = HWND(#main.b)
 
calldll #user32, "MoveWindow",_
    hButton as ulong, _     'control handle
    50 as long,_            'x location of control
    150 as long,_           'y location of control
    200 as long,_           'desired width of control
    50 as long,_            'desired height of control
    1 as long,_             'repaint flag,0=false,1=true
    ret as long             'nonzero=success
wait
 
[moveWindow]
    hMain = HWND(#main)
 
    calldll #user32, "MoveWindow",_
    hMain as ulong, _       'window handle
    10 as long,_            'x location of window
    20 as long,_            'y location of window
    730 as long,_           'desired width of window
    590 as long,_           'desired height of window
    1 as long,_             'repaint flag,0=false,1=true
    ret as long             'nonzero=success
 
    'we'll only move window once, then disable button 
    #main.move "!disable" 
 
 
wait