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

SetWindowTextW

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

シグネチャ

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

BOOL SetWindowTextW(
    HWND hWnd,
    LPCWSTR lpString   // optional
);

パラメーター

名前方向
hWndHWNDin
lpStringLPCWSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetWindowTextW(
    HWND hWnd,
    LPCWSTR lpString   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetWindowTextW(
    IntPtr hWnd,   // HWND
    [MarshalAs(UnmanagedType.LPWStr)] string lpString   // LPCWSTR optional
);
<DllImport("USER32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWindowTextW(
    hWnd As IntPtr,   ' HWND
    <MarshalAs(UnmanagedType.LPWStr)> lpString As String   ' LPCWSTR optional
) As Boolean
End Function
' hWnd : HWND
' lpString : LPCWSTR optional
Declare PtrSafe Function SetWindowTextW Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal lpString As LongPtr) 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

SetWindowTextW = ctypes.windll.user32.SetWindowTextW
SetWindowTextW.restype = wintypes.BOOL
SetWindowTextW.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.LPCWSTR,  # lpString : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowTextW = user32.NewProc("SetWindowTextW")
)

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