窗口下調用console實現顯示調試信息

編譯環境vc6.0  根據網上來的,稍微修改了一下:windows

頭文件:spa

// Console.h: interface for the CConsole class.調試

//對象

//////////////////////////////////////////////////////////////////////it

 

#if !defined(AFX_CONSOLE_H__833C45AE_1080_4666_B614_8C925BCB7FD2__INCLUDED_)io

#define AFX_CONSOLE_H__833C45AE_1080_4666_B614_8C925BCB7FD2__INCLUDED_編譯

 

#if _MSC_VER > 1000ast

#pragma onceclass

#endif // _MSC_VER > 1000test

 

 

 

#include <io.h>

#include <fcntl.h>

#include <stdio.h>

#include <windows.h>

typedef   INT_PTR   intptr_t; 

class CConsole  

{

public:

DWORD printf(CString message);

CConsole();

CConsole(LPCTSTR lpszTitle, SHORT ConsoleHeight = 300, SHORT ConsoleWidth = 80);

virtual ~CConsole();

private:

void Attach(SHORT ConsoleHeight, SHORT ConsoleWidth);

    static BOOL IsExistent;

    HANDLE  hStd;

//HANDLE  hStd;

 

};

 

#endif // !defined(AFX_CONSOLE_H__833C45AE_1080_4666_B614_8C925BCB7FD2__INCLUDED_)

cpp文件:
// Console.cpp: implementation of the CConsole class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "test002.h"
#include "Console.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

 BOOL CConsole::IsExistent = FALSE;
CConsole::CConsole()
{
if (IsExistent)
   return;

  AllocConsole();
  Attach(300, 80);

  IsExistent = TRUE;

}

CConsole::CConsole(LPCTSTR lpszTitle, SHORT ConsoleHeight, SHORT ConsoleWidth)
{
  if (IsExistent)
   return;

//  hStda = GetStdHandle(STD_OUTPUT_HANDLE);

  AllocConsole();
  SetConsoleTitle(lpszTitle);
  Attach(ConsoleHeight, ConsoleWidth);

  IsExistent = TRUE;
}

 void CConsole::Attach(SHORT ConsoleHeight, SHORT ConsoleWidth)
 {
 
  int     fd;
  FILE    *file;

// 重定向標準輸入流句柄到新的控制檯窗口

  hStd = GetStdHandle(STD_INPUT_HANDLE);
  fd = _open_osfhandle(reinterpret_cast<intptr_t>(hStd), _O_TEXT); // 文本模式
  file = _fdopen(fd, "r");
  setvbuf(file, NULL, _IONBF, 0); // 無緩衝
  *stdin = *file;

// 重定向標準輸出流句柄到新的控制檯窗口

  hStd = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD size;
  size.X = ConsoleWidth;
  size.Y = ConsoleHeight;
  SetConsoleScreenBufferSize(hStd, size);
  fd = _open_osfhandle(reinterpret_cast<intptr_t>(hStd), _O_TEXT); //文本模式
  file = _fdopen(fd, "w");
  setvbuf(file, NULL, _IONBF, 0); // 無緩衝
  *stdout = *file;

// 重定向標準錯誤流句柄到新的控制檯窗口

  hStd = GetStdHandle(STD_ERROR_HANDLE);
  fd = _open_osfhandle(reinterpret_cast<intptr_t>(hStd), _O_TEXT); // 文本模式
  file = _fdopen(fd, "w");
  setvbuf(file, NULL, _IONBF, 0); // 無緩衝
  *stderr = *file;

 }
CConsole::~CConsole()
{
if (IsExistent)
  {
   FreeConsole();
   IsExistent = FALSE;
  }

}

DWORD CConsole::printf(CString message)
{
DWORD dwWritten=0;
  
    if(WriteConsole(hStd,message,message.GetLength(),&dwWritten,NULL))
        return dwWritten;
    return 0;
}

使用的時候產生對象後直接調用printf輸出想要的調試信息便可
相關文章
相關標籤/搜索