求助高手:控制台画图问题(在线求解)
那个高手帮我解答下如和在控制台上画一个有阴影的矩形。百度上的我用VC++6.0编译不了。!
程序代码:#include <windows.h>
HANDLE hOut;
void ShadowWindowLine(char *str);
void DrawBox(bool bSingle, SMALL_RECT rc); // 绘制边框
int main(void)
{
hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄
SetConsoleOutputCP(437); // 设置代码页,这里如果设置成936(简体中文),那么程序会怎样?那样的话,将画不出边框。
ShadowWindowLine("Display a line of words, and center the window with shadow.");
CloseHandle(hOut); // 关闭标准输出设备句柄
return 0;
}
void ShadowWindowLine(char *str)
{
SMALL_RECT rc;
CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息
WORD att0,att1,attText;
int i, chNum = strlen(str);
GetConsoleScreenBufferInfo( hOut, &bInfo ); // 获取窗口缓冲区信息
// 计算显示窗口大小和位置
rc.Left = (bInfo.dwSize.X - chNum)/2 - 2;
rc.Top = 8; // 原代码段中此处为bInfo.dwSize.Y/2 - 2,但是如果您的DOS屏幕有垂直滚动条的话,还需要把滚动条下拉才能看到,为了方便就把它改为10
rc.Right = rc.Left + chNum + 4;
rc.Bottom = rc.Top + 4;
att0 = BACKGROUND_INTENSITY; // 阴影属性
att1 = FOREGROUND_RED |FOREGROUND_GREEN |FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE;// 文本属性
attText = FOREGROUND_RED |FOREGROUND_INTENSITY; // 文本属性
// 设置阴影然后填充
COORD posShadow = {rc.Left+1, rc.Top+1}, posText = {rc.Left, rc.Top};
for (i=0; i<5; i++)
{
FillConsoleOutputAttribute(hOut, att0, chNum + 4, posShadow, NULL);
posShadow.Y++;
}
for (i=0;i<5;i++)
{
FillConsoleOutputAttribute(hOut, att1,chNum + 4, posText, NULL);
posText.Y++;
}
// 写文本和边框
posText.X = rc.Left + 2;
posText.Y = rc.Top + 2;
WriteConsoleOutputCharacter(hOut, str, strlen(str), posText, NULL);
DrawBox(true, rc);
SetConsoleTextAttribute(hOut, bInfo.wAttributes); // 恢复原来的属性
}
void DrawBox(bool bSingle, SMALL_RECT rc) // 函数功能:画边框
{
char chBox[6];
COORD pos;
if (bSingle)
{
chBox[0] = (char)0xda; // 左上角点
chBox[1] = (char)0xbf; // 右上角点
chBox[2] = (char)0xc0; // 左下角点
chBox[3] = (char)0xd9; // 右下角点
chBox[4] = (char)0xc4; // 水平
chBox[5] = (char)0xb3; // 坚直
}
else
{
chBox[0] = (char)0xc9; // 左上角点
chBox[1] = (char)0xbb; // 右上角点
chBox[2] = (char)0xc8; // 左下角点
chBox[3] = (char)0xbc; // 右下角点
chBox[4] = (char)0xcd; // 水平
chBox[5] = (char)0xba; // 坚直
}
// 画边框的上 下边界
for(pos.X = rc.Left+1;pos.X<rc.Right-1;pos.X++)
{
pos.Y = rc.Top;
// 画上边界
WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL);
// 画左上角
if(pos.X == rc.Left+1)
{
pos.X--;
WriteConsoleOutputCharacter(hOut, &chBox[0],1, pos, NULL);
pos.X++;
}
// 画右上角
if(pos.X == rc.Right-2)
{
pos.X++;
WriteConsoleOutputCharacter(hOut, &chBox[1], 1, pos, NULL);
pos.X--;
}
pos.Y = rc.Bottom;
// 画下边界
WriteConsoleOutputCharacter(hOut, &chBox[4], 1, pos, NULL);
// 画左下角
if(pos.X == rc.Left+1)
{
pos.X--;
WriteConsoleOutputCharacter(hOut, &chBox[2], 1, pos, NULL);
pos.X++;
}
// 画右下角
if(pos.X==rc.Right-2)
{
pos.X++;
WriteConsoleOutputCharacter(hOut, &chBox[3], 1, pos, NULL);
pos.X--;
}
}
// 画边框的左右边界
for (pos.Y = rc.Top+1; pos.Y<=rc.Bottom-1; pos.Y++)
{
pos.X = rc.Left;
// 画左边界
WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL);
pos.X = rc.Right-1;
// 画右边界
WriteConsoleOutputCharacter(hOut, &chBox[5], 1, pos, NULL);
}
}
为什么我用VC++6.0编译不了啊、~
错误代码:
--------------------Configuration: 2 - Win32 Debug--------------------
Compiling...
2.c
c:\documents and settings\administrator\桌面\2.c(4) : error C2146: syntax error : missing ')' before identifier 'bSingle'
c:\documents and settings\administrator\桌面\2.c(4) : error C2061: syntax error : identifier 'bSingle'
c:\documents and settings\administrator\桌面\2.c(4) : error C2059: syntax error : ';'
c:\documents and settings\administrator\桌面\2.c(4) : error C2059: syntax error : ','
c:\documents and settings\administrator\桌面\2.c(4) : error C2059: syntax error : ')'
c:\documents and settings\administrator\桌面\2.c(29) : error C2275: 'COORD' : illegal use of this type as an expression
d:\vc98\include\wincon.h(32) : see declaration of 'COORD'
c:\documents and settings\administrator\桌面\2.c(29) : error C2146: syntax error : missing ';' before identifier 'posShadow'
c:\documents and settings\administrator\桌面\2.c(29) : error C2065: 'posShadow' : undeclared identifier
c:\documents and settings\administrator\桌面\2.c(29) : error C2059: syntax error : '{'
c:\documents and settings\administrator\桌面\2.c(32) : error C2115: 'function' : incompatible types
c:\documents and settings\administrator\桌面\2.c(32) : warning C4024: 'FillConsoleOutputAttribute' : different types for formal and actual parameter 4
c:\documents and settings\administrator\桌面\2.c(33) : error C2224: left of '.Y' must have struct/union type
c:\documents and settings\administrator\桌面\2.c(37) : error C2065: 'posText' : undeclared identifier
c:\documents and settings\administrator\桌面\2.c(37) : error C2115: 'function' : incompatible types
c:\documents and settings\administrator\桌面\2.c(37) : warning C4024: 'FillConsoleOutputAttribute' : different types for formal and actual parameter 4
c:\documents and settings\administrator\桌面\2.c(38) : error C2224: left of '.Y' must have struct/union type
c:\documents and settings\administrator\桌面\2.c(41) : error C2224: left of '.X' must have struct/union type
c:\documents and settings\administrator\桌面\2.c(42) : error C2224: left of '.Y' must have struct/union type
c:\documents and settings\administrator\桌面\2.c(43) : error C2115: 'function' : incompatible types
c:\documents and settings\administrator\桌面\2.c(43) : warning C4024: 'WriteConsoleOutputCharacterA' : different types for formal and actual parameter 4
c:\documents and settings\administrator\桌面\2.c(44) : warning C4013: 'DrawBox' undefined; assuming extern returning int
c:\documents and settings\administrator\桌面\2.c(44) : error C2065: 'true' : undeclared identifier
c:\documents and settings\administrator\桌面\2.c(47) : error C2146: syntax error : missing ')' before identifier 'bSingle'
c:\documents and settings\administrator\桌面\2.c(47) : error C2061: syntax error : identifier 'bSingle'
c:\documents and settings\administrator\桌面\2.c(47) : error C2059: syntax error : ';'
c:\documents and settings\administrator\桌面\2.c(47) : error C2059: syntax error : ','
c:\documents and settings\administrator\桌面\2.c(47) : error C2059: syntax error : ')'
执行 cl.exe 时出错.
2.obj - 1 error(s), 0 warning(s)
[ 本帖最后由 LynnA 于 2010-11-3 20:44 编辑 ]










