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

CreateCaret

関数
指定ウィンドウに新しいキャレットを作成する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL CreateCaret(
    HWND hWnd,
    HBITMAP hBitmap,   // optional
    INT nWidth,
    INT nHeight
);

パラメーター

名前方向説明
hWndHWNDinキャレットを所有するウィンドウへのハンドルです。
hBitmapHBITMAPinoptional

キャレットの形状を定義するビットマップへのハンドルです。このパラメーターが NULL の場合、キャレットは塗りつぶされた状態になります。このパラメーターが (HBITMAP) 1 の場合、キャレットはグレーになります。このパラメーターがビットマップハンドルの場合、キャレットは指定されたビットマップになります。ビットマップハンドルは、CreateBitmapCreateDIBitmap、または LoadBitmap 関数で作成されたものでなければなりません。 キャレットは XOR 演算によって画面に描画されます。

hBitmap がビットマップハンドルの場合、CreateCaretnWidth および nHeight パラメーターを無視します。ビットマップ自身が幅と高さを定義します。 アプリケーションは、キャレットが破棄されるか別のキャレットに置き換えられるまで、hBitmap を削除してはなりません。

nWidthINTinキャレットの幅(論理単位)です。このパラメーターが 0 の場合、幅はシステム定義のウィンドウ境界幅に設定されます。hBitmap がビットマップハンドルの場合、CreateCaret はこのパラメーターを無視します。
nHeightINTinキャレットの高さ(論理単位)です。このパラメーターが 0 の場合、高さはシステム定義のウィンドウ境界高さに設定されます。hBitmap がビットマップハンドルの場合、CreateCaret はこのパラメーターを無視します。

戻り値の型: BOOL

公式ドキュメント

システムキャレットの新しい形状を作成し、キャレットの所有権を指定したウィンドウに割り当てます。キャレットの形状は、線、ブロック、またはビットマップのいずれかにできます。

戻り値

型: BOOL

関数が成功した場合、戻り値は 0 以外になります。

関数が失敗した場合、戻り値は 0 になります。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

nWidth および nHeight パラメーターは、キャレットの幅と高さを論理単位で指定します。ピクセル単位での正確な幅と高さは、ウィンドウのマッピングモードによって異なります。

CreateCaret は、キャレットを所有するウィンドウにかかわらず、以前のキャレット形状があれば自動的に破棄します。キャレットは、アプリケーションが ShowCaret 関数を呼び出してキャレットを表示するまで非表示のままです。

システムはキューごとに 1 つのキャレットを提供します。ウィンドウは、キーボードフォーカスを持っているとき、またはアクティブなときにのみキャレットを作成すべきです。ウィンドウは、キーボードフォーカスを失う前、または非アクティブになる前にキャレットを破棄すべきです。

DPI 仮想化

この API は DPI 仮想化に参加しません。幅と高さのパラメーターは、対象となるウィンドウに対する論理サイズとして解釈されます。呼び出し元のスレッドは考慮されません。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

CreateCaret = ctypes.windll.user32.CreateCaret
CreateCaret.restype = wintypes.BOOL
CreateCaret.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.HANDLE,  # hBitmap : HBITMAP optional
    ctypes.c_int,  # nWidth : INT
    ctypes.c_int,  # nHeight : INT
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procCreateCaret = user32.NewProc("CreateCaret")
)

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

