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

SetWindowLongW

関数
ウィンドウの属性やスタイル値を設定する(Unicode版)。
DLLUSER32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// USER32.dll  (Unicode / -W)
#include <windows.h>

INT SetWindowLongW(
    HWND hWnd,
    WINDOW_LONG_PTR_INDEX nIndex,
    INT dwNewLong
);

パラメーター

名前方向
hWndHWNDin
nIndexWINDOW_LONG_PTR_INDEXin
dwNewLongINTin

戻り値の型: INT

各言語での呼び出し定義

// USER32.dll  (Unicode / -W)
#include <windows.h>

INT SetWindowLongW(
    HWND hWnd,
    WINDOW_LONG_PTR_INDEX nIndex,
    INT dwNewLong
);
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int SetWindowLongW(
    IntPtr hWnd,   // HWND
    int nIndex,   // WINDOW_LONG_PTR_INDEX
    int dwNewLong   // INT
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWindowLongW(
    hWnd As IntPtr,   ' HWND
    nIndex As Integer,   ' WINDOW_LONG_PTR_INDEX
    dwNewLong As Integer   ' INT
) As Integer
End Function
' hWnd : HWND
' nIndex : WINDOW_LONG_PTR_INDEX
' dwNewLong : INT
Declare PtrSafe Function SetWindowLongW Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetWindowLongW = ctypes.windll.user32.SetWindowLongW
SetWindowLongW.restype = ctypes.c_int
SetWindowLongW.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    ctypes.c_int,  # nIndex : WINDOW_LONG_PTR_INDEX
    ctypes.c_int,  # dwNewLong : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetWindowLongW = Fiddle::Function.new(
  lib['SetWindowLongW'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND
    Fiddle::TYPE_INT,  # nIndex : WINDOW_LONG_PTR_INDEX
    Fiddle::TYPE_INT,  # dwNewLong : INT
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "user32")]
extern "system" {
    fn SetWindowLongW(
        hWnd: *mut core::ffi::c_void,  // HWND
        nIndex: i32,  // WINDOW_LONG_PTR_INDEX
        dwNewLong: i32  // INT
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int SetWindowLongW(IntPtr hWnd, int nIndex, int dwNewLong);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetWindowLongW' -Namespace Win32 -PassThru
# $api::SetWindowLongW(hWnd, nIndex, dwNewLong)
#uselib "USER32.dll"
#func global SetWindowLongW "SetWindowLongW" wptr, wptr, wptr
; SetWindowLongW hWnd, nIndex, dwNewLong   ; 戻り値は stat
; hWnd : HWND -> "wptr"
; nIndex : WINDOW_LONG_PTR_INDEX -> "wptr"
; dwNewLong : INT -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetWindowLongW "SetWindowLongW" sptr, int, int
; res = SetWindowLongW(hWnd, nIndex, dwNewLong)
; hWnd : HWND -> "sptr"
; nIndex : WINDOW_LONG_PTR_INDEX -> "int"
; dwNewLong : INT -> "int"
; INT SetWindowLongW(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, INT dwNewLong)
#uselib "USER32.dll"
#cfunc global SetWindowLongW "SetWindowLongW" intptr, int, int
; res = SetWindowLongW(hWnd, nIndex, dwNewLong)
; hWnd : HWND -> "intptr"
; nIndex : WINDOW_LONG_PTR_INDEX -> "int"
; dwNewLong : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowLongW = user32.NewProc("SetWindowLongW")
)

// hWnd (HWND), nIndex (WINDOW_LONG_PTR_INDEX), dwNewLong (INT)
r1, _, err := procSetWindowLongW.Call(
	uintptr(hWnd),
	uintptr(nIndex),
	uintptr(dwNewLong),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // INT
function SetWindowLongW(
  hWnd: THandle;   // HWND
  nIndex: Integer;   // WINDOW_LONG_PTR_INDEX
  dwNewLong: Integer   // INT
): Integer; stdcall;
  external 'USER32.dll' name 'SetWindowLongW';
result := DllCall("USER32\SetWindowLongW"
    , "Ptr", hWnd   ; HWND
    , "Int", nIndex   ; WINDOW_LONG_PTR_INDEX
    , "Int", dwNewLong   ; INT
    , "Int")   ; return: INT
●SetWindowLongW(hWnd, nIndex, dwNewLong) = DLL("USER32.dll", "int SetWindowLongW(void*, int, int)")
# 呼び出し: SetWindowLongW(hWnd, nIndex, dwNewLong)
# hWnd : HWND -> "void*"
# nIndex : WINDOW_LONG_PTR_INDEX -> "int"
# dwNewLong : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。