• C++光标定位函数详解

    操作系统函数允许程序员对控制台屏幕上的输出进行控制。

    屏幕光标定位

    C++ 编译器提供了用于调用操作系统函数的特殊库,现在来看一下 Windows 操作系统有关屏幕光标定位的函数,此函数是 SetConsoleCursorPosition。

    操作系统函数是针对特定操作系统量身定制的,所以使用它们的程序只能在它们被写入的系统上运行。这里描述的函数可以与 Windows 2000 以及更高版本的操作系统配合使用。

    到目前为止,我们创建的所有程序都是在屏幕的顶行显示输出。然后,当用户按回车键或程序输出 endl 或“\n”时,它们一次移动一行。但是,如果正在屏幕的第 5 行写入内容,想要回到第 2 行该怎么办?或者,如果想在屏幕的正中间显示某些东西该怎么办?要满足这些要求,可以在 Windows 系统上使用 SetConsoleCursorPosition 函数,在写入输出之前将光标移动到所需位置。

    要使用此函数,必须做以下几件事情:

    • 在程序中添加 #include <windows.h>。
    • 通过在程序中包含以下定义,创建标准输出屏幕的句柄:

      HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

    典型的文本屏幕具有 25 行,每行具有 80 个打印位置。这些位置中的每一个称为格。格是一个可以显示单个字符的小块,它可以通过行号及其在该行上的位置进行区别。行的范围是 0〜24,其中 0 是屏幕的顶行。每行上的打印位置通常被称为列,范围为 0〜79,其中最左侧为 0。格的行和列可以标识它在屏幕上的位置,它们称为其坐标。

    要将光标放在指定的屏幕格中,必须通过在 Windows 中定义的 COORD 结构中设置两个变量来指定格坐标。这个结构有两个名为 X 和 Y 的成员变量,X 保存列位置,Y 保存行位置。以下是该结构大致的样子:

    struct COORD
    {
        short int X; // 列位置
        short int Y; // 行位置
    };

    现在来介绍其使用方法。以下代码段可以将单词“Hello”写入标准输出屏幕的中心:

    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position;    // position 是一个 COORD 结构
    position.X = 38; //设置列在屏幕中心附近
    position.Y = 11; //设置行在屏幕中心附近
    //将光标放在那里,然后开始打印
    SetConsoleCursorPosition(screen, position);
    cout << "Hello" << endl;

    注意,在设置一个屏幕位置时,必须在程序的所有输出后面都跟上 endl,这是确保输出实际显示在此位置的必要条件。如果不使用 endl,则在光标位置更改后,输出可能会缓冲并在很久以后写入屏幕。如果在输出后面接着使用换行符“\n”则不起作用,因为它不会像 endl 那样冲刷屏幕缓冲区。

    下面的程序定位光标以在屏幕中心附近显示一组嵌套框:

    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    void placeCursor(HANDLE, int, int);
    void printStars (int);
    int main()
    {
        const int midRow = 12,midCol = 40, numBoxes = 3;
        int width, startRow, endRow;
        //Get the handle to standard output device (the console)
        HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
        // Each loop prints one box
        for (int box = 1, height = 1; box <= numBoxes; box++, height+=2)
        {
            startRow = midRow - box;
            endRow = midRow + box;
            width = box*5 + (box+1)%2;// Adds 1 if box*5 is an even number
            //Draw box top
            placeCursor(screen, startRow, midCol-width/2);
            printStars(width);
            // Print box sides
            for (int sideRow = 1; sideRow <= height; sideRow++)
            {
                placeCursor(screen, startRow + sideRow, midCol-width/2);
                cout << '*' << endl;
                placeCursor(screen, startRow + sideRow,midCol+width/2);
                cout << '*' << endl;
            }
            // Draw box bottom
            placeCursor(screen, endRow, midCol-width/2);
            printStars(width);
            Sleep(750); //Pause 3/4 second between boxes displayed
        }
        placeCursor(screen, 20, 0); // Move cursor out of the way return 0;
    }
    void placeCursor(HANDLE screen, int row, int col)
    {
        COORD position;
        position.Y = row;
        position.X = col;
        SetConsoleCursorPosition(screen, position);
    }
    void printStars(int numStars)
    {
        for (int star = 1; star <= numStars; star++)
            cout << '*';
        cout << endl;
    }

    程序输出结果:

    ***************
    * *********** *
    * *  *****  * *
    * *  *   *  * *
    * *  *****  * *
    * *********** *
    ***************

    请注意,此程序中使用了 Sleep 函数,它可以使程序执行暂停一段时间,所以它不会使事情发生得太快导致用户看不到它们。传递给函数的实参告诉它应该暂停多少毫秒。毫秒是千分之一秒。因此,假如要暂停执行程序半秒钟,则可以调用以下函数语句:

    Sleep (500);

    创建一个屏幕输入表单

    上面程序运行起来很有趣,但下面程序演示了将光标定位在屏幕上的更实际的应用。

    当需要用户输入一系列条目时,不必每次提示一个输入一个,而是可以设计一个屏幕输入表单。这种更专业的从用户获得输入的方式,需要创建和显示一个界面,一次性显示所有提示,然后将光标放置在需要用户输入的特定提示旁边。当用户输入此提示的数据并按下回车键时,光标移动到下一个提示。

    #include <iostream>
    #include <windows.h> // Needed to set cursor positions
    #include <string>
    using namespace std;
    
    struct userInfo
    {
        string name;
        int age;
        char gender;
    };
    
    void placeCursor(HANDLE, int, int); // Function prototypes
    void displayPrompts(HANDLE);
    void getUserInput (HANDLE, userInfo&);
    void displayData (HANDLE, userInfo);
    
    int main()
    {
        userInfo input;
        HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
        displayPrompts(screen);
        getUserInput(screen, input);
        displayData (screen, input);
        return 0;
    }
    void placeCursor(HANDLE screen, int row, int col)
    {
        COORD position;    // holds a pair of X and Y coordinates
        position.Y = row;
        position.X = col;
        SetConsoleCursorPosition(screen, position);
    }
    void displayPrompts(HANDLE screen)
    {
        placeCursor(screen, 3, 25);
        cout << "******* Data Entry Form *******" << endl;
        placeCursor(screen, 5, 25);
        cout << "Name: " << endl;
        placeCursor(screen, 7, 25);
        cout <<"Age:       Gender (M/F) : " << endl;
    }
    void getUserInput(HANDLE screen, userInfo &input)
    {
        placeCursor(screen, 5, 31);
        getline(cin, input.name);
        placeCursor(screen, 7, 30);
        cin >> input.age;
        placeCursor(screen, 7, 55);
        cin >> input.gender;
    }
    void displayData(HANDLE screen, userInfo input)
    {
        placeCursor(screen, 10,0);
        cout<<"Here is the data you entered. \n";
        cout<<"Name   : "<< input.name << endl;
        cout<<"Age   : "<< input.name << endl;
        cout<<"Gender : "<< input.name << endl;
    }

    程序输出结果:

    ******* Data Entry Form *******
    Name: Mary Beth Jones
    Age: 19    Gender (M/F) :     F

    Here is the data you entered.
    Name   : Mary Beth Jones
    Age   : Mary Beth Jones
    Gender : Mary Beth Jones

更多...

加载中...