从c中的函数返回SetConsoleTextAttribute颜色值

时间:2017-12-06 00:24:55

标签: c function winapi colors

目前我正在尝试使用windows.h函数调用函数来更改文本背景的颜色。我以前做过但不是来自其他功能。问题是函数需要以某种方式返回颜色值或者只是出错了。 如果更改了任何内容,则从另一个函数调用该函数。

代码一起:

void setColour(HANDLE* hConsole, int ChangeColour, int Red, int Green, int Blue, int Colour) {
    CONSOLE_SCREEN_BUFFER_INFOEX info;
    info.cbSize = sizeof(info);
    GetConsoleScreenBufferInfoEx(hConsole, &info);
    info.ColorTable[ChangeColour] = RGB(Red, Green, Blue);
    SetConsoleTextAttribute(hConsole, Colour);
}

void mainMenu(WindowProp* Dimensions, HANDLE* hConsole) {
    getWindowSize(Dimensions);
    clearScreen();
    setColour(hConsole, 3, 120, 120, 236, 48);
    printf("Set");
    int DisplayRowCount, DisplayColumnCount;
    for (DisplayRowCount = 0; DisplayRowCount <= Dimensions->Y-1; DisplayRowCount++) {
        for (DisplayColumnCount = 0; DisplayColumnCount <= Dimensions->X-1; DisplayColumnCount++) {
            printf(" ");
        }
    }
}

mainMenu从main调用:

mainMenu(&WindowP, hConsole);

1 个答案:

答案 0 :(得分:0)

好的,我修好了。我首先决定在mainMenu()函数中声明HANDLE值并删除HANDLE指针。

其次,setColour()函数的GetConsoleScreenBufferInfoEx()在更改颜色值之前确实被调用但在修改颜色值后没有被调用因此我需要第二次调用GetConsoleScreenBufferInfoEx()来获取要使用的新颜色值

固定代码:

void setColour(HANDLE* hConsole, int ChangeColour, int Red, int Green, int Blue, int Colour) {
    CONSOLE_SCREEN_BUFFER_INFOEX info;
    info.cbSize = sizeof(info);
    GetConsoleScreenBufferInfoEx(hConsole, &info);
    info.ColorTable[ChangeColour] = RGB(Red, Green, Blue);
    GetConsoleScreenBufferInfoEx(hConsole, &info);
    SetConsoleTextAttribute(hConsole, Colour);
}

void mainMenu(WindowProp* Dimensions) {
    getWindowSize(Dimensions);
    clearScreen();
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    setColour(hConsole, 3, 120, 120, 236, 48);
    int DisplayRowCount, DisplayColumnCount;
    for (DisplayRowCount = 0; DisplayRowCount <= Dimensions->Y-1; DisplayRowCount++) {
        for (DisplayColumnCount = 0; DisplayColumnCount <= Dimensions->X-1; DisplayColumnCount++) {
            printf(" ");
        }
    }
}