调试秘笈--MessageBox()格式化输出,windows程序支持命令行输出

原文链接:https://my.oschina.net/u/160145/blog/264397

这是MessageBox的全家福:

调试秘笈--MessageBox()格式化输出,windows程序支持命令行输出

 格式化输出:

int BoxPrintf( TCHAR * szBuffer, const TCHAR * szFormat, ...)
{
    int     iReturn ;         
    va_list pArgs ;        
    va_start (pArgs, szFormat) ;        
    iReturn = wvsprintf (szBuffer, szFormat, pArgs) ;   
    va_end (pArgs) ;            //wvsprintf参数三为va_list,而wsprintf为"..."
    MessageBox(NULL,szBuffer,TEXT("message"),MB_OK|MB_TOPMOST) ;    
   return iReturn ;
}

发声:
//MessageBeep (MB_ICONWARNING);//轻dideng
//MessageBeep (MB_ICONASTERISK );//dideng
//MessageBeep ( MB_OK );//DENG,同-1
//MessageBeep ( MB_ICONHAND );//HONG,出错音
//MessageBeep (-1 );//DENG,全拼输入无汉字音
//MessageBeep ( MB_ICONQUESTION );//无声


下面是测试 int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);的标志位

//cl msg.cpp  /EHsc

#include "windows.h"
#include "iostream" 
#include   "string"

