似乎无法更改光标/提示框

时间:2019-01-30 06:47:27

标签: c++ visual-studio cursor caret

我正在尝试更改光标/尖号以指示控制台(DOS)文本应用程序中的覆盖或插入模式。

我尝试将SetCursorInfo的值设置为1到100(不变)。我尝试将CreateCaret的值设置为1到800。由于它是控制台应用程序,所以我宁愿避免花哨并显示模式特别是在某个地方但是,如果要加紧努力,我会走那条路,就像到目前为止我遇到的其他问题一样。我放弃了仅打印反向文本,而当我将字符(或打印字符)放置到第25行第80列时停止屏幕滚动:(

//Global variabls, at least 3 functions need them.
const HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
const HWND cscreen = GetConsoleWindow();
WORD attribute;
WORD rattribute;
_FLAGS flags = { false, false, false};  // insert, hex, escape

// attribute & rattribute are set by the calling program.

int GetNumber(int x, int y) {
    COORD position, prepos, cpos;
    position.X = x;
    prepos.X = x - 2;
    prepos.Y = position.Y = y;
    CONSOLE_CURSOR_INFO cInfo, cInsert;
    const char hlist[] = "0123456789ABCDEF";
    int cx = x, cy = y;
    int value = 0;
    unsigned long hold;
    char buffer[6] = "     ";
    char *ePtr = buffer + 5;
    int c = 0, i = 0, j;
    int base = (flags.hex) ? 16 : 10;

    buffer[4] = (flags.hex) ? '\0' : ' ';
    GetConsoleCursorInfo(screen, &cInfo);
    cInfo.bVisible = true;
    cInsert = cInfo;
    cInsert.dwSize *= 3;
    SetConsoleCursorInfo(screen, &cInfo);

    while (1) {
        SetConsoleTextAttribute(screen, attribute);
        CreateCaret(cscreen, (HBITMAP)NULL, (flags.insert) ? 100 : 800, (flags.insert) ? 800 : 100);
        if (flags.insert) SetCursorInfo(screen, cInsert);
        else SetCursorInfo(screen cInfo);
        SetConsoleCursorPosition(screen, prepos);
        if (flags.hex) printf("0x");
        else printf("  ");
        printf("     ");
        SetConsoleTextAttribute(screen, rattribute);
        while (c != _RETURN) {
            SetConsoleCursorPosition(screen, position);
            printf("%s", buffer);
            SetCaretPos(cx, cy);
            cpos.X = cx;
            cpos.Y = cy;
            SetConsoleCursorPosition(screen, cpos);
            ShowCaret(cscreen);
            c = toupper(_getch());
            if (c == _EXTENDED) c = ((c << 8) & 0xff00) + (_getch() & 0x00ff);
            if (c < 0x001c) c += 0xe000;
            if (c == 'H') {
                flags.hex = !flags.hex;
                SetConsoleTextAttribute(screen, attribute);
                SetConsoleCursorPosition(screen, prepos);
                if (flags.hex) printf("0x");
                else printf("  ");
                printf("     ");
                SetConsoleTextAttribute(screen, rattribute);
                base = (flags.hex) ? 16 : 10;
                buffer[4] = (flags.hex) ? '\0' : ' ';
            }
            else if (c > 0xe000) {
                switch (c) {
                case _BACKSP:
                    if (x < cx) {
                        i = cx - x;
                        while (buffer[i] != '\0') buffer[i - 1] = buffer[i++];
                        buffer[i - 1] = ' ';
                        --cx;
                    }
                    else printf("\a");
                    break;
                case _ESCAPE:
                    flags.escape = true;
                    SetConsoleTextAttribute(screen, attribute);
                    SetConsoleCursorPosition(screen, prepos);
                    HideCaret(cscreen);
                    printf("       ");
                    return 0;
                case _HOME:
                    cx = x;
                    break;
                case _LEFT:
                    if (cx > x) --cx;
                    else printf("\a");
                    break;
                case _RIGHT:
                    i = cx - x;
                    if (buffer[i + 1] != '\0') ++cx;
                    else printf("\a");
                    break;
                case _END:
                    i = 0;
                    while (buffer[i] != ' ' && buffer[i] != '\0') ++i;
                    if (buffer[i] == '\0') --i;
                    cx = x + i;
                    break;
                case _INSERT:
                    flags.insert = !flags.insert;
                    CreateCaret(cscreen, (HBITMAP)NULL, (flags.insert) ? 100 : 800, (flags.insert) ? 800 : 100);
                    if (flags.insert) SetCursorInfo(screen, cInsert);
                    else SetCursorInfo(screen, cInfo);
                    break;
                case _DELETE:
                    i = cx - x;
                    while (buffer[i + 1] != ' ' && buffer[i + 1] != '\0') {
                        buffer[i] = buffer[i + 1];
                        ++i;
                    }
                    buffer[i] = ' ';
                    break;
                default:
                    printf("\a");
                }
            }
            else {
                i = cx - x;
                for (j = 0; j < base; j++) if (c == hlist[j]) break;
                if (j < base) {
                    if (flags.insert) {
                        j = i;
                        while (buffer[j] != ' ' && buffer[j] != '\0') ++j;
                        if (buffer[j] == '\0') { --j; printf("\a"); }
                        for (; j > i; j--) buffer[j] = buffer[j - 1];
                        --cx;
                    }
                    buffer[i] = (char)(c & 0x00ff);
                    if (buffer[i + 1] == '\0') printf("\a");
                    else ++cx;
                }
                else printf("\a");
            }
        }
        hold = strtol(buffer, &ePtr, base);
        if (hold < 65536) {
            SetConsoleTextAttribute(screen, attribute);
            SetConsoleCursorPosition(screen, position);
            printf("     \b\b\b\b\b%s", buffer);
            break;
        }
        printf("\a");
    }

    value = (unsigned __int16)(hold & 0x0000ffff);
    return value;
}

这是当前的迭代(当我暂时放弃时)。代码可以运行,但是我得到的只是标准的带下划线的光标/插入符号,没有变化。期望它的大小是原来的三倍,或者变成竖线。

0 个答案:

没有答案