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

CreateCursor

関数
ANDマスクとXORマスクから新しいカーソルを作成する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

HCURSOR CreateCursor(
    HINSTANCE hInst,   // optional
    INT xHotSpot,
    INT yHotSpot,
    INT nWidth,
    INT nHeight,
    const void* pvANDPlane,
    const void* pvXORPlane
);

パラメーター

名前方向説明
hInstHINSTANCEinoptionalカーソルを作成するアプリケーションの現在のインスタンスへのハンドルです。
xHotSpotINTinカーソルのホットスポットの水平位置です。
yHotSpotINTinカーソルのホットスポットの垂直位置です。
nWidthINTinカーソルの幅 (ピクセル単位) です。
nHeightINTinカーソルの高さ (ピクセル単位) です。
pvANDPlanevoid*inモノクロビットマップと同様に、カーソルの AND マスクのビット値を格納するバイト配列です。「解説」を参照してください。
pvXORPlanevoid*inモノクロビットマップと同様に、カーソルの XOR マスクのビット値を格納するバイト配列です。「解説」を参照してください。

戻り値の型: HCURSOR

公式ドキュメント

指定したサイズ、ビットパターン、ホットスポットを持つカーソルを作成します。

戻り値

型: HCURSOR

関数が成功した場合、戻り値はカーソルへのハンドルです。

関数が失敗した場合、戻り値は NULL です。詳細なエラー情報を取得するには、GetLastError を呼び出してください。

解説(Remarks)

カーソルの公称サイズを判定するには、GetSystemMetrics 関数に SM_CXCURSOR または SM_CYCURSOR の値を指定して使用します。また、この API の DPI 対応バージョンとして GetSystemMetricsForDpi を使用できます。詳細については、High DPI Desktop Application Development on Windows を参照してください。

pvANDPlane および pvXORPlane パラメーターの詳細については、CreateBitmap 関数の lpBits パラメーターの説明を参照してください。

CreateCursor は、AND および XOR ビットマスクに次の真理値表を適用します:

AND ビットマスク XOR ビットマスク 表示
0 0
0 1
1 0 画面
1 1 画面の反転

アプリケーションを終了する前に、カーソルに関連付けられたシステムリソースを解放するために DestroyCursor 関数を呼び出す必要があります。

DPI 仮想化

この API は DPI 仮想化に参加しません。返される出力は物理座標で表され、呼び出し元スレッドの DPI による影響を受けません。ただし、作成されたカーソルは、描画される各ウィンドウの DPI に合わせてスケーリングされる場合があります。

例については、Creating a Cursor を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

