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

GetForegroundWindow

関数
現在の前面ウィンドウのハンドルを取得する。
DLLUSER32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

HWND GetForegroundWindow(void);

パラメーターなし。戻り値: HWND

各言語での呼び出し定義

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

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

GetForegroundWindow = ctypes.windll.user32.GetForegroundWindow
GetForegroundWindow.restype = ctypes.c_void_p
GetForegroundWindow.argtypes = []
require 'fiddle'
require 'fiddle/import'

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

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
)

r1, _, err := procGetForegroundWindow.Call()
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HWND
function GetForegroundWindow: THandle; stdcall;
  external 'USER32.dll' name 'GetForegroundWindow';
result := DllCall("USER32\GetForegroundWindow", "Ptr")
●GetForegroundWindow() = DLL("USER32.dll", "void* GetForegroundWindow()")
# 呼び出し: GetForegroundWindow()
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。