How to use setforegroundwindow in C++?
In C++, using the SetForegroundWindow function can set a specified window as the foreground window. The declaration of this function is as follows:
BOOL SetForegroundWindow(
HWND hWnd
);
The parameter hWnd is the handle of the window to be set to the foreground.
Below is a sample code demonstrating how to use the SetForegroundWindow function.
#include <iostream>
#include <windows.h>
int main() {
HWND hWnd = FindWindow(NULL, "Untitled - Notepad"); // 根据窗口标题查找窗口句柄
if (hWnd) {
SetForegroundWindow(hWnd); // 将找到的窗口设置为前台窗口
std::cout << "SetForegroundWindow succeeded." << std::endl;
} else {
std::cout << "Window not found." << std::endl;
}
return 0;
}
In this example, the FindWindow function is used to locate the window handle of Notepad based on its window title. Then, SetForegroundWindow is used to set the located window as the foreground window.
Please note that the SetForegroundWindow function may not be able to set a window as a foreground window due to permission issues.