C语言光标移动

Python062

C语言光标移动,第1张

CROOD就是这样的一个玩意:

typedef struct _COORD {

SHORT X

SHORT Y

} COORD, *PCOORD

很明显这个结构可以用来记录坐标

GetStdHandle()函数获得标准输入输出的句柄,参数STD_OUTPUT_HANDLE是个宏,代表标准输出,可以看作显示器

SetConsoleCursorPosition(hout,coord)看函数名称:设置控制台光标坐标,参数就是设备句柄,坐标,那么把标准输出的句柄传给函数,就可以把光标定位在对应的位置了(左上角位置是0,0然后向左 向下延伸)

下面这段代码可以在屏幕第10行第30列输出"Hello world"

#include <windows.h>

#include <iostream>

using namespace std

int main()

{

HANDLE hout

COORD coord

coord.X=30

coord.Y=10

hout=GetStdHandle(STD_OUTPUT_HANDLE)

SetConsoleCursorPosition(hout,coord)

cout<<"Hello world!\n"

return 0

}

具体代码如下:

#include <windows.h>

void HideCursor()

{

CONSOLE_CURSOR_INFO cursor_info = {1, 0}

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info)

}

函数和结构体都在windows.h中定义。

CONSOLE_CURSOR_INFO结构体定义如下:

typedef struct

{ DWORD dwSize

BOOL bVisible//为0时光标不可见

}CONSOLE_CURSOR_INFO, *PCONSOLE_CURSOR_INFO

VC,MinGW中均没问题。

扩展资料:

指针

如果一个变量声明时在前面使用 * 号,表明这是个指针型变量。换句话说,该变量存储一个地址,而 *(此处特指单目运算符 * ,下同。C语言中另有 双目运算符 *) 则是取内容操作符,意思是取这个内存地址里存储的内容。指针是 C 语言区别于其他同时代高级语言的主要特征之一。

指针不仅可以是变量的地址,还可以是数组、数组元素、函数的地址。通过指针作为形式参数可以在函数的调用过程得到一个以上的返回值,不同于return(z)这样的仅能得到一个返回值。

指针是一把双刃剑,许多操作可以通过指针自然的表达,但是不正确的或者过分的使用指针又会给程序带来大量潜在的错误。

参考资料来源:百度百科-c语言

#include<stdio.h>

#include<time.h>

#include<windows.h>

typedef struct

{

int x,y

char ch

}STU

STU st[100]

//出现位置 

void gotoxy(int x, int y)

{

  HANDLE hout

  COORD pos

  pos.X = x

  pos.Y = y

  hout = GetStdHandle(STD_OUTPUT_HANDLE)

  SetConsoleCursorPosition(hout, pos)

}

/*隐藏光标*/

void show_cursor(int hide)

{

  CONSOLE_CURSOR_INFO cciCursor

  HANDLE hout

  hout = GetStdHandle(STD_OUTPUT_HANDLE)

  if(GetConsoleCursorInfo(hout, &cciCursor))

  {

      cciCursor.bVisible = hide

      SetConsoleCursorInfo(hout, &cciCursor)

  }

}

/*设置颜色*/

void set_color(int color)

{

  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color)

}

main()

{

int i,j

show_cursor(0)

srand(time(NULL))

//初始化结构体

for (i=0i<100i++)

{

st[i].x = rand()%80

st[i].y = rand()%20

st[i].ch = rand()%(49-47)+48

}

while (1)

{

for (i=0i<100i++)

{

gotoxy(st[i].x,st[i].y)

set_color(0x2)//最先出现的颜色;

putchar(st[i].ch)

gotoxy(st[i].x,st[i].y-5)

putchar(' ')

st[i].y++

st[i].ch = rand()%(49-47)+48

if (st[i].y-5>=18)

{

gotoxy(st[i].x,st[i].y-1)

putchar(' ')

gotoxy(st[i].x,st[i].y-2)

putchar(' ')

gotoxy(st[i].x,st[i].y-3)

putchar(' ')

gotoxy(st[i].x,st[i].y-4)

putchar(' ')

gotoxy(st[i].x,st[i].y-4)

putchar(' ')

}

if (st[i].y >23)

{

st[i].x = rand()%80

st[i].y = rand()%20

}

gotoxy(st[i].x,st[i].y)

set_color(0xA)//由前一个颜色渐变成的颜色

putchar(st[i].ch)

}

Sleep(120)

}

}

    color(0)   printf("黑色\n")      color(1)   printf("蓝色\n")      color(2)   printf("绿色\n")       color(3)   printf("湖蓝色\n")      color(4)   printf("红色\n")      color(5)   printf("紫色\n")      color(6)   printf("黄色\n")       color(7)   printf("白色\n")      color(8)   printf("灰色\n")      color(9)   printf("淡蓝色\n")      color(10)  printf("淡绿色\n")      color(11)  printf("淡浅绿色\n")       color(12)  printf("淡红色\n")      color(13)  printf("淡紫色\n")      color(14)  printf("淡黄色\n")      color(15)  printf("亮白色\n")

几个基本的颜色;