#pragma comment(lib,"user32.lib")
using namespace std;
void showMenu()
{
	cout<<endl<<"-------测试 int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);中的uType--------"<<endl;
	cout<<"0.EXIT"<<endl;
	cout<<"1.MB_OK"<<endl;
	cout<<"2.MB_ICONEXCLAMATION"<<endl; 
	cout<<"3.MB_ICONWARNING"<<endl; 
	cout<<"4.MB_ICONQUESTION"<<endl; 
	cout<<"5.MB_ICONSTOP"<<endl; 
	cout<<"6.MB_ICONERROR"<<endl; 
	cout<<"7.MB_ICONHAND"<<endl; 
	cout<<"8.MB_ICONASTERISK"<<endl; 
	cout<<"9.MB_ICONINFORMATION"<<endl; 
	cout<<"a.MB_SETFOREGROUND"<<endl; 
	cout<<"b.MB_TOPMOST"<<endl; 
	cout<<"c.MB_SYSTEMMODAL"<<endl; 

	cout<<"-测试 int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType);的按键与返回值---"<<endl;
	cout<<"d.MB_ABORTRETRYIGNORE"<<endl;
	cout<<"e.MB_OK"<<endl;
	cout<<"f.MB_OKCANCEL"<<endl;
	cout<<"g.MB_RETRYCANCEL"<<endl;
	cout<<"h.MB_YESNO"<<endl;
	cout<<"i.MB_YESNOCANCEL"<<endl;


	
}
struct RETVALUE
{
	UINT uType;
	string typeName;
} retValue[]={
	IDABORT,"IDABORT",
	IDCANCEL,"IDCANCEL",
	IDIGNORE,"IDIGNORE",
	IDIGNORE,"IDIGNORE",
	IDNO,"IDNO",
	IDOK,"IDOK",
	IDRETRY ,"IDRETRY",
	IDYES,"IDYES",
};
int main()
{
		int key=0,ret=0;
		bool bQuit=false;
		while(!bQuit)
		{
			showMenu();
			cout<<"选择并回车:";
			key=cin.get();
			switch(key)
			{	
				case '0':
					bQuit=true;
					break;
				case '1':
					ret=MessageBox(NULL,TEXT("MB_OK"),TEXT("test.1"), MB_OK);
					break;
				case '2':
					ret=MessageBox(NULL,TEXT("MB_ICONEXCLAMATION"),TEXT("test.2"),MB_ICONEXCLAMATION);
					break;
				case '3':
					ret=MessageBox(NULL,TEXT("MB_ICONWARNING"),TEXT("test.3"),MB_ICONWARNING);
					break;
				case '4':
					ret=MessageBox(NULL,TEXT("MB_ICONQUESTION"),TEXT("test.4"),MB_ICONQUESTION);
					break;
				case '5':
					ret=MessageBox(NULL,TEXT("MB_ICONSTOP"),TEXT("test.5"),MB_ICONSTOP);
					break;
				case '6':
					ret=MessageBox(NULL,TEXT("MB_ICONERROR"),TEXT("test.6"),MB_ICONERROR);
					break;
				case '7':
					ret=MessageBox(NULL,TEXT("MB_ICONHAND"),TEXT("test.7"),MB_ICONHAND);
					break;
				case '8':
					ret=MessageBox(NULL,TEXT("MB_ICONASTERISK"),TEXT("test.8"),MB_ICONASTERISK);
					break;
				case '9':
					ret=MessageBox(NULL,TEXT("MB_ICONINFORMATION"),TEXT("test.9"),MB_ICONINFORMATION);
					break;
				case 'A':
				case 'a':
					ret=MessageBox(NULL,TEXT("MB_SETFOREGROUND"),TEXT("appear top.a"),MB_SETFOREGROUND);
					break;
				case 'B':
				case 'b':		 
					ret=MessageBox(NULL,TEXT("MB_TOPMOST"),TEXT("almost top.b"),MB_TOPMOST);
					break;
				case 'C':
				case 'c':
					ret=MessageBox(NULL,TEXT("MB_SYSTEMMODAL"),TEXT("almost top.c"),MB_SYSTEMMODAL);
					break;
					
				case 'D':
				case 'd':
					ret=MessageBox(NULL,TEXT("MB_ABORTRETRYIGNORE"),TEXT("test ret.d"),MB_ABORTRETRYIGNORE);
					break;
				case 'E':
				case 'e':
					ret=MessageBox(NULL,TEXT("MB_OK"),TEXT("test ret.e"),MB_OK);
					break;
				case 'F':
				case 'f':
					ret=MessageBox(NULL,TEXT("MB_OKCANCEL"),TEXT("test ret.f"),MB_OKCANCEL);
					break;
				case 'G':
				case 'g':
					ret=MessageBox(NULL,TEXT("MB_RETRYCANCEL"),TEXT("test ret.g"),MB_RETRYCANCEL);
					break;
				case 'H':
				case 'h':
					ret=MessageBox(NULL,TEXT("MB_YESNO"),TEXT("test ret.h"),MB_YESNO);
					break;
				case 'I':
				case 'i':
					ret=MessageBox(NULL,TEXT("MB_YESNOCANCEL"),TEXT("test ret.i"),MB_YESNOCANCEL);
					break;
			}
			for(int i=0;i<sizeof(retValue)/sizeof(retValue[0]);i++)
			{
				if(retValue[i].uType==ret)
				{
							 cout<<"返回值 ret="<<ret<<"即:"<<retValue[i].typeName<<endl;
				}
			}
		}
	return 0;
}

 让WINDOWS程序支持命令行输出:(来自:女孩不哭的串口调试助手common)
在初始化时加入
case WM_CREATE:
               AllocConsole();
				break;
在结束时加入
case WM_DESTROY:
               FreeConsole();
				break;

加入定义:
#pragma warning (disable:4996)// warning C4996: '_vsnprintf': This function or variable may be unsafe. Consider using _vsnprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
#include <stdio.h>
void cmdpf(TCHAR* fmt, ...)
{	
        TCHAR buf[1024] = {0};
        va_list va;
        int len,dw;
        va_start(va, fmt);
        len=wvsprintf(buf,fmt,va);
        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),buf,len,(LPDWORD)&dw,NULL);
}
然后我们就可以在WINDOWS窗口程序中插入命令行输出,更方便调试


转载于:https://my.oschina.net/u/160145/blog/264397

上一篇:求给出数据的平均值(不能用数组)


下一篇:awk,grep命令笔记