__fastcall TForm1::TForm1(TComponent*
Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
ConWin.Open();
//打开控制台窗口
ConWin.Caption
= "控制台视窗测试程序";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
ConWin.Execute("dir");
//在控制台视窗里面执行 DOS 命令
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
ConWin.printf("在控制台视窗里面显示文字\n");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
ConWin.ClrScr();
//清屏
SMALL_RECT r;
ConWin.GetRect(r);
ConWin.MoveTo((r.Right+r.Left-30)/2,
(r.Bottom+r.Top)/2);
ConWin.SetColor(FOREGROUND_GREEN|FOREGROUND_INTENSITY|BACKGROUND_BLUE);
ConWin.printf("屏幕中央显示蓝色背景的绿色文字");
ConWin.MoveTo(r.Left,r.Bottom); //光标移动到左下角
ConWin.SetColor(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
ConWin.Close();
//关闭控制台窗口
}
//---------------------------------------------------------------------------
--------------------next---------------------
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include
//---------------------------------------------------------------------------
class TConsoleWindow
{
public:
TConsoleWindow() { hCon=NULL;
}
~TConsoleWindow() { Close(); }
void Open(void);
void
Close(void);
int Execute(AnsiString
s);
DWORD printf(const char *f,...);
void MoveTo(short x, short y);
void
SetColor(WORD c);
void GetRect(SMALL_RECT
&r);
void ClrScr(WORD c=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
__property HANDLE Handle =
{ read = hCon };
__property AnsiString
Caption = { read = fGetCaption, write = fSetCaption };
private:
HANDLE hCon;
AnsiString
fGetCaption(void);
void fSetCaption(AnsiString);
};
//---------------------------------------------------------------------------
#endif
--------------------next---------------------
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
void TConsoleWindow::Open(void)
{
if(!hCon)
{
AllocConsole();
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
}
}
//---------------------------------------------------------------------------
void TConsoleWindow::Close(void)
{
if(hCon)
{
FreeConsole();
hCon = NULL;
}
}
//---------------------------------------------------------------------------
AnsiString TConsoleWindow::fGetCaption(void)
{
char s[260];
GetConsoleTitle(s,260);
return AnsiString(s);
}
//---------------------------------------------------------------------------
void TConsoleWindow::fSetCaption(AnsiString s)
{
SetConsoleTitle(s.c_str());
}
//---------------------------------------------------------------------------
DWORD TConsoleWindow::printf(const char *f,...)
{
AnsiString s;
va_list
argptr;
va_start(argptr, f);
s.vprintf(f, argptr);
va_end(argptr);
DWORD dwWritten=0;
if(WriteConsole(hCon,s.c_str(),s.Length(),&dwWritten,NULL))
return dwWritten;
return 0;
}
//---------------------------------------------------------------------------
void TConsoleWindow::MoveTo(short x, short y)
{
COORD CurPos = {x,y};
SetConsoleCursorPosition(hCon,
CurPos);
}
//---------------------------------------------------------------------------
void TConsoleWindow::SetColor(WORD c)
{
SetConsoleTextAttribute(hCon,
c);
}
//---------------------------------------------------------------------------
void TConsoleWindow::GetRect(SMALL_RECT &r)
{
CONSOLE_SCREEN_BUFFER_INFO ConInfo;
GetConsoleScreenBufferInfo(hCon, &ConInfo);
r = ConInfo.srWindow;
}
//---------------------------------------------------------------------------
int TConsoleWindow::Execute(AnsiString s)
{
return system(s.c_str());
}
//---------------------------------------------------------------------------
void TConsoleWindow::ClrScr(WORD c)
{
DWORD nBytesWrite;
CONSOLE_SCREEN_BUFFER_INFO
ConInfo;
GetConsoleScreenBufferInfo(hCon,
&ConInfo);
COORD CurPos = {
CurPos.X = ConInfo.srWindow.Left, CurPos.Y = ConInfo.srWindow.Top
};
FillConsoleOutputAttribute(hCon,
c, ConInfo.dwSize.X*ConInfo.dwSize.Y, CurPos, &nBytesWrite);
FillConsoleOutputCharacter(hCon, 0,
ConInfo.dwSize.X*ConInfo.dwSize.Y, CurPos, &nBytesWrite);
}
//---------------------------------------------------------------------------
--------------------next---------------------
阅读(1669) | 评论(0) | 转发(0) |