user197967
user197967

Reputation:

C/C++/C# Force window to be on top

Is the there a way to force another window to be on top? Not the application's window, but another one, already running on the system. (Windows, C/C++/C#)

Upvotes: 6

Views: 12505

Answers (4)

RED SOFT ADAIR
RED SOFT ADAIR

Reputation: 12238

BringWindowToTop() has no effect if you want to bring a applications window from behind (or minimized) to front. The following code does this trick reliably:

ShowWindow(hwnd, SW_MINIMIZE);
ShowWindow(hwnd, SW_RESTORE);

Upvotes: 5

camino
camino

Reputation: 10594

BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
     wchar_t buff[255];

    if (IsWindowVisible(hWnd)) {
        GetWindowText(hWnd, (LPWSTR) buff, 254);
        //wprintf(L"%s\n", buff);
        wstring ws = buff;
        if (ws.find(L"Firefox") != ws.npos)
        {
            ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
        }
    }
    return TRUE;
}

int main(){
    BOOL enumeratingWindowsSucceeded = ::EnumWindows( EnumWindowsProc, NULL );
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490623

SetWindowPos(that_window_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

BringWindowToTop moves the window to the top of the Z-order (for now) but does not make it a topmost window.

Upvotes: 10

Brian R. Bondy
Brian R. Bondy

Reputation: 347556

You can use the Win32 API BringWindowToTop. It takes an HWND.

You could also use the Win32 API SetWindowPos which also allows you to do things like make the window a top-level window.

Upvotes: 6

Related Questions