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

SetPixelFormat

関数
デバイスコンテキストにピクセル形式を設定する。
DLLGDI32.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL SetPixelFormat(
    HDC hdc,
    INT format,
    const PIXELFORMATDESCRIPTOR* ppfd
);

パラメーター

名前方向説明
hdcHDCinピクセル形式を設定する対象のデバイスコンテキストハンドル(HDC)。
formatINTin設定するピクセル形式のインデックス番号を示すINT値(1始まり)。
ppfdPIXELFORMATDESCRIPTOR*in設定するピクセル形式を示すPIXELFORMATDESCRIPTORへのポインタ。

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetPixelFormat(
    HDC hdc,
    INT format,
    const PIXELFORMATDESCRIPTOR* ppfd
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool SetPixelFormat(
    IntPtr hdc,   // HDC
    int format,   // INT
    IntPtr ppfd   // PIXELFORMATDESCRIPTOR*
);
<DllImport("GDI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetPixelFormat(
    hdc As IntPtr,   ' HDC
    format As Integer,   ' INT
    ppfd As IntPtr   ' PIXELFORMATDESCRIPTOR*
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hdc : HDC
' format : INT
' ppfd : PIXELFORMATDESCRIPTOR*
Declare PtrSafe Function SetPixelFormat Lib "gdi32" ( _
    ByVal hdc As LongPtr, _
    ByVal format As Long, _
    ByVal ppfd As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetPixelFormat = ctypes.windll.gdi32.SetPixelFormat
SetPixelFormat.restype = wintypes.BOOL
SetPixelFormat.argtypes = [
    wintypes.HANDLE,  # hdc : HDC
    ctypes.c_int,  # format : INT
    ctypes.c_void_p,  # ppfd : PIXELFORMATDESCRIPTOR*
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('GDI32.dll')
SetPixelFormat = Fiddle::Function.new(
  lib['SetPixelFormat'],
  [
    Fiddle::TYPE_VOIDP,  # hdc : HDC
    Fiddle::TYPE_INT,  # format : INT
    Fiddle::TYPE_VOIDP,  # ppfd : PIXELFORMATDESCRIPTOR*
  ],
  Fiddle::TYPE_INT)
#[link(name = "gdi32")]
extern "system" {
    fn SetPixelFormat(
        hdc: *mut core::ffi::c_void,  // HDC
        format: i32,  // INT
        ppfd: *const PIXELFORMATDESCRIPTOR  // PIXELFORMATDESCRIPTOR*
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("GDI32.dll", SetLastError = true)]
public static extern bool SetPixelFormat(IntPtr hdc, int format, IntPtr ppfd);
"@
$api = Add-Type -MemberDefinition $sig -Name 'GDI32_SetPixelFormat' -Namespace Win32 -PassThru
# $api::SetPixelFormat(hdc, format, ppfd)
#uselib "GDI32.dll"
#func global SetPixelFormat "SetPixelFormat" sptr, sptr, sptr
; SetPixelFormat hdc, format, varptr(ppfd)   ; 戻り値は stat
; hdc : HDC -> "sptr"
; format : INT -> "sptr"
; ppfd : PIXELFORMATDESCRIPTOR* -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "GDI32.dll"
#cfunc global SetPixelFormat "SetPixelFormat" sptr, int, var
; res = SetPixelFormat(hdc, format, ppfd)
; hdc : HDC -> "sptr"
; format : INT -> "int"
; ppfd : PIXELFORMATDESCRIPTOR* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetPixelFormat(HDC hdc, INT format, PIXELFORMATDESCRIPTOR* ppfd)
#uselib "GDI32.dll"
#cfunc global SetPixelFormat "SetPixelFormat" intptr, int, var
; res = SetPixelFormat(hdc, format, ppfd)
; hdc : HDC -> "intptr"
; format : INT -> "int"
; ppfd : PIXELFORMATDESCRIPTOR* -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procSetPixelFormat = gdi32.NewProc("SetPixelFormat")
)

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

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

typedef SetPixelFormatNative = Int32 Function(Pointer<Void>, Int32, Pointer<Void>);
typedef SetPixelFormatDart = int Function(Pointer<Void>, int, Pointer<Void>);
final SetPixelFormat = DynamicLibrary.open('GDI32.dll')
    .lookupFunction<SetPixelFormatNative, SetPixelFormatDart>('SetPixelFormat');
// hdc : HDC -> Pointer<Void>
// format : INT -> Int32
// ppfd : PIXELFORMATDESCRIPTOR* -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetPixelFormat(
  hdc: THandle;   // HDC
  format: Integer;   // INT
  ppfd: Pointer   // PIXELFORMATDESCRIPTOR*
): BOOL; stdcall;
  external 'GDI32.dll' name 'SetPixelFormat';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetPixelFormat"
  c_SetPixelFormat :: Ptr () -> Int32 -> Ptr () -> IO CInt
-- hdc : HDC -> Ptr ()
-- format : INT -> Int32
-- ppfd : PIXELFORMATDESCRIPTOR* -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

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

関連項目

使用する型