#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <Windows.h>
#include <conio.h>
int main()
{
//定义输出信息
char *str = "Hello World!";
int i;
int len = strlen(str);
//阴影属性
WORD shadow = BACKGROUND_INTENSITY;
//文本属性
WORD text = BACKGROUND_GREEN | BACKGROUND_INTENSITY;
//获得标准输出设备句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//定义窗口缓冲区信息结构体
CONSOLE_SCREEN_BUFFER_INFO screenInfo;
//获得窗口缓冲区信息
GetConsoleScreenBufferInfo(hOut, &screenInfo);
//定义一个文本框输出区域
SMALL_RECT rc;
//定义文本框的起始坐标
COORD posText;
//定义阴影框的起始坐标
COORD posShadow;
//确定区域的边界
rc.Top = 8; //上边界
rc.Bottom = rc.Top + 4; //下边界
rc.Left = (screenInfo.dwSize.X - len) / 2 - 2; //左边界,为了让输出的字符串居中
rc.Right = rc.Left + len + 4; //右边界
//确定文本框起始坐标
posText.X = rc.Left;
posText.Y = rc.Top;
//确定阴影框的起始坐标
posShadow.X = posText.X + 1;
posShadow.Y = posText.Y + 1;
for (i = 0; i < 5; ++i) //先输出阴影框
{
FillConsoleOutputAttribute(hOut, shadow, len + 4, posShadow, NULL);
posShadow.Y++;
}
for (i = 0; i < 5; ++i) //在输出文本框,其中与阴影框重合的部分会被覆盖掉
{
FillConsoleOutputAttribute(hOut, text, len + 4, posText, NULL);
posText.Y++;
}
//设置文本输出处的坐标
posText.X = rc.Left + 2;
posText.Y = rc.Top + 2;
WriteConsoleOutputCharacter(hOut, str, len, posText, NULL); //输出字符串
getchar();
SetConsoleTextAttribute(hOut, screenInfo.wAttributes); // 恢复原来的属性
CloseHandle(hOut);
system("pause");
return 0;
}
出错代码片段为
FillConsoleOutputAttribute(hOut, shadow, len + 4, posShadow, NULL);
来自 https://blog.csdn.net/dengjin20104042056/article/details/90549161