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

SetTimer

関数
指定間隔で発火するタイマーを作成する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

UINT_PTR SetTimer(
    HWND hWnd,   // optional
    UINT_PTR nIDEvent,
    DWORD uElapse,
    TIMERPROC lpTimerFunc   // optional
);

パラメーター

名前方向
hWndHWNDinoptional
nIDEventUINT_PTRin
uElapseDWORDin
lpTimerFuncTIMERPROCinoptional

戻り値の型: UINT_PTR

各言語での呼び出し定義

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

UINT_PTR SetTimer(
    HWND hWnd,   // optional
    UINT_PTR nIDEvent,
    DWORD uElapse,
    TIMERPROC lpTimerFunc   // optional
);
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern UIntPtr SetTimer(
    IntPtr hWnd,   // HWND optional
    UIntPtr nIDEvent,   // UINT_PTR
    uint uElapse,   // DWORD
    IntPtr lpTimerFunc   // TIMERPROC optional
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetTimer(
    hWnd As IntPtr,   ' HWND optional
    nIDEvent As UIntPtr,   ' UINT_PTR
    uElapse As UInteger,   ' DWORD
    lpTimerFunc As IntPtr   ' TIMERPROC optional
) As UIntPtr
End Function
' hWnd : HWND optional
' nIDEvent : UINT_PTR
' uElapse : DWORD
' lpTimerFunc : TIMERPROC optional
Declare PtrSafe Function SetTimer Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIDEvent As LongPtr, _
    ByVal uElapse As Long, _
    ByVal lpTimerFunc As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetTimer = ctypes.windll.user32.SetTimer
SetTimer.restype = ctypes.c_size_t
SetTimer.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND optional
    ctypes.c_size_t,  # nIDEvent : UINT_PTR
    wintypes.DWORD,  # uElapse : DWORD
    ctypes.c_void_p,  # lpTimerFunc : TIMERPROC optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetTimer = Fiddle::Function.new(
  lib['SetTimer'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND optional
    Fiddle::TYPE_UINTPTR_T,  # nIDEvent : UINT_PTR
    -Fiddle::TYPE_INT,  # uElapse : DWORD
    Fiddle::TYPE_VOIDP,  # lpTimerFunc : TIMERPROC optional
  ],
  Fiddle::TYPE_UINTPTR_T)
#[link(name = "user32")]
extern "system" {
    fn SetTimer(
        hWnd: *mut core::ffi::c_void,  // HWND optional
        nIDEvent: usize,  // UINT_PTR
        uElapse: u32,  // DWORD
        lpTimerFunc: *const core::ffi::c_void  // TIMERPROC optional
    ) -> usize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", SetLastError = true)]
public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, IntPtr lpTimerFunc);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetTimer' -Namespace Win32 -PassThru
# $api::SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc)
#uselib "USER32.dll"
#func global SetTimer "SetTimer" sptr, sptr, sptr, sptr
; SetTimer hWnd, nIDEvent, uElapse, lpTimerFunc   ; 戻り値は stat
; hWnd : HWND optional -> "sptr"
; nIDEvent : UINT_PTR -> "sptr"
; uElapse : DWORD -> "sptr"
; lpTimerFunc : TIMERPROC optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetTimer "SetTimer" sptr, sptr, int, sptr
; res = SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc)
; hWnd : HWND optional -> "sptr"
; nIDEvent : UINT_PTR -> "sptr"
; uElapse : DWORD -> "int"
; lpTimerFunc : TIMERPROC optional -> "sptr"
; UINT_PTR SetTimer(HWND hWnd, UINT_PTR nIDEvent, DWORD uElapse, TIMERPROC lpTimerFunc)
#uselib "USER32.dll"
#cfunc global SetTimer "SetTimer" intptr, intptr, int, intptr
; res = SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc)
; hWnd : HWND optional -> "intptr"
; nIDEvent : UINT_PTR -> "intptr"
; uElapse : DWORD -> "int"
; lpTimerFunc : TIMERPROC optional -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetTimer = user32.NewProc("SetTimer")
)

// hWnd (HWND optional), nIDEvent (UINT_PTR), uElapse (DWORD), lpTimerFunc (TIMERPROC optional)
r1, _, err := procSetTimer.Call(
	uintptr(hWnd),
	uintptr(nIDEvent),
	uintptr(uElapse),
	uintptr(lpTimerFunc),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // UINT_PTR
function SetTimer(
  hWnd: THandle;   // HWND optional
  nIDEvent: NativeUInt;   // UINT_PTR
  uElapse: DWORD;   // DWORD
  lpTimerFunc: Pointer   // TIMERPROC optional
): NativeUInt; stdcall;
  external 'USER32.dll' name 'SetTimer';
result := DllCall("USER32\SetTimer"
    , "Ptr", hWnd   ; HWND optional
    , "UPtr", nIDEvent   ; UINT_PTR
    , "UInt", uElapse   ; DWORD
    , "Ptr", lpTimerFunc   ; TIMERPROC optional
    , "UPtr")   ; return: UINT_PTR
●SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc) = DLL("USER32.dll", "int SetTimer(void*, int, dword, void*)")
# 呼び出し: SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc)
# hWnd : HWND optional -> "void*"
# nIDEvent : UINT_PTR -> "int"
# uElapse : DWORD -> "dword"
# lpTimerFunc : TIMERPROC optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。