Win32 API 日本語リファレンス
ホームGraphics.Gdi › SetPixel

SetPixel

関数
指定座標のピクセルを指定色で描画し、実際の色を返す。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

COLORREF SetPixel(
    HDC hdc,
    INT x,
    INT y,
    COLORREF color
);

パラメーター

名前方向説明
hdcHDCinデバイスコンテキストへのハンドルです。
xINTin設定する点の x 座標を、論理単位で指定します。
yINTin設定する点の y 座標を、論理単位で指定します。
colorCOLORREFin点を描画するために使用する色です。COLORREF の色値を作成するには、RGB マクロを使用します。

戻り値の型: COLORREF

公式ドキュメント

SetPixel 関数は、指定した座標のピクセルを指定した色に設定します。

戻り値

関数が成功した場合、戻り値は関数がピクセルに設定した RGB 値です。この値は crColor で指定した色と異なる場合があります。これは、指定した色に完全に一致する色が見つからない場合に発生します。

関数が失敗した場合、戻り値は -1 です。

次の値が返されることがあります。

戻り値 説明
ERROR_INVALID_PARAMETER
1 つ以上の入力パラメーターが無効です。

解説(Remarks)

ピクセル座標が現在のクリッピング領域の外側にある場合、関数は失敗します。

すべてのデバイスが SetPixel 関数をサポートしているわけではありません。詳細については、GetDeviceCaps を参照してください。

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

各言語での呼び出し定義

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

COLORREF SetPixel(
    HDC hdc,
    INT x,
    INT y,
    COLORREF color
);
[DllImport("GDI32.dll", ExactSpelling = true)]
static extern uint SetPixel(
    IntPtr hdc,   // HDC
    int x,   // INT
    int y,   // INT
    uint color   // COLORREF
);
<DllImport("GDI32.dll", ExactSpelling:=True)>
Public Shared Function SetPixel(
    hdc As IntPtr,   ' HDC
    x As Integer,   ' INT
    y As Integer,   ' INT
    color As UInteger   ' COLORREF
) As UInteger
End Function
' hdc : HDC
' x : INT
' y : INT
' color : COLORREF
Declare PtrSafe Function SetPixel Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal color As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetPixel = ctypes.windll.gdi32.SetPixel
SetPixel.restype = wintypes.DWORD
SetPixel.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    ctypes.c_int,  # x : INT
    ctypes.c_int,  # y : INT
    wintypes.DWORD,  # color : COLORREF
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
SetPixel = Fiddle::Function.new(
  lib['SetPixel'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    Fiddle::TYPE_INT,  # x : INT
    Fiddle::TYPE_INT,  # y : INT
    -Fiddle::TYPE_INT,  # color : COLORREF
  ],
  -Fiddle::TYPE_INT)
#[link(name = "gdi32")]
extern "system" {
    fn SetPixel(
        hdc: *mut core::ffi::c_void,  // HDC
        x: i32,  // INT
        y: i32,  // INT
        color: u32  // COLORREF
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("GDI32.dll")]
public static extern uint SetPixel(IntPtr hdc, int x, int y, uint color);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_SetPixel' -Namespace Win32 -PassThru
# $api::SetPixel(hdc, x, y, color)
#uselib "GDI32.dll"
#func global SetPixel "SetPixel" sptr, sptr, sptr, sptr
; SetPixel hdc, x, y, color   ; 戻り値は stat
; hdc : HDC -> "sptr"
; x : INT -> "sptr"
; y : INT -> "sptr"
; color : COLORREF -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "GDI32.dll"
#cfunc global SetPixel "SetPixel" sptr, int, int, int
; res = SetPixel(hdc, x, y, color)
; hdc : HDC -> "sptr"
; x : INT -> "int"
; y : INT -> "int"
; color : COLORREF -> "int"
; COLORREF SetPixel(HDC hdc, INT x, INT y, COLORREF color)
#uselib "GDI32.dll"
#cfunc global SetPixel "SetPixel" intptr, int, int, int
; res = SetPixel(hdc, x, y, color)
; hdc : HDC -> "intptr"
; x : INT -> "int"
; y : INT -> "int"
; color : COLORREF -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procSetPixel = gdi32.NewProc("SetPixel")
)

// hdc (HDC), x (INT), y (INT), color (COLORREF)
r1, _, err := procSetPixel.Call(
	uintptr(hdc),
	uintptr(x),
	uintptr(y),
	uintptr(color),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // COLORREF
function SetPixel(
  hdc: THandle;   // HDC
  x: Integer;   // INT
  y: Integer;   // INT
  color: DWORD   // COLORREF
): DWORD; stdcall;
  external 'GDI32.dll' name 'SetPixel';
result := DllCall("GDI32\SetPixel"
    , "Ptr", hdc   ; HDC
    , "Int", x   ; INT
    , "Int", y   ; INT
    , "UInt", color   ; COLORREF
    , "UInt")   ; return: COLORREF
●SetPixel(hdc, x, y, color) = DLL("GDI32.dll", "dword SetPixel(void*, int, int, dword)")
# 呼び出し: SetPixel(hdc, x, y, color)
# hdc : HDC -> "void*"
# x : INT -> "int"
# y : INT -> "int"
# color : COLORREF -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef SetPixelNative = Uint32 Function(Pointer<Void>, Int32, Int32, Uint32);
typedef SetPixelDart = int Function(Pointer<Void>, int, int, int);
final SetPixel = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<SetPixelNative, SetPixelDart>('SetPixel');
// hdc : HDC -> Pointer<Void>
// x : INT -> Int32
// y : INT -> Int32
// color : COLORREF -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetPixel(
  hdc: THandle;   // HDC
  x: Integer;   // INT
  y: Integer;   // INT
  color: DWORD   // COLORREF
): DWORD; stdcall;
  external 'GDI32.dll' name 'SetPixel';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetPixel"
  c_SetPixel :: Ptr () -> Int32 -> Int32 -> Word32 -> IO Word32
-- hdc : HDC -> Ptr ()
-- x : INT -> Int32
-- y : INT -> Int32
-- color : COLORREF -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setpixel =
  foreign "SetPixel"
    ((ptr void) @-> int32_t @-> int32_t @-> uint32_t @-> returning uint32_t)
(* hdc : HDC -> (ptr void) *)
(* x : INT -> int32_t *)
(* y : INT -> int32_t *)
(* color : COLORREF -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library gdi32 (t "GDI32.dll"))
(cffi:use-foreign-library gdi32)

(cffi:defcfun ("SetPixel" set-pixel :convention :stdcall) :uint32
  (hdc :pointer)   ; HDC
  (x :int32)   ; INT
  (y :int32)   ; INT
  (color :uint32))   ; COLORREF
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetPixel = Win32::API::More->new('GDI32',
    'DWORD SetPixel(HANDLE hdc, int x, int y, DWORD color)');
# my $ret = $SetPixel->Call($hdc, $x, $y, $color);
# hdc : HDC -> HANDLE
# x : INT -> int
# y : INT -> int
# color : COLORREF -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目