在WinXP里面,窗口的滚动对文本的颜色不会有任何问题,但是在Win98里面,一旦控制台文本需要向上滚动,就会导致你的颜色文本显示错误,应该是Win98的控制台对屏幕缓冲区控制有BUG,所以必须受冻滚动文本显示,否则颜色显示不正常!
procedure WriteEx(const S: string; Color: Word);
var
Written: DWord;
TmpAtt: TConsoleScreenBufferInfo;
OldAtt: Word;
Console: THandle;
begin
Console := OutputHandle;
GetConsoleScreenBufferInfo(Console, TmpAtt);
OldAtt := TmpAtt.wAttributes;
SetConsoleTextAttribute(Console, Color);
WriteConsole(Console, PChar(S), Length(S), Written, nil);
SetConsoleTextAttribute(Console, OldAtt);
end;
procedure WriteLnEx(const S: string; Color: Word);
var
c: TCoord;
ci: TCharInfo;
sr: TSmallRect;
TmpAtt: TConsoleScreenBufferInfo;
Written: DWord;
OldAtt: Word;
Console: THandle;
begin
Console := OutputHandle;
GetConsoleScreenBufferInfo(Console, TmpAtt);
OldAtt := TmpAtt.wAttributes;
SetConsoleTextAttribute(Console, Color);
WriteConsole(Console, PChar(S), Length(S), Written, nil);
GetConsoleScreenBufferInfo(Console, TmpAtt);
if TmpAtt.dwCursorPosition.Y >= Pred(TmpAtt.dwSize.Y) then
begin
sr.Left := 0;
sr.Top := 1;
sr.Right := TmpAtt.dwSize.X;
sr.Bottom := TmpAtt.dwSize.Y;
c.X := 0;
c.Y := 0;
ci.UniCodeChar := #32;
ci.Attributes := OldAtt;
ScrollConsoleScreenBuffer(Console, sr, nil, c, ci);
c.X := 0;
c.Y := TmpAtt.srWindow.Bottom;
SetConsoleCursorPosition(Console, c);
end
else
begin
c.X := 0;
c.Y := TmpAtt.dwCursorPosition.Y + 1;
SetConsoleCursorPosition(Console, c);
end;
SetConsoleTextAttribute(Console, OldAtt);
end;