DEV Community

vast cow
vast cow

Posted on

Activate the Window for the Specified HWND

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

ShowWindowAsync(hwnd, SW_SHOW);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)