C++ console application always on top? [closed]

↘锁芯ラ 提交于 2019-11-28 06:03:37

问题


I am NOT looking for:

  • making another window always on top
  • making any sort of GUI - dialogs etc... on top

I am however looking for a way how to make my simple C++ console application to stay always on top,
just to be clear - I am looking for a way how to do this programaticly :) I tried hard searching but only found the above - what I do not want...

So is there a way how to make your console app always on top programaticly in C++ on Windows?

PS: Yes there is an existing question with a coresponding title but the OP of that question is actually looking for something else (keyboard hooks,...) - so answers there are off-topic to my question.

Solution:

Quick answer => see accepted answer by @AlexanderVX

Example & explanation => my answer below


回答1:


The link in OP post refers to Windows.

First you need to obtain a handle for your console window: https://support.microsoft.com/kb/124103

Or even better and modern: GetConsoleWindow way to get that console handle.

Then you need to do quite a simple trick:

::SetWindowPos(hwndMyWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
::ShowWindow(hwndMyWnd, SW_NORMAL);



回答2:


As @AlexanderVX's answer shows an quick answer, I wanted to also show you my final implementation with appropriate comments explaining what does what :) :

Don't forget to set Windows version same or greater than 0x0500 and include windows.h library:

#define _WIN32_WINNT 0x0500
#include <windows.h>

I've put mini-app example on: http://ideone.com/CeLQj3

Example with explanation:

// 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 :(";
}

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

PS: I wanted to post this as an edit to @AlexanderVX's answer, but most of stackoverflow's reviewers somehow thought that "This edit deviates from the original intent of the post"...



来源:https://stackoverflow.com/questions/27068650/c-console-application-always-on-top

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!