4

私がしたいこと:

カーソルは、最初は画面の左上隅で点滅しています。

残り160文字
_

「i」を押すと:

残り159文字

「a」を押すと:

残り158文字

私は

「m」を押すと:

残り157文字

わたし

等々。

何をする必要がありますか(私によると):

  1. 画面の最初の 3 文字だけをクリアする必要があります。
  2. 画面上の新しく押されたキーを更新します

私が試したこと:

画面全体をクリアして、以前にあったものをすべて書き戻そうとしました。

自分のしたことに満足できない理由:

ぎくしゃくした印象になるからです。そして、エントリーはスムーズではありません。

手伝ってほしいこと:

画面の一部のみをクリアするための組み込み関数またはその他の手法。

私の仕様:

Windows XP SP3

IDE: Visual C++ 2010 Express

4

3 に答える 3

5

The first thing to understand is that C++ has no conception of a screen, as a standard part of the language. Standard output might be a file, a printer and cout doesn't know the difference.

The screen "device" itself, however, is typically a little smarter, and recognizes some commands. The most widely-implemented of these are '\r' - the carriage return and '\n' - the line feed. '\r' moves the cursor to the beginning of the line, and '\n' advances to the next line, but that's not fit into your needs as you've already tried.

It seems the only way forward here is to use curses (of which ncurses is only one implementation, though the standard one in Linux). It presents you with a virtual screen with various commands to update them. It then takes only the changed portions, and updates the terminal in an optimized way.

It's just an example of the typical C program using ncurses, could be worth to take a look:

#include <ncurses.h>

int main()
{   
    int ch;

    initscr();              /* Start curses mode        */
    raw();                  /* Line buffering disabled  */
    keypad(stdscr, TRUE);   /* We get F1, F2 etc..      */
    noecho();               /* Don't echo() while we do getch */

    printw("Type any character to see it in bold\n");
    ch = getch();           /* If raw() hadn't been called
                             * we have to press enter before it
                             * gets to the program      */

    printw("The pressed key is ");
    attron(A_BOLD);
    printw("%c", ch);
    attroff(A_BOLD);

    refresh();          /* Print it on to the real screen */
    getch();            /* Wait for user input */
    endwin();           /* End curses mode        */

    return 0;
}

The printw() function writes to an "imaginary" screen. It puts stuff into a buffer and updates some flags and does some other internal ncurses bookkeeping. It doesn't actually write anything to your real screen (the console window).

You can do as much printw() writing as you want to, but the stuff doesn't show up on the real screen until your program does something else to cause the "imaginary" screen buffer contents to go to the real screen.

One thing that causes the real screen to be updated from the printw() buffer is refresh() (as the source code example above does).

于 2013-02-25T09:05:51.630 に答える
2

Win32コンソールはエスケープ シーケンスをサポートしていません。コンソール APIを使用できます。

コンソールから (0, 0) の最初の 3 文字をクリアする小さな例

#include <windows.h>

int main()
{
   HANDLE hOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);

   COORD coord = {0,0};
   ::SetConsoleCursorPosition(hOutput, coord);

   char buff[] = "   ";
   ::WriteConsoleA(hOutput, buff, 3, NULL, NULL);

   return 0;
}

アナログが嫌いConsole APIで使いたい場合は、そちらを参照してください。ncurses

于 2013-02-25T09:30:11.940 に答える
1

完全な画面を管理したい場合cursesは、行く方法です。それ以外の場合は、エスケープ シーケンスを使用するだけで多くのことができます。たとえば、http://en.wikipedia.org/wiki/ANSI_escape_codeを参照してください。(歴史的に、このようなシーケンスは端末ごとに異なり、curses はもともとこれを回避する方法でした。現在、ANSI エスケープ コードは、ウィンドウ システムの下のコンソール ウィンドウで非常に普遍的であり、Windows コンソール ウィンドウと xterm の両方で使用されています。 .)

実際のシーケンスをカプセル化することに加えて、curses エコーの有無にかかわらず、文字指向の入力をサポートします。これは curses なしで行うのはより困難であり、依然として非常に移植性がありません。

于 2013-02-25T09:14:15.960 に答える