惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
宝玉的分享
宝玉的分享
量子位
V
Visual Studio Blog
罗磊的独立博客
Vercel News
Vercel News
B
Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
GbyAI
GbyAI
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Activate the Window for the Specified HWND
vast cow · 2026-06-15 · via DEV Community

In Windows applications, there are cases where you want to bring a specific window to the front and make it the active target for user interaction.

This code is designed to activate the window associated with a specified HWND and, when possible, make it the foreground window.

Purpose

The purpose of this function is to make the specified window available for user interaction.

For example, it can be used when you want to bring an application hidden behind other windows to the front, or restore and display a minimized window.

Usage

The primary function is activate_window.

bool result = activate_window(hwnd);

Pass the handle of the window you want to activate in hwnd.

The function returns true if the operation succeeds and the window becomes the foreground window.

It returns false if the operation fails.

Processing Flow

The code first converts the specified HWND into its top-level window.

This ensures that even if a child window handle is provided, the operation targets the parent top-level window.

Next, it verifies that the target window exists and can be displayed.

If the target window is already in the foreground, the function immediately returns true.

When the Window Is Minimized

If the target window is minimized, it is restored using ShowWindowAsync.

ShowWindowAsync(hwnd, SW_RESTORE);

If the window is not minimized, it is shown normally.

ShowWindowAsync(hwnd, SW_SHOW);

This makes hidden or minimized windows visible again.

Bringing the Window to the Front

After displaying the window, the code attempts to bring it to the foreground using operations such as:

SetWindowPos(...);
BringWindowToTop(hwnd);
SetForegroundWindow(hwnd);
SetActiveWindow(hwnd);
SetFocus(hwnd);

By combining these APIs, the code tries to move the specified window to the front and assign keyboard focus as reliably as possible.

Using AttachThreadInput

Windows may prevent a window owned by another thread from being activated directly.

To address this, the code temporarily associates the current thread with the target window's thread using AttachThreadInput.

This increases the likelihood that SetForegroundWindow and SetFocus will succeed.

After the operation is complete, the thread association is removed.

AttachThreadInput(current_tid, target_tid, FALSE);

In other words, the threads are temporarily linked, the activation work is performed, and then the connection is released.

ALT Key Fallback

If the normal activation process fails, the code sends an ALT key press as a fallback.

tap_alt_key();

On Windows, sending an ALT key event can sometimes make SetForegroundWindow succeed afterward.

This can be particularly effective in scenarios involving UWP applications or ApplicationFrameHost.

The code then retries displaying the window and bringing it to the foreground.

Return Value

At the end of the process, activate_window checks whether the target window is actually the foreground window.

return is_foreground(hwnd);

It returns true if successful and false otherwise.

Example Use Cases

This function can be useful in situations such as:

  • Bringing an external application's window to the foreground
  • Restoring a minimized window
  • Making a specific tool window the target of user interaction
  • Switching to a designated window in a multi-window application

Notes

Windows includes restrictions that prevent applications from freely forcing other windows into the foreground.

As a result, this code cannot guarantee success in every situation.

However, by combining top-level window normalization, thread input attachment, minimized-window restoration, and an ALT-key fallback mechanism, it achieves a practical and generally reliable approach.

Summary

This code provides a practical helper function for activating a window specified by an HWND.

Rather than relying solely on SetForegroundWindow, it includes window-state validation, restoration, thread-input attachment, and fallback handling to improve reliability.

If you need to bring a specific window to the foreground on Windows, this function serves as a convenient and robust utility.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdbool.h>

static HWND normalize_top_level(HWND hwnd)
{
    HWND root;

    if (!hwnd) return NULL;

    root = GetAncestor(hwnd, GA_ROOT);
    if (root && IsWindow(root)) return root;

    return hwnd;
}

static bool same_top_level(HWND a, HWND b)
{
    return normalize_top_level(a) == normalize_top_level(b);
}

static bool is_foreground(HWND hwnd)
{
    HWND fg = GetForegroundWindow();
    return fg && same_top_level(fg, hwnd);
}

static void tap_alt_key(void)
{
    INPUT inputs[2] = {0};

    inputs[0].type = INPUT_KEYBOARD;
    inputs[0].ki.wVk = VK_MENU;

    inputs[1].type = INPUT_KEYBOARD;
    inputs[1].ki.wVk = VK_MENU;
    inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;

    SendInput(2, inputs, sizeof(INPUT));
}

bool activate_window(HWND hwnd)
{
    DWORD current_tid;
    DWORD foreground_tid = 0;
    DWORD target_tid = 0;
    DWORD dummy_pid = 0;
    HWND foreground_hwnd;
    BOOL attached_foreground = FALSE;
    BOOL attached_target = FALSE;
    MSG msg;

    hwnd = normalize_top_level(hwnd);

    if (!hwnd) return false;
    if (!IsWindow(hwnd)) return false;
    if (!IsWindowVisible(hwnd)) return false;

    if (is_foreground(hwnd)) return true;

    /*
        AttachThreadInput 用に自スレッドの message queue を作る。
    */
    PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);

    current_tid = GetCurrentThreadId();

    foreground_hwnd = GetForegroundWindow();
    if (foreground_hwnd && IsWindow(foreground_hwnd)) {
        foreground_tid = GetWindowThreadProcessId(foreground_hwnd, &dummy_pid);
    }

    target_tid = GetWindowThreadProcessId(hwnd, &dummy_pid);

    if (foreground_tid && foreground_tid != current_tid) {
        attached_foreground = AttachThreadInput(current_tid, foreground_tid, TRUE);
    }

    if (target_tid && target_tid != current_tid && target_tid != foreground_tid) {
        attached_target = AttachThreadInput(current_tid, target_tid, TRUE);
    }

    if (IsIconic(hwnd)) {
        ShowWindowAsync(hwnd, SW_RESTORE);
    } else {
        ShowWindowAsync(hwnd, SW_SHOW);
    }

    SetWindowPos(
        hwnd,
        HWND_TOP,
        0,
        0,
        0,
        0,
        SWP_NOMOVE |
        SWP_NOSIZE |
        SWP_SHOWWINDOW |
        SWP_ASYNCWINDOWPOS
    );

    BringWindowToTop(hwnd);
    SetForegroundWindow(hwnd);
    SetActiveWindow(hwnd);
    SetFocus(hwnd);

    if (attached_target) {
        AttachThreadInput(current_tid, target_tid, FALSE);
    }

    if (attached_foreground) {
        AttachThreadInput(current_tid, foreground_tid, FALSE);
    }

    if (is_foreground(hwnd)) {
        return true;
    }

    tap_alt_key();

    if (IsIconic(hwnd)) {
        ShowWindowAsync(hwnd, SW_RESTORE);
    } else {
        ShowWindowAsync(hwnd, SW_SHOW);
    }

    SetWindowPos(
        hwnd,
        HWND_TOP,
        0,
        0,
        0,
        0,
        SWP_NOMOVE |
        SWP_NOSIZE |
        SWP_SHOWWINDOW |
        SWP_ASYNCWINDOWPOS
    );

    BringWindowToTop(hwnd);
    SetForegroundWindow(hwnd);

    return is_foreground(hwnd);
}