看了一下非常方便,收藏.
用VC写WIN32程序,没有控制台输出查看参数确实很不方便,这个功能提供了创建windows窗口的同时,又调出DOS界面,方便调试参数.
http://www.cnblogs.com/mr-wid/archive/2013/04/10/3012365.html
// 就一个头文件 mdebug.h
HWND hConsole = (HWND)GetStdHandle( STD_OUTPUT_HANDLE ); // 这里需要做个强制转换...
//mdebug.h
#pragma once
//
#include <windows.h>
#include <conio.h>
#include <stdio.h>
//
void debugInit(); //初始化调试库
void debugFree(); //关闭调试
//输出类函数
void mPuts( char *szFormat, ... ); //基础数据类型格式化输出
void mPutsEx( char *szFormat, ... ); //增强型数据类型格式化输出
//检测类函数
BOOL matchString( const char *szSrc, char **szDest ); //串匹配
//
int printPOINT( POINT ); //输出一个点的坐标信息, 输出格式 (x, y)
int printRECT( RECT ); //输出一个矩形的坐标信息, 输出格式 (left=left, top=top, right=right, bottom=bottom)
int printSIZE( SIZE ); //输出一个尺寸的坐标信息, 输出格式 [cx=cx, cy=cy]
int printRGBQUAD( RGBQUAD ); //输出一组 RGB 值, 输出格式, (r=r, g=g, b=b)
//
void debugInit()
{
AllocConsole();
}
void debugFree()
{
FreeConsole();
}
//
//基础数据类型格式化输出
void mPuts( char *szFormat, ... )
{
char szBuffer[10*1024] = {0};
va_list pArgList;
va_start( pArgList, szFormat );
_vsnprintf( szBuffer, sizeof(szBuffer)/sizeof(char), szFormat, pArgList );
va_end(pArgList);
_cputs( szBuffer );
}
//增强数据类型输出
void mPutsEx( char *szFormat, ... )
{
va_list pArgList;
POINT pt;
RECT rect;
SIZE size;
RGBQUAD rgb;
HWND hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
va_start( pArgList, szFormat );
while( *szFormat )
{
switch( *szFormat )
{
case '%':
{
szFormat++;
//
//增强型数据类型输出
if( matchString( "pt", &szFormat ) ) // %pt == POINT Structure
{
pt = va_arg( pArgList, POINT );
printPOINT( pt );
}
//
if( matchString( "rect", &szFormat ) ) // %rect == RECT Structure
{
rect = va_arg( pArgList, RECT );
printRECT( rect );
}
//
if( matchString( "size", &szFormat ) ) // %size == SIZE Structure
{
size = va_arg( pArgList, SIZE );
printSIZE( size );
}
//
if( matchString( "rgb", &szFormat ) ) // %rgb == RGBQUAD Structure
{
rgb = va_arg( pArgList, RGBQUAD );
printRGBQUAD( rgb );
}
//
if( matchString( "endfr", &szFormat ) ) // %endfr == 结束前景颜色格式化输出
SetConsoleTextAttribute( hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
if( matchString( "endbk", &szFormat ) ) // %endbk == 结束背景颜色格式化输出
{
SetConsoleTextAttribute( hConsole, 0 );
SetConsoleTextAttribute( hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
}
//--------------------色彩格式化输出---------------------
//前景颜色
if( matchString( "frred", &szFormat ) ) // %frred == 前景颜色, 红色
SetConsoleTextAttribute( hConsole, FOREGROUND_RED );
if( matchString( "frgreen", &szFormat ) ) // %frgreen == 前景颜色, 绿色
SetConsoleTextAttribute( hConsole, FOREGROUND_GREEN );
if( matchString( "frblue", &szFormat ) ) // %frblue == 前景颜色, 蓝色
SetConsoleTextAttribute( hConsole, FOREGROUND_BLUE );
if( matchString( "fryellow", &szFormat ) ) // %fryellow == 前景颜色, 黄色
SetConsoleTextAttribute( hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY );
if( matchString( "frcyan", &szFormat ) ) // %frfrcyan == 前景颜色, 青色
SetConsoleTextAttribute( hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
if( matchString( "frmag", &szFormat ) ) // %frmag == 前景颜色, 品红
SetConsoleTextAttribute( hConsole, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
//背景颜色
if( matchString( "bkred", &szFormat ) ) // %bkred == 背景颜色, 红色
SetConsoleTextAttribute( hConsole, BACKGROUND_RED );
if( matchString( "bkgreen", &szFormat ) ) // %bkgreen == 背景颜色, 绿色
SetConsoleTextAttribute( hConsole, BACKGROUND_GREEN );
if( matchString( "bkblue", &szFormat ) ) // %bkblue == 背景颜色, 蓝色
SetConsoleTextAttribute( hConsole, BACKGROUND_BLUE );
if( matchString( "bkyellow", &szFormat ) ) // %bkyellow == 背景颜色, 黄色
SetConsoleTextAttribute( hConsole, BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY );
if( matchString( "bkcyan", &szFormat ) ) // %bkfrcyan == 背景颜色, 青色
SetConsoleTextAttribute( hConsole, BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY );
if( matchString( "bkmag", &szFormat ) ) // %bkmag == 背景颜色, 品红
SetConsoleTextAttribute( hConsole, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY );
}
break;
case '\n': //换行 并结束当前前\背景色输出
SetConsoleTextAttribute( hConsole, 0 );
SetConsoleTextAttribute( hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY );
_cputs( "\n" );
szFormat++;
break;
case '\a': //发出 beep 音
MessageBeep(0);
szFormat++;
break;
default:
_cprintf( "%c", (char)*szFormat++ );
}
}
va_end( pArgList );
}
//串匹配
BOOL matchString( const char *szSrc, char **szDest )
{
char *szTmp = *szDest;
while( *szSrc )
{
if( *szSrc != **szDest )
{
*szDest = szTmp;
return FALSE;
}
szSrc++;
(*szDest)++;
}
return TRUE;
}
//
//输出点
int printPOINT( POINT pt )
{
_cprintf( "(x=%d, y=%d)", pt.x, pt.y ); //输出格式
return 0;
}
//输出矩形
int printRECT( RECT rect )
{
_cprintf( "(left=%d, top=%d, right=%d, bottom=%d)", rect.left, rect.top, rect.right, rect.bottom );
return 0;
}
//输出尺寸
int printSIZE( SIZE size )
{
_cprintf( "[cx=%d, cy=%d]", size.cx, size.cy );
return 0;
}
//输出一组RGB值
int printRGBQUAD( RGBQUAD rgb )
{
_cprintf( "(blue=%d, green=%d, red=%d, reserved=%d)", rgb.rgbBlue, rgb.rgbGreen, rgb.rgbRed, rgb.rgbReserved );
return 0;
}
//
//
示范:
#include <windows.h>
#include "mdebug.h"
//
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
//
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdline, int iCmdShow )
{
TCHAR szAppName[] = TEXT("mDebug");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = GetSysColorBrush( COLOR_3DFACE );
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
if( !RegisterClass(&wndclass) )
{
MessageBox( NULL, TEXT("窗口类注册失败!"), TEXT("应用程序错误"), MB_OK | MB_ICONERROR );
return 0;
}
hwnd = CreateWindow(
szAppName,
TEXT("mDebug - Win32 Application 调试工具 - Demo"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
600, 400,
NULL, NULL, hInstance, NULL
);
ShowWindow( hwnd, iCmdShow );
UpdateWindow( hwnd );
while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
//
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
RECT rect = {0};
POINT pt = {0};
RGBQUAD rgb = {0};
rgb.rgbBlue = 100;
rgb.rgbGreen = 125;
rgb.rgbRed = 213;
switch( message )
{
case WM_CREATE: //在处理创建消息时对 mDebug 进行初始化
debugInit(); //初始化 mDebug
return 0;
case WM_LBUTTONDOWN:
rect.left = 10; rect.top = 20;
rect.right = 30; rect.bottom = 40;
pt.x = 100; pt.y = 200;
mPuts( "mDebug提供的普通格式化输出:\n" ); //使用普通格式化输出函数 mPuts
mPuts( "x = %d, f = %f, c = %c\n\n", 3+5, 1.79, 'w' );
mPutsEx( "mDebug提供的增强型格式化输出:\n" ); //使用增强型格式化输出函数 mPutEx
mPutsEx( "RECT结构: %rect\n", rect );
mPutsEx( "%bkgreen颜色增强:%endbk %fryellow%rect\n", rect );
mPutsEx( "%frmagRGBQUAD结构:%endbk %bkred%rgb\n", rgb );
return 0;
case WM_DESTROY:
debugFree(); //关闭调试窗口
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hwnd, message, wParam, lParam );
}