
屏幕光标定位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,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,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); // Function prototypesvoID 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 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,31); getline(cin,input.name); placeCursor(screen,30); cin >> input.age; placeCursor(screen,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
以上是内存溢出为你收集整理的C++光标定位函数详解全部内容,希望文章能够帮你解决C++光标定位函数详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)