在mfc中打开console窗口

在mfc中打开console窗口,备忘。

1 console.h

  
  
#ifndef __Console_H__ #define __Console_H__ #include < stdio.h > #include < stdlib.h > #include < stdarg.h > #include < io.h > #include < Windows.h > class Console : public Singleton < ARCConsole > { public : Console() { if ( ! m_hConsoleOutput) { AllocConsole(); m_hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); char str[ 512 ] = " start Console Log...\n " ; WriteConsole(m_hConsoleOutput, str, strlen(str),NULL, NULL); } } void printf( const char * format, ...) { va_list args; int len = 0 ; char str[ 512 ] = { 0 }; va_start(args, format); len = vsprintf(str, format, args) + 1 ; WriteConsole(m_hConsoleOutput, str, len,NULL, NULL); va_end(args); return ; } ~ Console() { if (m_hConsoleOutput) { char str[ 512 ] = " destroy console...\n " ; WriteConsole(m_hConsoleOutput, str, strlen(str),NULL, NULL); CloseHandle(m_hConsoleOutput); FreeConsole(); } } static ARCConsole & getSingleton( void ) { assert( ms_Singleton ); return ( * ms_Singleton ); } static ARCConsole * getSingletonPtr( void ) { return ms_Singleton; } protected : static HANDLE m_hConsoleOutput; }; template <> Console * Ogre::Singleton < Console > ::ms_Singleton = 0 ; HANDLE Console::m_hConsoleOutput = 0 ; extern Console _G_Console; Console _G_Console;
 
偷懒继承了ogre里的singleton类。若不同的lib分别创建了实例,子类记得覆盖父类的getSingleton()和getSingletonPtr()2个函数。

2 singleton.h

  
  
template < typename T > class Singleton { private : /* * \brief Explicit private copy constructor. This is a forbidden operation. */ Singleton( const Singleton < T > & ); /* * \brief Private operator= . This is a forbidden operation. */ Singleton & operator = ( const Singleton < T > & ); protected : static T * ms_Singleton; public : Singleton( void ) { assert( ! ms_Singleton ); #if defined( _MSC_VER ) && _MSC_VER < 1200 int offset = ( int )(T * ) 1 - ( int )(Singleton < T >* )(T * ) 1 ; ms_Singleton = (T * )(( int ) this + offset); #else ms_Singleton = static_cast < T * > ( this ); #endif } ~ Singleton( void ) { assert( ms_Singleton ); ms_Singleton = 0 ; } static T & getSingleton( void ) { assert( ms_Singleton ); return ( * ms_Singleton ); } static T * getSingletonPtr( void ) { return ms_Singleton; } };
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值