为什么控制台中的文本在点击时消失?

时间:2018-05-29 04:25:08

标签: c++ console

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
WORD setting1 = FOREGROUND_RED; // these are the user settings
WORD setting2 = FOREGROUND_RED;
WORD setting3 = FOREGROUND_RED;

WORD green = FOREGROUND_GREEN; // the 2 colors the settings can change from
WORD red = FOREGROUND_RED;

int changecolor(int key) //find out what the user pressed and then changed the color of the setting
{
    switch (key)
    {
    case VK_F1:
        if (setting1 == red)
        {
            setting1 = green;
        }
        else
            setting1 = red;
    case VK_F2:
        if (setting2 == red)
        {
            setting2 = green;
        }
    case VK_F3:
        if (setting3 = red)
        {
            setting3 = green;
        }
    }
}

WORD getconsoletextattributes(HANDLE hcon)
{
    CONSOLE_SCREEN_BUFFER_INFO bufinfo; // setting a screen buffer variable
    GetConsoleScreenBufferInfo(hcon, &bufinfo); // getting the console screen buffer and putting it into the bufinfo
    return bufinfo.wAttributes;
}

WORD setcolor(HANDLE hcon, WORD color)
{
    SetConsoleTextAttribute(hcon,color);
}

int main()
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    const int getcolors = getconsoletextattributes(hcon);

    while(true)
    {
        int key;
        for(key = 8; key < 190; key++)
        {
            if (GetAsyncKeyState(key))
            {
                changecolor(key);
            }
        }
        setcolor(hcon,setting1);
        cout << "Setting1 \n\n\n" << endl;
        setcolor(hcon,setting2);
        cout << "Setting2 \n\n\n" << endl;
        setcolor(hcon,setting3);
        cout << "Setting3 \n\n\n" << endl;

        Sleep(20);
        system("cls");
    }
}

我正在运行Windows 10,使用带有GNU编译器的codeblocks IDE。

我不熟悉代码块IDE,但它有点小,我的笔记本电脑没有那么多空间。
因此,我试图制作一个黑客菜单类型的东西,应该发生的是:它打印出一些设置,当你按下F1-F3时,它会将颜色从红色切换到绿色(如果黑客是开或关)。

它的工作方式与其应有的相似,但每当我点击控制台时,我的文本就会消失。回归的唯一方法就是重新构建程序。

有谁知道如何解决这个问题?

0 个答案:

没有答案