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

2程序示例

示例一:
#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);
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值