词条 | WriteConsoleOutputCharacter |
释义 | 函数简介函数原型: BOOL WriteConsoleOutputCharacter( // 在指定位置处插入指定数量的字符 HANDLE hConsoleOutput, // 句柄 LPCTSTR lpCharacter, // 字符串 DWORD nLength, // 字符个数 COORD dwWriteCoord, // 起始位置 LPDWORD lpNumberOfCharsWritten // 已写个数 ); 参数简介: hConsoleOutput:控制台输出句柄,通过调用GetStdHandle函数获得 HANDLE hnd; hnd=GetStdHandle(STD_INPUT_HANDLE); lpCharacter:要输出的字符串 nLength:输出长度 dwWriteCoord:起始位置 其中,COORD是个结构体变量类型 typedef struct _COORD { SHORT X; SHORT Y; } COORD; lpNumberOfCharsWritten:已写个数,通常置为NULL 程序示例示例一: #include <windows.h> #include <string.h> int main(void) { COORD size = {10, 10}; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); WriteConsoleOutputCharacter(hOut,"Hello,World",strlen("Hello,World"),size,NULL); return 0; } 示例二: // 在DOS窗口中央输出“Hello,World” #include <windows.h> #include <stdio.h> #include <string.h> int main(void) { COORD size; int length; CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo( hOut, &bInfo ); // 获取窗口缓冲区信息 char *strText = "Hello,World!"; length = strlen(strText); size.X = ( bInfo.dwSize.X - length ) / 2; size.Y = bInfo.dwSize.Y / 2; WriteConsoleOutputCharacter(hOut,strText,length,size,NULL); return 0; } 示例三: #include<iostream> #include<iomanip> #include<windows.h> using namespace std; void Draw(HANDLE hand,int colour,char *s,int len,COORD point,LPDWORD lPdword); int main() { int x=4; int y=4; HANDLE hand; hand=GetStdHandle(STD_OUTPUT_HANDLE); int colour=200; int len=2; COORD point = {x,y}; SMALL_RECT rt = {0,0,35,32}; // 窗口尺寸 SetConsoleWindowInfo(hand,true,&rt); // 由于DOS默认只有25行,为了便于观察,把dos窗口行数调为35,这个也可以用system函数实现 char *string="■"; Draw(hand,colour,string,len,point,NULL); INPUT_RECORD keyRec; DWORD res; HANDLE hnd; hnd=GetStdHandle(STD_INPUT_HANDLE); for(;;) { cout<<setfill('0')<<"("<<setw(2)<<point.X<<","<<setw(2)<<point.Y<<")"; ReadConsoleInput(hnd,&keyRec,1,&res); if(keyRec.EventType==KEY_EVENT) { if(keyRec.Event.KeyEvent.wVirtualKeyCode==65) { if(point.X>1) // 这里不用那么麻烦,因为按a时只会使X减小,下同 { Draw(hand,1," ",len,point,NULL); point.X-=1; Draw(hand,colour,string,len,point,NULL); } } if(keyRec.Event.KeyEvent.wVirtualKeyCode==68) { if(point.X<30) { Draw(hand,1," ",len,point,NULL); point.X+=1; Draw(hand,colour,string,len,point,NULL); } } if(keyRec.Event.KeyEvent.wVirtualKeyCode==87) { if(point.Y>1) { Draw(hand,1," ",len,point,NULL); point.Y-=1; Draw(hand,colour,string,len,point,NULL); } } if(keyRec.Event.KeyEvent.wVirtualKeyCode==83) { if(point.Y<30) { Draw(hand,1," ",len,point,NULL); point.Y+=1; Draw(hand,colour,string,len,point,NULL); } } } cout<<"\\b\\b\\b\\b\\b\\b\\b"; } } void Draw(HANDLE hand,int colour,char *s,int len,COORD point,LPDWORD lPdword) { FillConsoleOutputAttribute(hand,colour,len,point,lPdword); WriteConsoleOutputCharacter(hand,s,len,point,lPdword); } |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。