HCURSOR CreateCursor(
    HINSTANCE hInst,   // optional
    INT xHotSpot,
    INT yHotSpot,
    INT nWidth,
    INT nHeight,
    const void* pvANDPlane,
    const void* pvXORPlane
);
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr CreateCursor(
    IntPtr hInst,   // HINSTANCE optional
    int xHotSpot,   // INT
    int yHotSpot,   // INT
    int nWidth,   // INT
    int nHeight,   // INT
    IntPtr pvANDPlane,   // void*
    IntPtr pvXORPlane   // void*
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateCursor(
    hInst As IntPtr,   ' HINSTANCE optional
    xHotSpot As Integer,   ' INT
    yHotSpot As Integer,   ' INT
    nWidth As Integer,   ' INT
    nHeight As Integer,   ' INT
    pvANDPlane As IntPtr,   ' void*
    pvXORPlane As IntPtr   ' void*
) As IntPtr
End Function
' hInst : HINSTANCE optional
' xHotSpot : INT
' yHotSpot : INT
' nWidth : INT
' nHeight : INT
' pvANDPlane : void*
' pvXORPlane : void*
Declare PtrSafe Function CreateCursor Lib "user32" ( _
    ByVal hInst As LongPtr, _
    ByVal xHotSpot As Long, _
    ByVal yHotSpot As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal pvANDPlane As LongPtr, _
    ByVal pvXORPlane As LongPtr) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateCursor = ctypes.windll.user32.CreateCursor
CreateCursor.restype = ctypes.c_void_p
CreateCursor.argtypes = [
    wintypes.HANDLE,  # hInst : HINSTANCE optional
    ctypes.c_int,  # xHotSpot : INT
    ctypes.c_int,  # yHotSpot : INT
    ctypes.c_int,  # nWidth : INT
    ctypes.c_int,  # nHeight : INT
    ctypes.POINTER(None),  # pvANDPlane : void*
    ctypes.POINTER(None),  # pvXORPlane : void*
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
CreateCursor = Fiddle::Function.new(
  lib['CreateCursor'],
  [
    Fiddle::TYPE_VOIDP,  # hInst : HINSTANCE optional
    Fiddle::TYPE_INT,  # xHotSpot : INT
    Fiddle::TYPE_INT,  # yHotSpot : INT
    Fiddle::TYPE_INT,  # nWidth : INT
    Fiddle::TYPE_INT,  # nHeight : INT
    Fiddle::TYPE_VOIDP,  # pvANDPlane : void*
    Fiddle::TYPE_VOIDP,  # pvXORPlane : void*
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "user32")]
extern "system" {
    fn CreateCursor(
        hInst: *mut core::ffi::c_void,  // HINSTANCE optional
        xHotSpot: i32,  // INT
        yHotSpot: i32,  // INT
        nWidth: i32,  // INT
        nHeight: i32,  // INT
        pvANDPlane: *const (),  // void*
        pvXORPlane: *const ()  // void*
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", SetLastError = true)]
public static extern IntPtr CreateCursor(IntPtr hInst, int xHotSpot, int yHotSpot, int nWidth, int nHeight, IntPtr pvANDPlane, IntPtr pvXORPlane);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_CreateCursor' -Namespace Win32 -PassThru
# $api::CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
#uselib "USER32.dll"
#func global CreateCursor "CreateCursor" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CreateCursor hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane   ; 戻り値は stat
; hInst : HINSTANCE optional -> "sptr"
; xHotSpot : INT -> "sptr"
; yHotSpot : INT -> "sptr"
; nWidth : INT -> "sptr"
; nHeight : INT -> "sptr"
; pvANDPlane : void* -> "sptr"
; pvXORPlane : void* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global CreateCursor "CreateCursor" sptr, int, int, int, int, sptr, sptr
; res = CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
; hInst : HINSTANCE optional -> "sptr"
; xHotSpot : INT -> "int"
; yHotSpot : INT -> "int"
; nWidth : INT -> "int"
; nHeight : INT -> "int"
; pvANDPlane : void* -> "sptr"
; pvXORPlane : void* -> "sptr"
; HCURSOR CreateCursor(HINSTANCE hInst, INT xHotSpot, INT yHotSpot, INT nWidth, INT nHeight, void* pvANDPlane, void* pvXORPlane)
#uselib "USER32.dll"
#cfunc global CreateCursor "CreateCursor" intptr, int, int, int, int, intptr, intptr
; res = CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
; hInst : HINSTANCE optional -> "intptr"
; xHotSpot : INT -> "int"
; yHotSpot : INT -> "int"
; nWidth : INT -> "int"
; nHeight : INT -> "int"
; pvANDPlane : void* -> "intptr"
; pvXORPlane : void* -> "intptr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procCreateCursor = user32.NewProc("CreateCursor")
)

// hInst (HINSTANCE optional), xHotSpot (INT), yHotSpot (INT), nWidth (INT), nHeight (INT), pvANDPlane (void*), pvXORPlane (void*)
r1, _, err := procCreateCursor.Call(
	uintptr(hInst),
	uintptr(xHotSpot),
	uintptr(yHotSpot),
	uintptr(nWidth),
	uintptr(nHeight),
	uintptr(pvANDPlane),
	uintptr(pvXORPlane),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HCURSOR
function CreateCursor(
  hInst: THandle;   // HINSTANCE optional
  xHotSpot: Integer;   // INT
  yHotSpot: Integer;   // INT
  nWidth: Integer;   // INT
  nHeight: Integer;   // INT
  pvANDPlane: Pointer;   // void*
  pvXORPlane: Pointer   // void*
): THandle; stdcall;
  external 'USER32.dll' name 'CreateCursor';
result := DllCall("USER32\CreateCursor"
    , "Ptr", hInst   ; HINSTANCE optional
    , "Int", xHotSpot   ; INT
    , "Int", yHotSpot   ; INT
    , "Int", nWidth   ; INT
    , "Int", nHeight   ; INT
    , "Ptr", pvANDPlane   ; void*
    , "Ptr", pvXORPlane   ; void*
    , "Ptr")   ; return: HCURSOR
●CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane) = DLL("USER32.dll", "void* CreateCursor(void*, int, int, int, int, void*, void*)")
# 呼び出し: CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
# hInst : HINSTANCE optional -> "void*"
# xHotSpot : INT -> "int"
# yHotSpot : INT -> "int"
# nWidth : INT -> "int"
# nHeight : INT -> "int"
# pvANDPlane : void* -> "void*"
# pvXORPlane : void* -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "user32" fn CreateCursor(
    hInst: ?*anyopaque, // HINSTANCE optional
    xHotSpot: i32, // INT
    yHotSpot: i32, // INT
    nWidth: i32, // INT
    nHeight: i32, // INT
    pvANDPlane: ?*anyopaque, // void*
    pvXORPlane: ?*anyopaque // void*
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc CreateCursor(
    hInst: pointer,  # HINSTANCE optional
    xHotSpot: int32,  # INT
    yHotSpot: int32,  # INT
    nWidth: int32,  # INT
    nHeight: int32,  # INT
    pvANDPlane: pointer,  # void*
    pvXORPlane: pointer  # void*
): pointer {.importc: "CreateCursor", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
void* CreateCursor(
    void* hInst,   // HINSTANCE optional
    int xHotSpot,   // INT
    int yHotSpot,   // INT
    int nWidth,   // INT
    int nHeight,   // INT
    void* pvANDPlane,   // void*
    void* pvXORPlane   // void*
);
ccall((:CreateCursor, "USER32.dll"), stdcall, Ptr{Cvoid},
      (Ptr{Cvoid}, Int32, Int32, Int32, Int32, Ptr{Cvoid}, Ptr{Cvoid}),
      hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
# hInst : HINSTANCE optional -> Ptr{Cvoid}
# xHotSpot : INT -> Int32
# yHotSpot : INT -> Int32
# nWidth : INT -> Int32
# nHeight : INT -> Int32
# pvANDPlane : void* -> Ptr{Cvoid}
# pvXORPlane : void* -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* CreateCursor(
    void* hInst,
    int32_t xHotSpot,
    int32_t yHotSpot,
    int32_t nWidth,
    int32_t nHeight,
    void* pvANDPlane,
    void* pvXORPlane);
]]
local user32 = ffi.load("user32")
-- user32.CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
-- hInst : HINSTANCE optional
-- xHotSpot : INT
-- yHotSpot : INT
-- nWidth : INT
-- nHeight : INT
-- pvANDPlane : void*
-- pvXORPlane : void*
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const CreateCursor = lib.func('__stdcall', 'CreateCursor', 'void *', ['void *', 'int32_t', 'int32_t', 'int32_t', 'int32_t', 'void *', 'void *']);
// CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
// hInst : HINSTANCE optional -> 'void *'
// xHotSpot : INT -> 'int32_t'
// yHotSpot : INT -> 'int32_t'
// nWidth : INT -> 'int32_t'
// nHeight : INT -> 'int32_t'
// pvANDPlane : void* -> 'void *'
// pvXORPlane : void* -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  CreateCursor: { parameters: ["pointer", "i32", "i32", "i32", "i32", "pointer", "pointer"], result: "pointer" },
});
// lib.symbols.CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane)
// hInst : HINSTANCE optional -> "pointer"
// xHotSpot : INT -> "i32"
// yHotSpot : INT -> "i32"
// nWidth : INT -> "i32"
// nHeight : INT -> "i32"
// pvANDPlane : void* -> "pointer"
// pvXORPlane : void* -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* CreateCursor(
    void* hInst,
    int32_t xHotSpot,
    int32_t yHotSpot,
    int32_t nWidth,
    int32_t nHeight,
    void* pvANDPlane,
    void* pvXORPlane);