extern "user32" fn CreateCaret(
    hWnd: ?*anyopaque, // HWND
    hBitmap: ?*anyopaque, // HBITMAP optional
    nWidth: i32, // INT
    nHeight: i32 // INT
) callconv(std.os.windows.WINAPI) i32;
proc CreateCaret(
    hWnd: pointer,  # HWND
    hBitmap: pointer,  # HBITMAP optional
    nWidth: int32,  # INT
    nHeight: int32  # INT
): int32 {.importc: "CreateCaret", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
int CreateCaret(
    void* hWnd,   // HWND
    void* hBitmap,   // HBITMAP optional
    int nWidth,   // INT
    int nHeight   // INT
);
ccall((:CreateCaret, "USER32.dll"), stdcall, Int32,
      (Ptr{Cvoid}, Ptr{Cvoid}, Int32, Int32),
      hWnd, hBitmap, nWidth, nHeight)
# hWnd : HWND -> Ptr{Cvoid}
# hBitmap : HBITMAP optional -> Ptr{Cvoid}
# nWidth : INT -> Int32
# nHeight : INT -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CreateCaret(
    void* hWnd,
    void* hBitmap,
    int32_t nWidth,
    int32_t nHeight);
]]
local user32 = ffi.load("user32")
-- user32.CreateCaret(hWnd, hBitmap, nWidth, nHeight)
-- hWnd : HWND
-- hBitmap : HBITMAP optional
-- nWidth : INT
-- nHeight : INT
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const CreateCaret = lib.func('__stdcall', 'CreateCaret', 'int32_t', ['void *', 'void *', 'int32_t', 'int32_t']);
// CreateCaret(hWnd, hBitmap, nWidth, nHeight)
// hWnd : HWND -> 'void *'
// hBitmap : HBITMAP optional -> 'void *'
// nWidth : INT -> 'int32_t'
// nHeight : INT -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("USER32.dll", {
  CreateCaret: { parameters: ["pointer", "pointer", "i32", "i32"], result: "i32" },
});
// lib.symbols.CreateCaret(hWnd, hBitmap, nWidth, nHeight)
// hWnd : HWND -> "pointer"
// hBitmap : HBITMAP optional -> "pointer"
// nWidth : INT -> "i32"
// nHeight : INT -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CreateCaret(
    void* hWnd,
    void* hBitmap,
    int32_t nWidth,
    int32_t nHeight);
C, "USER32.dll");
// $ffi->CreateCaret(hWnd, hBitmap, nWidth, nHeight);
// hWnd : HWND
// hBitmap : HBITMAP optional
// nWidth : INT
// nHeight : INT
// 構造体/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);
    boolean CreateCaret(
        Pointer hWnd,   // HWND
        Pointer hBitmap,   // HBITMAP optional
        int nWidth,   // INT
        int nHeight   // INT
    );
}
@[Link("user32")]
lib LibUSER32
  fun CreateCaret = CreateCaret(
    hWnd : Void*,   # HWND
    hBitmap : Void*,   # HBITMAP optional
    nWidth : Int32,   # INT
    nHeight : Int32   # INT
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CreateCaretNative = Int32 Function(Pointer<Void>, Pointer<Void>, Int32, Int32);
typedef CreateCaretDart = int Function(Pointer<Void>, Pointer<Void>, int, int);
final CreateCaret = DynamicLibrary.open('USER32.dll')
    .lookupFunction<CreateCaretNative, CreateCaretDart>('CreateCaret');
// hWnd : HWND -> Pointer<Void>
// hBitmap : HBITMAP optional -> Pointer<Void>
// nWidth : INT -> Int32
// nHeight : INT -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CreateCaret(
  hWnd: THandle;   // HWND
  hBitmap: THandle;   // HBITMAP optional
  nWidth: Integer;   // INT
  nHeight: Integer   // INT
): BOOL; stdcall;
  external 'USER32.dll' name 'CreateCaret';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CreateCaret"
  c_CreateCaret :: Ptr () -> Ptr () -> Int32 -> Int32 -> IO CInt
-- hWnd : HWND -> Ptr ()
-- hBitmap : HBITMAP optional -> Ptr ()
-- nWidth : INT -> Int32
-- nHeight : INT -> Int32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let createcaret =
  foreign "CreateCaret"
    ((ptr void) @-> (ptr void) @-> int32_t @-> int32_t @-> returning int32_t)
(* hWnd : HWND -> (ptr void) *)
(* hBitmap : HBITMAP optional -> (ptr void) *)
(* nWidth : INT -> int32_t *)
(* nHeight : INT -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("CreateCaret" create-caret :convention :stdcall) :int32
  (h-wnd :pointer)   ; HWND
  (h-bitmap :pointer)   ; HBITMAP optional
  (n-width :int32)   ; INT
  (n-height :int32))   ; INT
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateCaret = Win32::API::More->new('USER32',
    'BOOL CreateCaret(HANDLE hWnd, HANDLE hBitmap, int nWidth, int nHeight)');
# my $ret = $CreateCaret->Call($hWnd, $hBitmap, $nWidth, $nHeight);
# hWnd : HWND -> HANDLE
# hBitmap : HBITMAP optional -> HANDLE
# nWidth : INT -> int
# nHeight : INT -> int
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目