/*console.h*/
#ifndef CONSOLE_DEBUG_WINDOW
#define CONSOLE_DEBUG_WINDOW
#include "stdlib.h"
#include "stdio.h"
#include "windows.h"
class TMyOutputConsole
{
private:
FILE *__fStdOut;
HANDLE __hStdOut;
int iWidth;
int iHeight;
char *pFname;
void __fastcall startConsoleWin(int,int,char *);
bool __fastcall EndConsoleWin();
protected:
public:
__fastcall TMyOutputConsole(int width = 80,int height=25,char *fname=NULL);
__fastcall ~TMyOutputConsole();
HANDLE __fastcall GetConsoleHanle();
int __fastcall wprintf(char *fmt);
};
#endif
/*console.cpp*/
#include "console.h"
__fastcall TMyOutputConsole::TMyOutputConsole(int width,int height,char *fname)
{
__fStdOut = NULL;
__hStdOut = NULL;
iWidth = width;
iHeight = height;
pFname = fname;
startConsoleWin(iWidth,iHeight,pFname);
}
__fastcall TMyOutputConsole::~TMyOutputConsole()
{
EndConsoleWin();
}
void __fastcall TMyOutputConsole::startConsoleWin(int width,int height,char *fname)
{
//
AllocConsole();
SetConsoleTitle("Debug Window");
__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
char szBuff[100];
sprintf(szBuff,"My Handle is %08X\n",__hStdOut);
wprintf(szBuff);
COORD co;
co.X = (short)width;
co.Y = (short)height;
SetConsoleScreenBufferSize(__hStdOut,co);
if(fname)
__fStdOut = fopen(fname,"w");
}
HANDLE __fastcall TMyOutputConsole::GetConsoleHanle()
{
return __hStdOut;
}
bool __fastcall TMyOutputConsole::EndConsoleWin()
{
if(__hStdOut)
{
return(FreeConsole());
}
return false;
}
int __fastcall TMyOutputConsole::wprintf(char *fmt)
{
char s[300];
va_list argptr;
int cnt;
va_start(argptr,fmt);
cnt = vsprintf(s,fmt,argptr);
va_end(artptr);
DWORD cCharsWritten;
if(__hStdOut)
WriteConsole(__hStdOut,s,strlen(s),&cCharsWritten,NULL);
if(__fStdOut)
fprintf(__fStdOut,s);
return cnt;
}
/****************************************************************************/
现在关键是如何抓住WM_DESTROY消息? |