C, "USER32.dll");
// $ffi->CreateCursor(hInst, xHotSpot, yHotSpot, nWidth, nHeight, pvANDPlane, pvXORPlane);
// hInst : HINSTANCE optional
// xHotSpot : INT
// yHotSpot : INT
// nWidth : INT
// nHeight : INT
// pvANDPlane : void*
// pvXORPlane : void*
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface User32 extends StdCallLibrary {
    User32 INSTANCE = Native.load("user32", User32.class);
    Pointer CreateCursor(
        Pointer hInst,   // HINSTANCE optional
        int xHotSpot,   // INT
        int yHotSpot,   // INT
        int nWidth,   // INT
        int nHeight,   // INT
        Pointer pvANDPlane,   // void*
        Pointer pvXORPlane   // void*
    );
}
@[Link("user32")]
lib LibUSER32
  fun CreateCursor = CreateCursor(
    hInst : Void*,   # HINSTANCE optional
    xHotSpot : Int32,   # INT
    yHotSpot : Int32,   # INT
    nWidth : Int32,   # INT
    nHeight : Int32,   # INT
    pvANDPlane : Void*,   # void*
    pvXORPlane : Void*   # void*
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CreateCursorNative = Pointer<Void> Function(Pointer<Void>, Int32, Int32, Int32, Int32, Pointer<Void>, Pointer<Void>);
typedef CreateCursorDart = Pointer<Void> Function(Pointer<Void>, int, int, int, int, Pointer<Void>, Pointer<Void>);
final CreateCursor = DynamicLibrary.open('USER32.dll')
    .lookupFunction<CreateCursorNative, CreateCursorDart>('CreateCursor');
// hInst : HINSTANCE optional -> Pointer<Void>
// xHotSpot : INT -> Int32
// yHotSpot : INT -> Int32
// nWidth : INT -> Int32
// nHeight : INT -> Int32
// pvANDPlane : void* -> Pointer<Void>
// pvXORPlane : void* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateCursor(
  hInst: THandle;   // HINSTANCE optional
  xHotSpot: Integer;   // INT
  yHotSpot: Integer;   // INT
  nWidth: Integer;   // INT
  nHeight: Integer;   // INT
  pvANDPlane: Pointer;   // void*
  pvXORPlane: Pointer   // void*
): THandle; stdcall;
  external 'USER32.dll' name 'CreateCursor';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateCursor"
  c_CreateCursor :: Ptr () -> Int32 -> Int32 -> Int32 -> Int32 -> Ptr () -> Ptr () -> IO (Ptr ())
