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

SetCursorPos

関数
カーソルを指定の画面座標へ移動する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetCursorPos(
    INT X,
    INT Y
);

パラメーター

名前方向
XINTin
YINTin

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetCursorPos(
    INT X,
    INT Y
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetCursorPos(
    int X,   // INT
    int Y   // INT
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetCursorPos(
    X As Integer,   ' INT
    Y As Integer   ' INT
) As Boolean
End Function
' X : INT
' Y : INT
Declare PtrSafe Function SetCursorPos Lib "user32" ( _
    ByVal X As Long, _
    ByVal Y As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetCursorPos = ctypes.windll.user32.SetCursorPos
SetCursorPos.restype = wintypes.BOOL
SetCursorPos.argtypes = [
    ctypes.c_int,  # X : INT
    ctypes.c_int,  # Y : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetCursorPos = Fiddle::Function.new(
  lib['SetCursorPos'],
  [
    Fiddle::TYPE_INT,  # X : INT
    Fiddle::TYPE_INT,  # Y : INT
  ],
  Fiddle::TYPE_INT)
#[link(name = "user32")]
extern "system" {
    fn SetCursorPos(
        X: i32,  // INT
        Y: i32  // INT
    ) -> 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 SetCursorPos(int X, int Y);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetCursorPos' -Namespace Win32 -PassThru
# $api::SetCursorPos(X, Y)
#uselib "USER32.dll"
#func global SetCursorPos "SetCursorPos" sptr, sptr
; SetCursorPos X, Y   ; 戻り値は stat
; X : INT -> "sptr"
; Y : INT -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetCursorPos "SetCursorPos" int, int
; res = SetCursorPos(X, Y)
; X : INT -> "int"
; Y : INT -> "int"
; BOOL SetCursorPos(INT X, INT Y)
#uselib "USER32.dll"
#cfunc global SetCursorPos "SetCursorPos" int, int
; res = SetCursorPos(X, Y)
; X : INT -> "int"
; Y : INT -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetCursorPos = user32.NewProc("SetCursorPos")
)

// X (INT), Y (INT)
r1, _, err := procSetCursorPos.Call(
	uintptr(X),
	uintptr(Y),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetCursorPos(
  X: Integer;   // INT
  Y: Integer   // INT
): BOOL; stdcall;
  external 'USER32.dll' name 'SetCursorPos';
result := DllCall("USER32\SetCursorPos"
    , "Int", X   ; INT
    , "Int", Y   ; INT
    , "Int")   ; return: BOOL
●SetCursorPos(X, Y) = DLL("USER32.dll", "bool SetCursorPos(int, int)")
# 呼び出し: SetCursorPos(X, Y)
# X : INT -> "int"
# Y : INT -> "int"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。