# SetWindowPos Function

`Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long`

SetWindowPos moves a window to a new location. Its physical coordinates, dimensions, and Z-order position (the Z-order determines which windows are on top of others) can be set. The function returns 0 if an error occured or a non-zero value if successful.

hwnd
  The handle of the window to move.
hWndInsertAfter
  Either the handle of the window to position this window behind, or one of the insert after flags stating where in the Z-order to put the window.
x
  The x coordinate of the upper-left corner of the window.
y
  The y coordinate of the upper-left corner of the window.
cx
  The x coordinate of the lower-right corner of the window.
cy
  The y coordinate of the lower-right corner of the window.
wFlags
  Zero or more of the SetWindowPos flags stating how to move the window.

Example:

    ' Move Form1 to the upper-left corner of the screen
    '  and above any other windows
    flags = SWP_NOSIZE Or SWP_DRAWFRAME  ' don't resize, but redraw window
    ' (the cx and cy parameters are ignored because of SWP_NOSIZE)
    x = SetWindowPos(Form1.hWnd, HWND_TOPMOST, 0, 0, 0, 0, flags)

Related Call: GetWindowRect
Category: Windows
Back to the index.