Debug Console class

작성자

카테고리:

H

class CDebugConsole : public CSingleton<CDebugConsole>
{
friend class CSingleton<CDebugConsole>;

private:
HANDLE m_hOut;
HWND m_hWnd;

public:
enum { CSL_TR = 0, CSL_TL, CSL_BR, CSL_BL }; // Edge

private:
CDebugConsole()
{
this->Initialize();
}
virtual ~CDebugConsole()
{
this->Release();
}

VOID Initialize();
VOID Release();

VOID GetConsoleHwnd( HWND&amp; _hWnd );
VOID GetEdgePosition( DWORD _Edge );

public:
// CDebugConsole::m_Console().Message( " " );
VOID Message( LPWSTR str );
VOID Message( LPSTR str );

// CDebugConsole::m_Console().Messagef( "%s %d %f", char, int, float );
VOID Messagef( LPWSTR str, ... );
VOID Messagef( LPSTR str, ... );

// 모니터가 수평 배치가 아닌 수직 배치일때는 지원하지 않음
VOID SetPosition( DWORD _dEdge, BOOL _bTopMost );
};


CPP

#include "stdafx.h"
#include "DebugConsole.h"

VOID CDebugConsole::Initialize()
{
    if( !AllocConsole() )
    {
        MessageBox( NULL, L"CDebugConsole::AllocConsole()", NULL, MB_OK );
        return;
    }

    if( ( m_hOut = CreateFile(L"CONOUT$",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0) ) == INVALID_HANDLE_VALUE )
    {
        MessageBox( NULL, L"CDebugConsole::Initialize() failed", NULL, MB_OK );
        return;
    }

    SetConsoleTitle( L"1.0v" );
}

VOID CDebugConsole::Release()
{
    CloseHandle( m_hOut );
    FreeConsole();
}

VOID CDebugConsole::GetConsoleHwnd( HWND* _hWnd )
{
    const INT BUFSIZE = 1024;

    TCHAR pszNewWindowTitle[ BUFSIZE ]; // Contains fabricated
    TCHAR pszOldWindowTitle[ BUFSIZE ]; // Contains original

    // Fetch current window title.
    GetConsoleTitle(pszOldWindowTitle, BUFSIZE);

    wsprintf( pszNewWindowTitle, L"%d/%d", GetTickCount(), GetCurrentProcessId() );

    // Change current window title.
    SetConsoleTitle(pszNewWindowTitle);

    // Ensure window title has been updated.
    Sleep(40);

    // Look for NewWindowTitle.
    _hWnd = FindWindow(NULL, pszNewWindowTitle);

    // Restore original window title.
    SetConsoleTitle(pszOldWindowTitle);
}

VOID CDebugConsole::GetEdgePosition( DWORD _Edge )
{
    // Get Display Impormation
    HDC hDC;
    hDC = CreateDC( TEXT("DISPLAY"), NULL, NULL, NULL );
    INT iWidth = GetDeviceCaps( hDC, HORZRES );
    INT iHeight = GetDeviceCaps( hDC, VERTRES );
    INT iNumMonitor = GetSystemMetrics( SM_CMONITORS);
    DeleteDC( hDC );

    this->;Messagef( L"모니터 갯수 : %dn 해상도 : %d * %dn", iNumMonitor, iWidth, iHeight );

    iWidth *= iNumMonitor;

    RECT rt;
    GetClientRect( m_hWnd, *rt );
    INT x = 0, y = 0;

    switch( _Edge )
    {
        case CSL_TL:
            x = 0;
            y = 0;
            break;
        case CSL_TR:
            x = iWidth - rt.right;
            y = 0;
            break;
        case CSL_BL:
            x = 0;
            y = iHeight - rt.bottom;
            break;
        case CSL_BR:
            x = iWidth - rt.right;
            y = iHeight - rt.bottom;
            break;
    }

    MoveWindow( m_hWnd, x, y, rt.right, rt.bottom, FALSE );
}

VOID CDebugConsole::Message( LPWSTR _str )
{
    DWORD sz;

    // 멀티바이트 ->; 유니코드
    CHAR str[ 256 ];
    WideCharToMultiByte( CP_ACP, 0, _str, -1, str, 256, NULL, NULL );
    // End

    WriteFile( m_hOut, str, strlen( str ), *sz, NULL );
}

VOID CDebugConsole::Message( LPSTR _str )
{
    DWORD sz;

    WriteFile( m_hOut, _str, strlen( _str ), *sz, NULL );
}

VOID CDebugConsole::Messagef( LPWSTR _str, ... )
{
    // 멀티바이트 -> 유니코드
    CHAR str[ 256 ];
    WideCharToMultiByte( CP_ACP, 0, _str, -1, str, 256, NULL, NULL );
    // End

    va_list argp;
    CHAR szBuf[ 1024 ];

    va_start( argp, _str );
    vsprintf_s( szBuf, str, argp );
    va_end( argp );

    DWORD sz;
    WriteFile( m_hOut, szBuf, strlen( szBuf ), *sz, NULL );
}

VOID CDebugConsole::Messagef( LPSTR _str, ... )
{
    va_list argp;
    CHAR szBuf[ 1024 ];

    va_start( argp, _str );
    vsprintf_s( szBuf, _str, argp );
    va_end( argp );

    DWORD sz;
    WriteFile( m_hOut, szBuf, strlen( szBuf ), *sz, NULL );
}

VOID CDebugConsole::SetPosition( DWORD _dEdge, BOOL _bTopMost )
{
    // Get HWND
    GetConsoleHwnd( m_hWnd );

    // Set Position
    GetEdgePosition( _dEdge );

    // Set TopMost
    if( _bTopMost )
    SetWindowPos( m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
    else
    SetWindowPos( m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );

}