SetWindowPos
関数シグネチャ
// 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
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| hWnd | HWND | in | ウィンドウへのハンドルです。 | ||||||||||||||||||||||||||||||||
| hWndInsertAfter | HWND | inoptional | Z オーダーにおいて、配置するウィンドウの直前に位置するウィンドウへのハンドルです。このパラメーターには、ウィンドウハンドルか、次のいずれかの値を指定する必要があります。
このパラメーターの使用方法の詳細については、後述の「解説」セクションを参照してください。 | ||||||||||||||||||||||||||||||||
| X | INT | in | ウィンドウの左端の新しい位置です(クライアント座標)。 | ||||||||||||||||||||||||||||||||
| Y | INT | in | ウィンドウの上端の新しい位置です(クライアント座標)。 | ||||||||||||||||||||||||||||||||
| cx | INT | in | ウィンドウの新しい幅です(ピクセル単位)。 | ||||||||||||||||||||||||||||||||
| cy | INT | in | ウィンドウの新しい高さです(ピクセル単位)。 | ||||||||||||||||||||||||||||||||
| uFlags | SET_WINDOW_POS_FLAGS | in | ウィンドウのサイズ変更と位置指定に関するフラグです。このパラメーターには、次の値の組み合わせを指定できます。
|
戻り値の型: BOOL
公式ドキュメント
子ウィンドウ、ポップアップウィンドウ、またはトップレベルウィンドウのサイズ、位置、および Z オーダーを変更します。これらのウィンドウは画面上での表示順序に従って並びます。最前面のウィンドウが最も高い順位を持ち、Z オーダーの先頭に位置します。
戻り値
解説(Remarks)
Vista の再設計の一環として、すべてのサービスは対話型デスクトップから Session 0 へ移されました。hwnd およびウィンドウマネージャーの操作はセッション内でのみ有効であり、セッションをまたいで hwnd を操作しようとすると失敗します。詳細については、The Windows Vista Developer Story: Application Compatibility Cookbook を参照してください。
SetWindowLong を使用して特定のウィンドウデータを変更した場合、その変更を有効にするには SetWindowPos を呼び出す必要があります。uFlags には次の組み合わせを使用します: SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED。
ウィンドウを最前面ウィンドウにするには、hWndInsertAfter パラメーターを HWND_TOPMOST に設定し、かつ SWP_NOZORDER フラグが設定されていないことを確認するか、または既存のすべての最前面ウィンドウの上になるようにウィンドウの Z オーダー内の位置を設定します。非最前面ウィンドウが最前面にされると、そのウィンドウが所有するウィンドウも最前面になります。ただし、そのオーナーは変更されません。
SWP_NOACTIVATE フラグも SWP_NOZORDER フラグも指定されていない場合(つまり、アプリケーションがウィンドウのアクティブ化と Z オーダー内の位置変更を同時に要求している場合)、hWndInsertAfter で指定された値は次の状況においてのみ使用されます。
- hWndInsertAfter に HWND_TOPMOST フラグも HWND_NOTOPMOST フラグも指定されていない場合。
- hWnd で識別されるウィンドウがアクティブウィンドウではない場合。
最前面ウィンドウが Z オーダーの最下部(HWND_BOTTOM)または任意の非最前面ウィンドウの後ろに再配置されると、そのウィンドウはもはや最前面ではなくなります。最前面ウィンドウが非最前面にされると、そのオーナーおよびそのウィンドウが所有するウィンドウも非最前面ウィンドウになります。
非最前面ウィンドウは最前面ウィンドウを所有できますが、その逆はできません。最前面ウィンドウが所有する任意のウィンドウ(たとえばダイアログボックス)は、それ自体が最前面ウィンドウになります。これは、所有されるすべてのウィンドウが必ずそのオーナーの上に表示されるようにするためです。
アプリケーションがフォアグラウンドにないにもかかわらずフォアグラウンドにあるべき場合は、SetForegroundWindow 関数を呼び出す必要があります。
SetWindowPos を使用してウィンドウを最上部に表示するには、そのウィンドウを所有するプロセスが SetForegroundWindow の権限を持っている必要があります。
例
例については、Initializing a Dialog Box を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 <MarshalAs(UnmanagedType.Bool)> 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 // BOOLfunction 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)。const std = @import("std");
extern "user32" fn SetWindowPos(
hWnd: ?*anyopaque, // HWND
hWndInsertAfter: ?*anyopaque, // HWND optional
X: i32, // INT
Y: i32, // INT
cx: i32, // INT
cy: i32, // INT
uFlags: u32 // SET_WINDOW_POS_FLAGS
) callconv(std.os.windows.WINAPI) i32;proc SetWindowPos(
hWnd: pointer, # HWND
hWndInsertAfter: pointer, # HWND optional
X: int32, # INT
Y: int32, # INT
cx: int32, # INT
cy: int32, # INT
uFlags: uint32 # SET_WINDOW_POS_FLAGS
): int32 {.importc: "SetWindowPos", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
int SetWindowPos(
void* hWnd, // HWND
void* hWndInsertAfter, // HWND optional
int X, // INT
int Y, // INT
int cx, // INT
int cy, // INT
uint uFlags // SET_WINDOW_POS_FLAGS
);ccall((:SetWindowPos, "USER32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Cvoid}, Int32, Int32, Int32, Int32, UInt32),
hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
# hWnd : HWND -> Ptr{Cvoid}
# hWndInsertAfter : HWND optional -> Ptr{Cvoid}
# X : INT -> Int32
# Y : INT -> Int32
# cx : INT -> Int32
# cy : INT -> Int32
# uFlags : SET_WINDOW_POS_FLAGS -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetWindowPos(
void* hWnd,
void* hWndInsertAfter,
int32_t X,
int32_t Y,
int32_t cx,
int32_t cy,
uint32_t uFlags);
]]
local user32 = ffi.load("user32")
-- user32.SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
-- hWnd : HWND
-- hWndInsertAfter : HWND optional
-- X : INT
-- Y : INT
-- cx : INT
-- cy : INT
-- uFlags : SET_WINDOW_POS_FLAGS
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetWindowPos = lib.func('__stdcall', 'SetWindowPos', 'int32_t', ['void *', 'void *', 'int32_t', 'int32_t', 'int32_t', 'int32_t', 'uint32_t']);
// SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
// hWnd : HWND -> 'void *'
// hWndInsertAfter : HWND optional -> 'void *'
// X : INT -> 'int32_t'
// Y : INT -> 'int32_t'
// cx : INT -> 'int32_t'
// cy : INT -> 'int32_t'
// uFlags : SET_WINDOW_POS_FLAGS -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
SetWindowPos: { parameters: ["pointer", "pointer", "i32", "i32", "i32", "i32", "u32"], result: "i32" },
});
// lib.symbols.SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
// hWnd : HWND -> "pointer"
// hWndInsertAfter : HWND optional -> "pointer"
// X : INT -> "i32"
// Y : INT -> "i32"
// cx : INT -> "i32"
// cy : INT -> "i32"
// uFlags : SET_WINDOW_POS_FLAGS -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetWindowPos(
void* hWnd,
void* hWndInsertAfter,
int32_t X,
int32_t Y,
int32_t cx,
int32_t cy,
uint32_t uFlags);
C, "USER32.dll");
// $ffi->SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
// hWnd : HWND
// hWndInsertAfter : HWND optional
// X : INT
// Y : INT
// cx : INT
// cy : INT
// uFlags : SET_WINDOW_POS_FLAGS
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface User32 extends StdCallLibrary {
User32 INSTANCE = Native.load("user32", User32.class);
boolean SetWindowPos(
Pointer hWnd, // HWND
Pointer hWndInsertAfter, // HWND optional
int X, // INT
int Y, // INT
int cx, // INT
int cy, // INT
int uFlags // SET_WINDOW_POS_FLAGS
);
}@[Link("user32")]
lib LibUSER32
fun SetWindowPos = SetWindowPos(
hWnd : Void*, # HWND
hWndInsertAfter : Void*, # HWND optional
X : Int32, # INT
Y : Int32, # INT
cx : Int32, # INT
cy : Int32, # INT
uFlags : UInt32 # SET_WINDOW_POS_FLAGS
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetWindowPosNative = Int32 Function(Pointer<Void>, Pointer<Void>, Int32, Int32, Int32, Int32, Uint32);
typedef SetWindowPosDart = int Function(Pointer<Void>, Pointer<Void>, int, int, int, int, int);
final SetWindowPos = DynamicLibrary.open('USER32.dll')
.lookupFunction<SetWindowPosNative, SetWindowPosDart>('SetWindowPos');
// hWnd : HWND -> Pointer<Void>
// hWndInsertAfter : HWND optional -> Pointer<Void>
// X : INT -> Int32
// Y : INT -> Int32
// cx : INT -> Int32
// cy : INT -> Int32
// uFlags : SET_WINDOW_POS_FLAGS -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetWindowPos"
c_SetWindowPos :: Ptr () -> Ptr () -> Int32 -> Int32 -> Int32 -> Int32 -> Word32 -> IO CInt
-- hWnd : HWND -> Ptr ()
-- hWndInsertAfter : HWND optional -> Ptr ()
-- X : INT -> Int32
-- Y : INT -> Int32
-- cx : INT -> Int32
-- cy : INT -> Int32
-- uFlags : SET_WINDOW_POS_FLAGS -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setwindowpos =
foreign "SetWindowPos"
((ptr void) @-> (ptr void) @-> int32_t @-> int32_t @-> int32_t @-> int32_t @-> uint32_t @-> returning int32_t)
(* hWnd : HWND -> (ptr void) *)
(* hWndInsertAfter : HWND optional -> (ptr void) *)
(* X : INT -> int32_t *)
(* Y : INT -> int32_t *)
(* cx : INT -> int32_t *)
(* cy : INT -> int32_t *)
(* uFlags : SET_WINDOW_POS_FLAGS -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("SetWindowPos" set-window-pos :convention :stdcall) :int32
(h-wnd :pointer) ; HWND
(h-wnd-insert-after :pointer) ; HWND optional
(x :int32) ; INT
(y :int32) ; INT
(cx :int32) ; INT
(cy :int32) ; INT
(u-flags :uint32)) ; SET_WINDOW_POS_FLAGS
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetWindowPos = Win32::API::More->new('USER32',
'BOOL SetWindowPos(HANDLE hWnd, HANDLE hWndInsertAfter, int X, int Y, int cx, int cy, DWORD uFlags)');
# my $ret = $SetWindowPos->Call($hWnd, $hWndInsertAfter, $X, $Y, $cx, $cy, $uFlags);
# hWnd : HWND -> HANDLE
# hWndInsertAfter : HWND optional -> HANDLE
# X : INT -> int
# Y : INT -> int
# cx : INT -> int
# cy : INT -> int
# uFlags : SET_WINDOW_POS_FLAGS -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f MoveWindow — ウィンドウの位置とサイズを変更する。
- f SetActiveWindow — 指定したウィンドウをアクティブにする。
- f SetForegroundWindow — 指定ウィンドウを前面化しアクティブにする。