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

GetWindowRgn

関数
ウィンドウの表示領域リージョンを取得する。
DLLUSER32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

GDI_REGION_TYPE GetWindowRgn(
    HWND hWnd,
    HRGN hRgn
);

パラメーター

名前方向説明
hWndHWNDinウィンドウ領域を取得するウィンドウへのハンドルです。
hRgnHRGNinウィンドウ領域を表すように変更されるリージョンへのハンドルです。

戻り値の型: GDI_REGION_TYPE

公式ドキュメント

GetWindowRgn 関数は、ウィンドウのウィンドウ領域のコピーを取得します。

戻り値

戻り値は、関数が取得したリージョンの種類を指定します。次のいずれかの値になります。

戻り値 説明
NULLREGION
リージョンが空です。
SIMPLEREGION
リージョンは単一の長方形です。
COMPLEXREGION
リージョンは複数の長方形で構成されています。
ERROR
指定されたウィンドウにリージョンが存在しないか、リージョンを返そうとする際にエラーが発生しました。

解説(Remarks)

ウィンドウのウィンドウ領域の座標は、ウィンドウのクライアント領域ではなく、ウィンドウの左上隅を基準とします。

ウィンドウのウィンドウ領域を設定するには、SetWindowRgn 関数を呼び出します。

次のコードは、既存のリージョンのハンドルを渡す方法を示しています。


HRGN hrgn = CreateRectRgn(0,0,0,0);
int regionType = GetWindowRgn(hwnd, hrgn);
if (regionType != ERROR) 
{ 
/* hrgn contains window region */ 
}
DeleteObject(hrgn); /* finished with region */
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

GDI_REGION_TYPE GetWindowRgn(
    HWND hWnd,
    HRGN hRgn
);
[DllImport("USER32.dll", ExactSpelling = true)]
static extern int GetWindowRgn(
    IntPtr hWnd,   // HWND
    IntPtr hRgn   // HRGN
);
<DllImport("USER32.dll", ExactSpelling:=True)>
Public Shared Function GetWindowRgn(
    hWnd As IntPtr,   ' HWND
    hRgn As IntPtr   ' HRGN
) As Integer
End Function
' hWnd : HWND
' hRgn : HRGN
Declare PtrSafe Function GetWindowRgn Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal hRgn As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetWindowRgn = ctypes.windll.user32.GetWindowRgn
GetWindowRgn.restype = ctypes.c_int
GetWindowRgn.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND
    wintypes.HANDLE,  # hRgn : HRGN
]
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetWindowRgn = user32.NewProc("GetWindowRgn")
)

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

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

typedef GetWindowRgnNative = Int32 Function(Pointer<Void>, Pointer<Void>);
typedef GetWindowRgnDart = int Function(Pointer<Void>, Pointer<Void>);
final GetWindowRgn = DynamicLibrary.open('USER32.dll')
    .lookupFunction<GetWindowRgnNative, GetWindowRgnDart>('GetWindowRgn');
// hWnd : HWND -> Pointer<Void>
// hRgn : HRGN -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetWindowRgn(
  hWnd: THandle;   // HWND
  hRgn: THandle   // HRGN
): Integer; stdcall;
  external 'USER32.dll' name 'GetWindowRgn';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetWindowRgn"
  c_GetWindowRgn :: Ptr () -> Ptr () -> IO Int32
-- hWnd : HWND -> Ptr ()
-- hRgn : HRGN -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

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

(cffi:defcfun ("GetWindowRgn" get-window-rgn :convention :stdcall) :int32
  (h-wnd :pointer)   ; HWND
  (h-rgn :pointer))   ; HRGN
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetWindowRgn = Win32::API::More->new('USER32',
    'int GetWindowRgn(HANDLE hWnd, HANDLE hRgn)');
# my $ret = $GetWindowRgn->Call($hWnd, $hRgn);
# hWnd : HWND -> HANDLE
# hRgn : HRGN -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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