-- hInst : HINSTANCE optional -> Ptr ()
-- xHotSpot : INT -> Int32
-- yHotSpot : INT -> Int32
-- nWidth : INT -> Int32
-- nHeight : INT -> Int32
-- pvANDPlane : void* -> Ptr ()
-- pvXORPlane : void* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createcursor =
  foreign "CreateCursor"
    ((ptr void) @-> int32_t @-> int32_t @-> int32_t @-> int32_t @-> (ptr void) @-> (ptr void) @-> returning (ptr void))
(* hInst : HINSTANCE optional -> (ptr void) *)
(* xHotSpot : INT -> int32_t *)
(* yHotSpot : INT -> int32_t *)
(* nWidth : INT -> int32_t *)
(* nHeight : INT -> int32_t *)
(* pvANDPlane : void* -> (ptr void) *)
(* pvXORPlane : void* -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("CreateCursor" create-cursor :convention :stdcall) :pointer
  (h-inst :pointer)   ; HINSTANCE optional
  (x-hot-spot :int32)   ; INT
  (y-hot-spot :int32)   ; INT
  (n-width :int32)   ; INT
  (n-height :int32)   ; INT
  (pv-andplane :pointer)   ; void*
  (pv-xorplane :pointer))   ; void*
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateCursor = Win32::API::More->new('USER32',
    'HANDLE CreateCursor(HANDLE hInst, int xHotSpot, int yHotSpot, int nWidth, int nHeight, LPVOID pvANDPlane, LPVOID pvXORPlane)');
# my $ret = $CreateCursor->Call($hInst, $xHotSpot, $yHotSpot, $nWidth, $nHeight, $pvANDPlane, $pvXORPlane);
# hInst : HINSTANCE optional -> HANDLE
# xHotSpot : INT -> int
# yHotSpot : INT -> int
# nWidth : INT -> int
# nHeight : INT -> int
# pvANDPlane : void* -> LPVOID
# pvXORPlane : void* -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。