MFC 어플리케이션에서 콘솔에 3개의 함수를 사용해서 간단하게 메세지를 남기는 법에 대해 알아보겠습니다.
몇가지 이유에서 콘솔에 디버깅정보를 보내기를 원하게 되었습니다.
여기서 주의 할점은 어플리케이션을 종료하기 전에는 콘솔창을 닫아서는 안됩니다.
콘솔 만들기
콘솔창을 만들기 위해서 AllocConsole() 을 호출해야 합니다.
이 함수는 어떤 파라미터도 받지 않고 BOOL을 리턴해줍니다.
코드가 실행되면서 콘솔을 만들어야 합니다.
CWinApp::OnInitInstance를 오버라이드에서 코드를 입력했습니다.
아래와 같습니다 :
BOOL CSmplConsoleApp::InitInstance()
{
// 콘솔 할당
#ifdef _DEBUG
if (!AllocConsole())
AfxMessageBox("Failed to create the console!", MB_ICONEXCLAMATION);
#endif
// 여기에서 부터 프레임을 만듭니다.
pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
메인 윈도우가 만들어지기전에 콘솔창을 만들어야 하는게 중요합니다.
콘솔이 만들어지지 않는다면 _cprintf()로 보내는 디버깅 메세지가 콘솔창에 도달하지 못할겁니다.
이게 왜그러는지는 알수가 없네요. 아시는 분은 설명 부탁좀 드리겠습니다.
콘솔에 쓰기
콘솔에 출력은 _cprintf()를 통해서 보냅니다.
int _cprintf( const char *format [, argument] ... );
형식은 printf()와 같습니다.
_cprintf()를 사용하기위해서는 #include <conio.h>을 추가하는걸 잊어선 안됩니다.
CChildeView::OnPaint() 오버라이드한 데모는 아래와 같습니다 :
void CChildView::OnPaint()
{
CPaintDC dc(this);
// device context for painting
// TODO: Add your message handler code here
#ifdef _DEBUG
static int nCallCounter = 0;
nCallCounter++;
_cprintf("Window painted now %i time(s)\n", nCallCounter);
#endif
// Do not call CWnd::OnPaint() for painting messages
}
콘솔의 제거
프로그램을 종료하기 전에 FreeConsole()로 콘솔을 해제해 줘야 합니다.
이 함수도 역시 파라미터는 없으면 BOOL을 리턴해줍니다.
CWinApp::ExitInstance()를 오버라이한 데모는 아래와 같습니다.
int CSmplConsoleApp::ExitInstance()
{
// deallocate console
#ifdef _DEBUG
if (!FreeConsole())
AfxMessageBox("Could not free the console!");
#endif return
CWinApp::ExitInstance();
댓글을 달아 주세요