/***
* THIS IS AN PUBLIC WINDOWS C++ MINI-APP
* TO DEMONSTRATE MAKING A CONSOLE WINDOW ALWAYS ON TOP
*
* COMPILE & USE ON WINDOWS (not here :P)
***/
#include <iostream> // standard input/output C++ library
#define _WIN32_WINNT 0x0500 // to use certain API, the windows version must be set to 0x0500 or more
#include <windows.h> // using main Windows library for C++ Windows API
using namespace std; // to make this example clearer - not recommended for production
int main() {
// 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(); // getting the console window handle
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 :(";
}
char buffer = 'y';
while( buffer != 'n' ){
cout << endl << "Continue? (y/n):";
cin >> buffer;
}
return 0;
}
/***
References:
GetConsoleWindow => http://msdn.microsoft.com/en-us/library/windows/desktop/ms683175%28v=vs.85%29.aspx
SetWindowPos => http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx
ShowWindow => http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
Setting WINVER or _WIN32_WINNT => http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745(v=vs.85).aspx#setting_winver_or__win32_winnt
***/