WriteConsoleOutputCharacter
2014-01-14 16:32
344 查看
函数简介
函数原型: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);
}
相关文章推荐
- WriteConsoleOutput(三)
- Write debug output to console window
- [c#] where to see console.write output in visual studio?
- WriteConsoleOutput(一)
- WriteConsoleOutput(二)
- Silence output of return object in IRB console on windows
- 解决:Could not write to output file 'c:\windows\Microsoft.NET\Framework\........dll' -- '拒绝访问。 '
- omap35 出现 No more output is seen on the console after "Starting Kernel..." 调试信息
- Unparsed aapt error(s)! Check the console for output"
- 开发中碰到的问题:Unparsed aapt error(s)! Check the console for output.
- no output in console for unittests in pycharm 2017
- C# CONSOLE.WRITELINE打印 小错误
- C#里面Console.Write与Console.WriteLine有什么区别????
- c# Trace.WriteLine Console.WriteLine console.WriteLine 区别
- console.writeline 参数详细说明
- Console.Write("程序猿?开发?写代码?编程?移动、网页、桌面开发?Oh,我连菜鸟都不是!");
- Console.Writeline(“a={0},b={1}”,a,b); 请问这是什么意思
- CS0016: Could not write to output file 'c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/-- '拒绝访问'
- 关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files… ‘Access is denied.’ 的解决办法
- C# Console.WriteLine()的输出格式控制