搜索
您的当前位置:首页正文

MessageBox与AfxMessageBox用法与区别

来源:好走旅游网


VC中MessageBox与AfxMessageBox用法与区别 MessageBox()用法

消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合。

1.MessageBox(\"这是一个最简单的消息框!\"); 2.MessageBox(\"这是一个有标题的消息框!\标题\");

3.MessageBox(\"这是一个确定 取消的消息框!\标题\MB_OKCANCEL );

4.MessageBox(\"这是一个警告的消息框!\标题\MB_ICONEXCLAMATION );

5.MessageBox(\"这是一个两种属性的消息框!\标题\MB_ICONEXCLAMATION|MB_OKCANCEL );

6.if(MessageBox(\"一种常用的应用\标题\B_OKCANCEL)==IDCANCEL) return;

附其它常用属性

系统默认图标,可在消息框上显示

X错误 MB_ICONHAND, MB_ICONSTOP, and MB_ICONERROR ?询问 MB_ICONQUESTION

!警告 MB_ICONEXCLAMATION and MB_ICONWARNING i信息 MB_ICONASTERISK and MB_ICONINFORMATION 按钮的形式 MB_OK 默认

MB_OKCANCEL 确定取消 MB_YESNO 是否

MB_YESNOCANCEL 是否取消 返回值

IDCANCEL 取消被选

IDNO 否被选 IDOK 确定被选 IDYES 是被选

以上消息框的用法是在CWnd的子类中的应用,

如果不是,则要MessageBox(NULL,\"ddd\ 或MessageBox(hWnd,\"ddd\ hWnd为某窗口的句柄,或者直接用AfxMessageBox。 AfxMessageBox的用法 nt AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );

lpszText为显示内容。 nType 基本类型:

MB_ABORTRETRYIGNORE The message box contains three pushbuttons: Abort, Retry, and Ignore.

MB_OK The message box contains one pushbutton: OK.

MB_OKCANCEL The message box contains two pushbuttons: OK and Cancel.

MB_RETRYCANCEL The message box contains two pushbuttons: Retry and Cancel.

MB_YESNO The message box contains two pushbuttons: Yes and No.

MB_YESNOCANCEL The message box contains three pushbuttons: Yes, No, and Cancel.

AfxMessageBox()与MessageBox()的区别

带afx的是全局函数,可以在程序任何地方使用,不带的是CWnd的子函数,只

能在CWnd窗口类对象里面使用 AfxMessageBox的函数原型

int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );

int AFXAPI AfxMessageBox( UINT nIDPrompt, UINT nType = MB_OK, UINT nIDHelp = (UINT) –1 );

在第一种形式中,lpszText表示在消息框内部显示的文本,消息框的标题为应用程序的可执行文件名(如Hello)。在第二种形式中,nIDPrompt为要显示的文本字符串在字符串表中的ID。函数调用时会自动从字符串表中载入字符串并显示在消息框中。nType为消息框中显示的按钮风格和图标风格的组合,可以采用|(或)操作符组合各种风格。 按钮风格

MB_ABORTRETRYIGNORE 消息框中显示Abort、Retry、Ignore按钮 MB_OK 显示OK按钮

MB_OKCANCEL 显示OK、Cancel按钮

MB_RETRYCANCEL 显示Retry、Cancel按钮 MB_YESNO 显示Yes、No按钮

MB_YESNOCANCEL 示Yes、No、Cancel按钮 图标风格

MB_ICONINFORMATION 显示一个i图标,表示提示 MB_ICONEXCLAMATION 显示一个惊叹号,表示警告 MB_ICONSTOP 显示手形图标,表示警告或严重错误 MB_ICONQUESTION 显示问号图标,表示疑问

与AfxMessageBox类似的函数MessageBox,它是CWnd的类成员函数: int MessageBox( LPCTSTR lpszText,LPCTSTR lpszCaption = NULL,UINT nType = MB_OK );

两个函数的区别:AfxMessageBox比 MessageBox简单一些,因为它是一个全局函数所以不需要对应的一个窗口类,但是不能控制消息框标题,常用于调试程序时的内部数据输出或警告;MessageBox比较正式,常用在要提交的应用程

序版本中,可以控制标题内容而不必采用含义不明的可执行文件名为标题。 举例:

AfxMessageBox(“Are you sure?”,MB_YESNO|MB_ICONQUESTION); int a = MessageBox(TEXT( \"是否确认删除?\" ), TEXT(\"Warning!!\"),4); if (a == 6)

AfxMessageBox(\"Yes\"); else

AfxMessageBox(\"No\");

(其中#define IDYES 6 #define IDNO 7) VC中可调用的函数大致可分三类:

1.类自己的函数,只对类自己的数据成员有作用;

2.AFX小组在设计 Application Framworks 时设计的全局函数,多冠在Afx前缀,在包含了MFC库/框架的工程中可用;

3.Windows API的全局函数。对所有Windows平台下的程序设计都可以调用,如Vb,Vc,Dephi等等。你说的 MessageBox是属于 CWnd 类的成员函数,只能在 CWnd 和CWnd的派生类的对象中调用;AfxMessageBox则可在任何地方调用。另外对应的还有: ::MessageBox()这个windows API的全局函数。上述中1和3一般有一个区别,就是1要比3少一个参数,即窗口句柄。大家知道,这个句柄是通过 this 指针曲折转换得到的,不用程序员操心了。 如何修改AfxMessageBox的标题?

应用程序的exe的名称可以随意更改,但是如果程序内部弹出一个系统对话框,这时候就会发现对话框的标题和应用程序的名称不一致了。怎么办呢? 系统内部弹出的对话框一般调用的是AfxMessageBox()函数,那就检查一下这个函数吧。一路跟下去,最后发现到了CWinApp类中。

int CWinApp::ShowAppMessageBox(CWinApp *pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt) {

// disable windows for modal dialog DoEnableModeless(FALSE);

HWND hWndTop;

HWND hWnd = CWnd::GetSafeOwner_(NULL, &hWndTop); // re-enable the parent window, so that focus is restored // correctly when the dialog is dismissed. if (hWnd != hWndTop) EnableWindow(hWnd, TRUE); // set help context if possible DWORD* pdwContext = NULL; DWORD dwWndPid=0;

GetWindowThreadProcessId(hWnd,&dwWndPid);

if (hWnd != NULL && dwWndPid==GetCurrentProcessId() ) {

// use app-level context or frame level context

LRESULT lResult = ::SendMessage(hWnd, WM_HELPPROMPTADDR, 0, 0);

if (lResult != 0)

pdwContext = (DWORD*)lResult; }

// for backward compatibility use app context if possible if (pdwContext == NULL && pApp != NULL) pdwContext = &pApp->m_dwPromptContext; DWORD dwOldPromptContext = 0; if (pdwContext != NULL) {

// save old prompt context for restoration later dwOldPromptContext = *pdwContext;

if (nIDPrompt != 0)

*pdwContext = HID_BASE_PROMPT+nIDPrompt; }

// determine icon based on type specified if ((nType & MB_ICONMASK) == 0) {

switch (nType & MB_TYPEMASK) {

case MB_OK:

case MB_OKCANCEL:

nType |= MB_ICONEXCLAMATION; break;

case MB_YESNO:

case MB_YESNOCANCEL: nType |= MB_ICONQUESTION; break;

case MB_ABORTRETRYIGNORE: case MB_RETRYCANCEL:

// No default icon for these types, since they are rarely used. // The caller should specify the icon. break; } }

#ifdef _DEBUG

if ((nType & MB_ICONMASK) == 0)

TRACE(traceAppMsg, 0, \"Warning: no icon specified for message b

ox.\\n\"); #endif

TCHAR szAppName[_MAX_PATH]; szAppName[0] = '\\0'; LPCTSTR pszAppName; if (pApp != NULL)

pszAppName = pApp->m_pszAppName; else {

pszAppName = szAppName;

DWORD dwLen = GetModuleFileName(NULL, szAppName, _MAX_PATH);

if (dwLen == _MAX_PATH)

szAppName[_MAX_PATH - 1] = '\\0'; }

int nResult =

::AfxCtxMessageBox(hWnd, lpszPrompt, pszAppName, nType); // restore prompt context if possible if (pdwContext != NULL)

*pdwContext = dwOldPromptContext; // re-enable windows if (hWndTop != NULL)

::EnableWindow(hWndTop, TRUE); DoEnableModeless(TRUE); return nResult; }

红字的地方是关键,最终pszAppName的值会指向当前应用程序的一个字符串m_pszAppName,这个值是开放的可以被动态修改的。如此一来,在应用程序初始化之前修改m_pszAppName这个值就会更改AfxMessageBox的标题了。 那这个m_pszAppName的值怎么修改呢?很简单。 WCHAR* szAppName = new WCHAR[MAX_PATH]; lstrcpy(szAppName , L\"TargetTitle\");

AfxGetApp()->m_pszAppName = szAppName;

因篇幅问题不能全部显示,请点此查看更多更全内容

Top