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

SetWindowTextA

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

シグネチャ

// USER32.dll  (ANSI / -A)
#include <windows.h>

BOOL SetWindowTextA(
    HWND hWnd,
    LPCSTR lpString   // optional
);

パラメーター

名前方向
hWndHWNDin
lpStringLPCSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

// USER32.dll  (ANSI / -A)
#include <windows.h>

BOOL SetWindowTextA(
    HWND hWnd,
    LPCSTR lpString   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool SetWindowTextA(
    IntPtr hWnd,   // HWND
    [MarshalAs(UnmanagedType.LPStr)] string lpString   // LPCSTR optional
);
<DllImport("USER32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetWindowTextA(
    hWnd As IntPtr,   ' HWND
    <MarshalAs(UnmanagedType.LPStr)> lpString As String   ' LPCSTR optional
) As Boolean
End Function
' hWnd : HWND
' lpString : LPCSTR optional
Declare PtrSafe Function SetWindowTextA Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal lpString As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

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

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetWindowTextA = user32.NewProc("SetWindowTextA")
)

// hWnd (HWND), lpString (LPCSTR optional)
r1, _, err := procSetWindowTextA.Call(
	uintptr(hWnd),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpString))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetWindowTextA(
  hWnd: THandle;   // HWND
  lpString: PAnsiChar   // LPCSTR optional
): BOOL; stdcall;
  external 'USER32.dll' name 'SetWindowTextA';
result := DllCall("USER32\SetWindowTextA"
    , "Ptr", hWnd   ; HWND
    , "AStr", lpString   ; LPCSTR optional
    , "Int")   ; return: BOOL
●SetWindowTextA(hWnd, lpString) = DLL("USER32.dll", "bool SetWindowTextA(void*, char*)")
# 呼び出し: SetWindowTextA(hWnd, lpString)
# hWnd : HWND -> "void*"
# lpString : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。