Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › SetWindowPos

SetWindowPos

関数
ウィンドウの位置やサイズやZ順序を変更する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// USER32.dll
#include <windows.h>

BOOL SetWindowPos(
    HWND hWnd,
    HWND hWndInsertAfter,   // optional
    INT X,
    INT Y,
    INT cx,
    INT cy,
    SET_WINDOW_POS_FLAGS uFlags
);

パラメーター

名前方向
hWndHWNDin
hWndInsertAfterHWNDinoptional
XINTin
YINTin
cxINTin
cyINTin
uFlagsSET_WINDOW_POS_FLAGSin

戻り値の型: BOOL

各言語での呼び出し定義

// USER32.dll
#include <windows.h>

BOOL SetWindowPos(
    HWND hWnd,
    HWND hWndInsertAfter,   // optional
    INT X,
    INT Y,
    INT cx,
    INT cy,
    SET_WINDOW_POS_FLAGS uFlags
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetWindowPos(
    IntPtr hWnd,   // HWND
    IntPtr hWndInsertAfter,   // HWND optional
    int X,   // INT
    int Y,   // INT
    int cx,   // INT
    int cy,   // INT
    uint uFlags   // SET_WINDOW_POS_FLAGS
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWindowPos(
    hWnd As IntPtr,   ' HWND
    hWndInsertAfter As IntPtr,   ' HWND optional
    X As Integer,   ' INT
    Y As Integer,   ' INT
    cx As Integer,   ' INT
    cy As Integer,   ' INT
    uFlags As UInteger   ' SET_WINDOW_POS_FLAGS
) As Boolean
End Function
' hWnd : HWND
' hWndInsertAfter : HWND optional
' X : INT
' Y : INT
' cx : INT
' cy : INT
' uFlags : SET_WINDOW_POS_FLAGS
Declare PtrSafe Function SetWindowPos Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal hWndInsertAfter As LongPtr, _
    ByVal X As Long, _
    ByVal Y As Long, _
    ByVal cx As Long, _
    ByVal cy As Long, _
    ByVal uFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWindowPos = ctypes.windll.user32.SetWindowPos
SetWindowPos.restype = wintypes.BOOL
SetWindowPos.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.HANDLE,  # hWndInsertAfter : HWND optional
    ctypes.c_int,  # X : INT
    ctypes.c_int,  # Y : INT
    ctypes.c_int,  # cx : INT
    ctypes.c_int,  # cy : INT
    wintypes.DWORD,  # uFlags : SET_WINDOW_POS_FLAGS
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetWindowPos = Fiddle::Function.new(
  lib['SetWindowPos'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    Fiddle::TYPE_VOIDP,  # hWndInsertAfter : HWND optional
    Fiddle::TYPE_INT,  # X : INT
    Fiddle::TYPE_INT,  # Y : INT
    Fiddle::TYPE_INT,  # cx : INT
    Fiddle::TYPE_INT,  # cy : INT
    -Fiddle::TYPE_INT,  # uFlags : SET_WINDOW_POS_FLAGS
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetWindowPos(
        hWnd: *mut core::ffi::c_void,  // HWND
        hWndInsertAfter: *mut core::ffi::c_void,  // HWND optional
        X: i32,  // INT
        Y: i32,  // INT
        cx: i32,  // INT
        cy: i32,  // INT
        uFlags: u32  // SET_WINDOW_POS_FLAGS
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWindowPos' -Namespace Win32 -PassThru
# $api::SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
#uselib "USER32.dll"
#func global SetWindowPos "SetWindowPos" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SetWindowPos hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags   ; 戻り値は stat
; hWnd : HWND -> "sptr"
; hWndInsertAfter : HWND optional -> "sptr"
; X : INT -> "sptr"
; Y : INT -> "sptr"
; cx : INT -> "sptr"
; cy : INT -> "sptr"
; uFlags : SET_WINDOW_POS_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetWindowPos "SetWindowPos" sptr, sptr, int, int, int, int, int
; res = SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
; hWnd : HWND -> "sptr"
; hWndInsertAfter : HWND optional -> "sptr"
; X : INT -> "int"
; Y : INT -> "int"
; cx : INT -> "int"
; cy : INT -> "int"
; uFlags : SET_WINDOW_POS_FLAGS -> "int"
; BOOL SetWindowPos(HWND hWnd, HWND hWndInsertAfter, INT X, INT Y, INT cx, INT cy, SET_WINDOW_POS_FLAGS uFlags)
#uselib "USER32.dll"
#cfunc global SetWindowPos "SetWindowPos" intptr, intptr, int, int, int, int, int
; res = SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
; hWnd : HWND -> "intptr"
; hWndInsertAfter : HWND optional -> "intptr"
; X : INT -> "int"
; Y : INT -> "int"
; cx : INT -> "int"
; cy : INT -> "int"
; uFlags : SET_WINDOW_POS_FLAGS -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowPos = user32.NewProc("SetWindowPos")
)

// hWnd (HWND), hWndInsertAfter (HWND optional), X (INT), Y (INT), cx (INT), cy (INT), uFlags (SET_WINDOW_POS_FLAGS)
r1, _, err := procSetWindowPos.Call(
	uintptr(hWnd),
	uintptr(hWndInsertAfter),
	uintptr(X),
	uintptr(Y),
	uintptr(cx),
	uintptr(cy),
	uintptr(uFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetWindowPos(
  hWnd: THandle;   // HWND
  hWndInsertAfter: THandle;   // HWND optional
  X: Integer;   // INT
  Y: Integer;   // INT
  cx: Integer;   // INT
  cy: Integer;   // INT
  uFlags: DWORD   // SET_WINDOW_POS_FLAGS
): BOOL; stdcall;
  external 'USER32.dll' name 'SetWindowPos';
result := DllCall("USER32\SetWindowPos"
    , "Ptr", hWnd   ; HWND
    , "Ptr", hWndInsertAfter   ; HWND optional
    , "Int", X   ; INT
    , "Int", Y   ; INT
    , "Int", cx   ; INT
    , "Int", cy   ; INT
    , "UInt", uFlags   ; SET_WINDOW_POS_FLAGS
    , "Int")   ; return: BOOL
●SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags) = DLL("USER32.dll", "bool SetWindowPos(void*, void*, int, int, int, int, dword)")
# 呼び出し: SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
# hWnd : HWND -> "void*"
# hWndInsertAfter : HWND optional -> "void*"
# X : INT -> "int"
# Y : INT -> "int"
# cx : INT -> "int"
# cy : INT -> "int"
# uFlags : SET_WINDOW_POS_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。