正如@AlexanderVX 的回答显示了一个快速的答案,我还想向您展示我的最终实现,并附上适当的评论来解释什么是做什么的:):
不要忘记将 Windows 版本设置为相同或更高版本0x0500
并包含windows.h
库:
#define _WIN32_WINNT 0x0500
#include <windows.h>
我已将迷你应用示例放在:http: //ideone.com/CeLQj3
示例说明:
// GetConsoleWindow() => returns:
// "handle to the window used by the console
// associated with the calling process
// or NULL
// if there is no such associated console."
HWND consoleWindowHandle = GetConsoleWindow();
if( consoleWindowHandle ){
cout << endl << "Setting up associated console window ON TOP !";
SetWindowPos(
consoleWindowHandle, // window handle
HWND_TOPMOST, // "handle to the window to precede
// the positioned window in the Z order
// OR one of the following:"
// HWND_BOTTOM or HWND_NOTOPMOST or HWND_TOP or HWND_TOPMOST
0, 0, // X, Y position of the window (in client coordinates)
0, 0, // cx, cy => width & height of the window in pixels
SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW // The window sizing and positioning flags.
);
// OPTIONAL ! - SET WINDOW'S "SHOW STATE"
ShowWindow(
consoleWindowHandle, // window handle
SW_NORMAL // how the window is to be shown
// SW_NORMAL => "Activates and displays a window.
// If the window is minimized or maximized,
// the system restores it to its original size and position.
// An application should specify this flag
// when displaying the window for the first time."
);
cout << endl << "Done.";
} else {
cout << endl << "There is no console window associated with this app :(";
}
参考:
PS:我想将其发布为对@AlexanderVX 答案的编辑,但大多数stackoverflow 的评论者都认为“此编辑偏离了帖子的初衷”......