//---------------------------------------------------------------------------
#pragma hdrstop
#include "TConsoleWindow.h"
#include <Windows.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
int get_sys_time(char* buf)
{
FILETIME fileTime;
SYSTEMTIME systemTime;
//char buffer[20];
GetSystemTimeAsFileTime(&fileTime);
SystemTimeToFileTime((CONST SYSTEMTIME*)&fileTime, (LPFILETIME)&fileTime);
FileTimeToSystemTime(&fileTime, &systemTime);
// 格式化时间字符串
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d.%03d ", systemTime.wYear,
systemTime.wMonth, systemTime.wDay, systemTime.wHour,
systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
return 1;
}
void CharToTchar(const char* _char, TCHAR* tchar)
{
int iLength;
iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);
}
void TConsoleWindow::Open(void)
{
if (!hCon) {
AllocConsole();
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
HWND hwnd = GetConsoleWindow(); // 获取控制台窗口句柄
RECT rect;
GetWindowRect(hwnd, &rect); // 获取窗口大小和位置
//int x = (GetSystemMetrics(SM_CXSCREEN) - rect.right + rect.left) / 2+100;
//int y = (GetSystemMetrics(SM_CYSCREEN) - rect.bottom + rect.top) / 2+300;
//SetWindowPos(hwnd, HWND_TOP, 1500, 500, 1000, 6000, SWP_NOMOVE | SWP_NOSIZE); // 将窗口置于屏幕中央
//SWP_NOSIZE: 保持当前大小,即cx和cy会被忽略。
//SWP_NOMOVE: 保持当前位置,即x和y不会被改变。
SetWindowPos(hwnd, HWND_TOP, 600, 0, 800, 1000, 0);
}
}
//---------------------------------------------------------------------------
void TConsoleWindow::Close(void)
{
if (hCon) {
FreeConsole();
hCon = NULL;
}
}
//---------------------------------------------------------------------------
void TConsoleWindow::Show(void)
{
ShowWindow(GetConsoleWindow(), SW_SHOW);
}
//---------------------------------------------------------------------------
void TConsoleWindow::Hide(void)
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
//---------------------------------------------------------------------------
DWORD TConsoleWindow::printf(const char* f, ...)
{
HANDLE hStdout;
DWORD dwChars;
int len;
//COORD pos = { 10, 100 }; // 设置起始坐标为 (10, 10)
char buf2[512];
TCHAR buf[1024];
int colorCodes[] = { FOREGROUND_RED | FOREGROUND_GREEN, // 红色和绿色
FOREGROUND_BLUE | FOREGROUND_GREEN, // 蓝色和绿色
FOREGROUND_RED | FOREGROUND_BLUE, // 红色和蓝色
FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE // 红色、绿色和蓝色
};
static int colorIndex = 0;
get_sys_time(buf2);
len = strlen(buf2);
va_list args;
va_start(args, f);
vsprintf(&buf2[len], f, args);
va_end(args);
CharToTchar(buf2, buf);
len = lstrlen(buf);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdout == INVALID_HANDLE_VALUE) {
return -1;
}
//SetConsoleCursorPosition(hStdout,pos); /* 设置控制台光标坐标(设备句柄, 光标坐标) */
//colorIndex % 3
SetConsoleTextAttribute(hStdout, colorCodes[colorIndex++ % 4]);
WriteConsole(hStdout, buf, len, &dwChars, NULL);
}
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE)
return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo(hStdOut, &csbi))
return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut, (TCHAR)' ', cellCount, homeCoords, &count))
return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut, csbi.wAttributes, cellCount, homeCoords, &count))
return;
/* Move the cursor home */
SetConsoleCursorPosition(hStdOut, homeCoords);
}
//---------------------------------------------------------------------------
void TConsoleWindow::ClrScr(WORD c)
{
ClearScreen();
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef TConsoleWindowH
#define TConsoleWindowH
//---------------------------------------------------------------------------
#include <vcl.h>
//---------------------------------------------------------------------------
class TConsoleWindow
{
public:
TConsoleWindow() { hCon=NULL; }
~TConsoleWindow() { Close(); }
void Open(void);
void Close(void);
void Show(void);
void Hide(void);
DWORD printf(const char *f,...);
void ClrScr(WORD c=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
__property HANDLE Handle = { read = hCon };
private:
HANDLE hCon;
};
#endif
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
TConsoleWindow console;
//---------------------------------------------------------------------------
#endif