c++builder vcl窗体调用控制台 多彩控制 支持形参 时间

//---------------------------------------------------------------------------

#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

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值