SetForegroundWindow
関数シグネチャ
// USER32.dll
#include <windows.h>
BOOL SetForegroundWindow(
HWND hWnd
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hWnd | HWND | in | アクティブ化してフォアグラウンドに移動するウィンドウへのハンドルです。 |
戻り値の型: BOOL
公式ドキュメント
指定したウィンドウを作成したスレッドをフォアグラウンドに移動し、そのウィンドウをアクティブにします。
戻り値
型: BOOL
ウィンドウがフォアグラウンドに移動された場合、戻り値は 0 以外になります。
ウィンドウがフォアグラウンドに移動されなかった場合、戻り値は 0 になります。
解説(Remarks)
システムは、どのプロセスがフォアグラウンドウィンドウを設定できるかを制限します。プロセスが SetForegroundWindow を呼び出してフォアグラウンドウィンドウを設定できるのは、次の場合に限られます。
- 次の条件がすべて満たされている場合:
- 呼び出し元のプロセスがデスクトップアプリケーションに属していること。UWP アプリや、Windows 8 または 8.1 向けに設計された Windows ストアアプリではないこと。
- フォアグラウンドプロセスが、以前に LockSetForegroundWindow 関数を呼び出して SetForegroundWindow の呼び出しを無効化していないこと。
- アクティブなメニューが存在しないこと。
- さらに、次の条件のうち少なくとも 1 つが満たされている場合:
- フォアグラウンドロックのタイムアウトが経過していること(SystemParametersInfo の SPI_GETFOREGROUNDLOCKTIMEOUT を参照)。
- 呼び出し元のプロセスがフォアグラウンドプロセスであること。
- 呼び出し元のプロセスがフォアグラウンドプロセスによって開始されたこと。
- 現在フォアグラウンドウィンドウが存在せず、したがってフォアグラウンドプロセスも存在しないこと。
- 呼び出し元のプロセスが最後の入力イベントを受け取ったこと。
- フォアグラウンドプロセスまたは呼び出し元のプロセスのいずれかがデバッグ中であること。
これらの条件を満たしていても、プロセスがフォアグラウンドウィンドウを設定する権利を拒否されることがあります。
ユーザーが別のウィンドウで作業している間は、アプリケーションがウィンドウを強制的にフォアグラウンドへ移動することはできません。代わりに、Windows はそのウィンドウのタスクバーボタンを点滅させてユーザーに通知します。
フォアグラウンドウィンドウを設定できるプロセスは、AllowSetForegroundWindow 関数を呼び出すことで、別のプロセスにフォアグラウンドウィンドウの設定を許可できます。AllowSetForegroundWindow の dwProcessId パラメーターで指定されたプロセスは、次にユーザーが入力を発生させたとき(その入力がそのプロセスに向けられた場合を除く)、または次にいずれかのプロセスが AllowSetForegroundWindow を呼び出したとき(前回の AllowSetForegroundWindow の呼び出しと同じプロセスが指定された場合を除く)に、フォアグラウンドウィンドウを設定する権限を失います。
フォアグラウンドプロセスは、LockSetForegroundWindow 関数を呼び出すことで SetForegroundWindow の呼び出しを無効化できます。
例
次のコード例は SetForegroundWindow の使用方法を示しています。
// If the window is invisible we will show it and make it topmost without the
// foreground focus. If the window is visible it will also be made the
// topmost window without the foreground focus. If wParam is TRUE then
// for both cases the window will be forced into the foreground focus
if (uMsg == m_ShowStageMessage) {
BOOL bVisible = IsWindowVisible(hwnd);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
(bVisible ? SWP_NOACTIVATE : 0));
// Should we bring the window to the foreground
if (wParam == TRUE) {
SetForegroundWindow(hwnd);
}
return (LRESULT) 1;
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// USER32.dll
#include <windows.h>
BOOL SetForegroundWindow(
HWND hWnd
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll", ExactSpelling = true)]
static extern bool SetForegroundWindow(
IntPtr hWnd // HWND
);<DllImport("USER32.dll", ExactSpelling:=True)>
Public Shared Function SetForegroundWindow(
hWnd As IntPtr ' HWND
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' hWnd : HWND
Declare PtrSafe Function SetForegroundWindow Lib "user32" ( _
ByVal hWnd As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SetForegroundWindow = ctypes.windll.user32.SetForegroundWindow
SetForegroundWindow.restype = wintypes.BOOL
SetForegroundWindow.argtypes = [
wintypes.HANDLE, # hWnd : HWND
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('USER32.dll')
SetForegroundWindow = Fiddle::Function.new(
lib['SetForegroundWindow'],
[
Fiddle::TYPE_VOIDP, # hWnd : HWND
],
Fiddle::TYPE_INT)#[link(name = "user32")]
extern "system" {
fn SetForegroundWindow(
hWnd: *mut core::ffi::c_void // HWND
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USER32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetForegroundWindow' -Namespace Win32 -PassThru
# $api::SetForegroundWindow(hWnd)#uselib "USER32.dll"
#func global SetForegroundWindow "SetForegroundWindow" sptr
; SetForegroundWindow hWnd ; 戻り値は stat
; hWnd : HWND -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "USER32.dll"
#cfunc global SetForegroundWindow "SetForegroundWindow" sptr
; res = SetForegroundWindow(hWnd)
; hWnd : HWND -> "sptr"; BOOL SetForegroundWindow(HWND hWnd)
#uselib "USER32.dll"
#cfunc global SetForegroundWindow "SetForegroundWindow" intptr
; res = SetForegroundWindow(hWnd)
; hWnd : HWND -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
user32 = windows.NewLazySystemDLL("USER32.dll")
procSetForegroundWindow = user32.NewProc("SetForegroundWindow")
)
// hWnd (HWND)
r1, _, err := procSetForegroundWindow.Call(
uintptr(hWnd),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetForegroundWindow(
hWnd: THandle // HWND
): BOOL; stdcall;
external 'USER32.dll' name 'SetForegroundWindow';result := DllCall("USER32\SetForegroundWindow"
, "Ptr", hWnd ; HWND
, "Int") ; return: BOOL●SetForegroundWindow(hWnd) = DLL("USER32.dll", "bool SetForegroundWindow(void*)")
# 呼び出し: SetForegroundWindow(hWnd)
# hWnd : HWND -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "user32" fn SetForegroundWindow(
hWnd: ?*anyopaque // HWND
) callconv(std.os.windows.WINAPI) i32;proc SetForegroundWindow(
hWnd: pointer # HWND
): int32 {.importc: "SetForegroundWindow", stdcall, dynlib: "USER32.dll".}pragma(lib, "user32");
extern(Windows)
int SetForegroundWindow(
void* hWnd // HWND
);ccall((:SetForegroundWindow, "USER32.dll"), stdcall, Int32,
(Ptr{Cvoid},),
hWnd)
# hWnd : HWND -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetForegroundWindow(
void* hWnd);
]]
local user32 = ffi.load("user32")
-- user32.SetForegroundWindow(hWnd)
-- hWnd : HWND
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetForegroundWindow = lib.func('__stdcall', 'SetForegroundWindow', 'int32_t', ['void *']);
// SetForegroundWindow(hWnd)
// hWnd : HWND -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("USER32.dll", {
SetForegroundWindow: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.SetForegroundWindow(hWnd)
// hWnd : HWND -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetForegroundWindow(
void* hWnd);
C, "USER32.dll");
// $ffi->SetForegroundWindow(hWnd);
// hWnd : HWND
// 構造体/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);
boolean SetForegroundWindow(
Pointer hWnd // HWND
);
}@[Link("user32")]
lib LibUSER32
fun SetForegroundWindow = SetForegroundWindow(
hWnd : Void* # HWND
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetForegroundWindowNative = Int32 Function(Pointer<Void>);
typedef SetForegroundWindowDart = int Function(Pointer<Void>);
final SetForegroundWindow = DynamicLibrary.open('USER32.dll')
.lookupFunction<SetForegroundWindowNative, SetForegroundWindowDart>('SetForegroundWindow');
// hWnd : HWND -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetForegroundWindow(
hWnd: THandle // HWND
): BOOL; stdcall;
external 'USER32.dll' name 'SetForegroundWindow';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetForegroundWindow"
c_SetForegroundWindow :: Ptr () -> IO CInt
-- hWnd : HWND -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setforegroundwindow =
foreign "SetForegroundWindow"
((ptr void) @-> returning int32_t)
(* hWnd : HWND -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)
(cffi:defcfun ("SetForegroundWindow" set-foreground-window :convention :stdcall) :int32
(h-wnd :pointer)) ; HWND
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetForegroundWindow = Win32::API::More->new('USER32',
'BOOL SetForegroundWindow(HANDLE hWnd)');
# my $ret = $SetForegroundWindow->Call($hWnd);
# hWnd : HWND -> HANDLE
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。