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

CreateHatchBrush

関数
指定したハッチパターンと色のブラシを作成する。
DLLGDI32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

HBRUSH CreateHatchBrush(
    HATCH_BRUSH_STYLE iHatch,
    COLORREF color
);

パラメーター

名前方向説明
iHatchHATCH_BRUSH_STYLEin

ブラシのハッチスタイル。このパラメーターには、次のいずれかの値を指定できます。

意味
HS_BDIAGONAL
左下から右上へ向かう 45 度のハッチ
HS_CROSS
水平方向と垂直方向のクロスハッチ
HS_DIAGCROSS
45 度のクロスハッチ
HS_FDIAGONAL
左上から右下へ向かう 45 度のハッチ
HS_HORIZONTAL
水平方向のハッチ
HS_VERTICAL
垂直方向のハッチ
colorCOLORREFinハッチに使用されるブラシの前景色。COLORREF の色値を作成するには、RGB マクロを使用します。

戻り値の型: HBRUSH

公式ドキュメント

CreateHatchBrush 関数は、指定されたハッチパターンと色を持つ論理ブラシを作成します。

戻り値

関数が成功した場合、戻り値は論理ブラシを識別します。

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

解説(Remarks)

ブラシは、塗りつぶされた図形の内部を描画するためにシステムが使用するビットマップです。

アプリケーションは CreateHatchBrush を呼び出してブラシを作成した後、SelectObject 関数を呼び出すことで、そのブラシを任意のデバイスコンテキストに選択できます。また、SetBkMode を呼び出してブラシの描画に影響を与えることもできます。

アプリケーションがハッチブラシを使用して、親ウィンドウと子ウィンドウの両方の背景を同じ色で塗りつぶす場合は、子ウィンドウの背景を描画する前にブラシの原点を設定する必要があります。これは SetBrushOrgEx 関数を呼び出すことで行えます。アプリケーションは GetBrushOrgEx 関数を呼び出すことで、現在のブラシの原点を取得できます。

ブラシが不要になったら、DeleteObject 関数を呼び出して削除します。

ICM: ブラシの作成時には色は定義されません。ただし、ICM 対応のデバイスコンテキストにブラシが選択されると、カラーマネジメントが実行されます。

次の例では、指定されたハッチパターンと色を持つ論理ブラシを作成します。ハッチブラシの背景を透過または不透過に設定することもできます。


#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stddef.h>


#include <gdiplus.h>
#include <assert.h>

using namespace Gdiplus;

// Reference to the GDI+ static library).
#pragma comment (lib,"Gdiplus.lib")

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");


// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Application Hatch Brush");

HINSTANCE hInst;

#define BTN_MYBUTTON_ID_1    503
#define BTN_MYBUTTON_ID_2    504


// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    UNREFERENCED_PARAMETER(lpCmdLine);
    UNREFERENCED_PARAMETER(hPrevInstance);

    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application does not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        1000, 500,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // Create button controls.
    CreateWindowEx(NULL, L"BUTTON", L"Transparent", WS_VISIBLE | WS_CHILD,
        35, 35, 120, 20, hWnd, (HMENU)BTN_MYBUTTON_ID_1, NULL, NULL);

    CreateWindowEx(NULL, L"BUTTON", L"Opaque", WS_VISIBLE | WS_CHILD,
        35, 65, 120, 20, hWnd, (HMENU)BTN_MYBUTTON_ID_2, NULL, NULL);

    // The parameters to ShowWindow:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

/***
 *    This function creates the following rectangles:
 *        1.    An outer rectangle using a solid brush with blue background.
 *        2.    An inner rectangle using a hatch brush with red horizontal lines and yellow background.
 *    It makes the background of the inner rectangle transparent or opaque in function of the user's input.
 *    Inputs: 
 *        1.    hdc, the display device context.
 *        2.    transparent, the hatch brush background user's value; true if transparent, false if opaque.
 ***/
VOID SetHatchBrushBackground(HDC hdc, bool transparent)
{
    // Define a brush handle.
    HBRUSH hBrush;

    // Create a solid blue brush.
    hBrush = CreateSolidBrush (RGB(0, 0, 255));

    // Associate the brush with the display device context.
    SelectObject (hdc, hBrush);

    // Draw a rectangle with blue background.
    Rectangle (hdc,  400,40,800,400);


    // Create a hatch brush that draws horizontal red lines.
    hBrush = CreateHatchBrush(HatchStyleHorizontal, RGB(255, 0, 0));

    // Set the background color to yellow.
    SetBkColor(hdc, RGB(255, 255, 0));


    // Select the hatch brush background transparency based on user's input.
    if (transparent == true)
        // Make the hatch brush background transparent.
        // This displays the outer rectangle blue background.
        SetBkMode(hdc, TRANSPARENT);
    else
        // Make the hatch brush background opaque.
        // This displays the inner rectangle yellow background.
        SetBkMode(hdc, OPAQUE);

    // Associate the hatch brush with the current device context.
    SelectObject(hdc, hBrush);

    // Draw a rectangle with the specified hatch brush.
    Rectangle(hdc,  500,130,700,300);

}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Select your brush background.");
    TCHAR wmId;
    TCHAR wmEvent;


    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Start application-specific layout section.
        // Just print the greeting string in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, (int)_tcslen(greeting));
        // End application-specific layout section.

        // Draw rectangles using hatch brush.
        SetHatchBrushBackground(hdc, true);


        EndPaint(hWnd, &ps);
        break;

    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        hdc = GetDC(hWnd);

        switch (wmId) {

            case BTN_MYBUTTON_ID_1:
                // Draw the inner rectangle using a hatch brush transparent background.
                SetHatchBrushBackground(hdc, true);
                MessageBox(hWnd, _T("Hatch brush background is TRANSPARENT"), _T("Information"), MB_OK);
                break;

            case BTN_MYBUTTON_ID_2:
                // Draw the inner rectangle using a hatch brush opaque background.
                SetHatchBrushBackground(hdc, false);
                MessageBox(hWnd, _T("Hatch brush background is OPAQUE"), _T("Information"), MB_OK);
                break;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;


    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

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

各言語での呼び出し定義

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

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

CreateHatchBrush = ctypes.windll.gdi32.CreateHatchBrush
CreateHatchBrush.restype = ctypes.c_void_p
CreateHatchBrush.argtypes = [
    ctypes.c_int,  # iHatch : HATCH_BRUSH_STYLE
    wintypes.DWORD,  # color : COLORREF
]
require 'fiddle'
require 'fiddle/import'

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

var (
	gdi32 = windows.NewLazySystemDLL("GDI32.dll")
	procCreateHatchBrush = gdi32.NewProc("CreateHatchBrush")
)

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

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

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

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

let createhatchbrush =
  foreign "CreateHatchBrush"
    (int32_t @-> uint32_t @-> returning (ptr void))
(* iHatch : HATCH_BRUSH_STYLE -> 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 ("CreateHatchBrush" create-hatch-brush :convention :stdcall) :pointer
  (i-hatch :int32)   ; HATCH_BRUSH_STYLE
  (color :uint32))   ; COLORREF
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CreateHatchBrush = Win32::API::More->new('GDI32',
    'HANDLE CreateHatchBrush(int iHatch, DWORD color)');
# my $ret = $CreateHatchBrush->Call($iHatch, $color);
# iHatch : HATCH_BRUSH_STYLE -> int
# color : COLORREF -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型