Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;控制台应用程序始终位于顶部?_C++_Windows_Console Application_Always On Top - Fatal编程技术网

C++ C++;控制台应用程序始终位于顶部?

C++ C++;控制台应用程序始终位于顶部?,c++,windows,console-application,always-on-top,C++,Windows,Console Application,Always On Top,我不是在寻找: 使另一个窗口始终位于顶部 制作任何类型的图形用户界面等。。。最上面 但是我正在寻找一种方法,让我的简单C++ >强>控制台应用程序永远保持在顶部, 我只是想说清楚-我正在寻找一种方法来编程:)我努力搜索,但只找到了上面的-我不想要的 P> >在强> Windows ,C++有没有一种方法可以让你的强>控制台应用程序总是在顶部? PS:是的,有一个存在的问题,但这个问题的OP实际上是在寻找其他的东西(键盘挂钩…)-所以答案与我的问题无关 解决方案: 快速回答=>参见@Alex

我不是在寻找:

  • 使另一个窗口始终位于顶部
  • 制作任何类型的图形用户界面等。。。最上面
但是我正在寻找一种方法,让我的简单C++ >强>控制台<强>应用程序永远保持在顶部,
我只是想说清楚-我正在寻找一种方法来编程:)我努力搜索,但只找到了上面的-我不想要的

<> P> >在<>强> Windows

,C++有没有一种方法可以让你的<>强>控制台<强>应用程序总是在顶部? PS:是的,有一个存在的问题,但这个问题的OP实际上是在寻找其他的东西(键盘挂钩…)-所以答案与我的问题无关

解决方案: 快速回答=>参见@AlexanderVX


示例和解释=>下面的OP post中的链接指的是Windows

首先,您需要获取控制台窗口的句柄:

或者更好更现代:一种获得控制台手柄的方式

然后你需要做一个非常简单的技巧:

::SetWindowPos(hwndMyWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
::ShowWindow(hwndMyWnd, SW_NORMAL);
正如快速回答所示,我还想向您展示我的最终实现,并提供适当的注释,解释什么是做什么:):

不要忘记将Windows版本设置为相同或大于
0x0500
,并包括
Windows.h
库:

#define _WIN32_WINNT 0x0500
#include <windows.h>
#定义_WIN32_WINNT 0x0500
#包括
我已将迷你应用程序示例放在:

示例及说明:

// 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 :(";
}
//GetConsoleWindow()=>返回:
//“控制台使用的窗口的句柄
//与调用进程关联
//或空
//如果没有此类关联控制台。”
HWND consolewindowandle=GetConsoleWindow();
如果(控制台Windows Handle){

如果这是针对Windows的,我可以告诉你需要什么。是吗?这很可能是针对Windows框架的。你能澄清一下你的目标吗?Windows、X window、@Nit和其他-什么是不清楚的?(因为在我添加了Windows的用途之后,这更奇怪…)您的链接描述了一种获取XP之前版本Windows句柄的过于复杂的方法。在XP及以后的版本中,您只需使用
GetConsoleWindow()
。同意GetConsoleWindow可以改进它。