三者的本质区别如下:
Windows API: MessageBox();
CWnd::MessageBox();
MFC: AfxMessageBox();
其中 CWnd::MessageBox () 与 AfxMessageBox () 都是对 API 函数 MessageBox()的封装。
关于是否需要句柄 HWND 的说明:
1)MessageBox 是 API,因此它需要 HWND 参数指定其所属窗体。
2)CWnd::MessageBox () 是 CWnd 的成员函数,它要求 MessageBox 只能在 cwnd 类及其子类中使用;它不要求 HWND 参数,因为 CWnd 已经指明了其所属窗口。 因此 CWnd::MessageBox () 和::MessageBox (HWND) 作用等同。
3)AfxMessageBox()mfc 中的全局函数,无句柄 HWND 参数。能用 MessageBox () 的地方都能用 AfxMessageBox (),但 AfxMessageBox 不能设置消息框标题(它的默认标题就是应用程序的可执行文件名).
使用建议:
1)在非 CWnd 类中使用则是 API MessageBox 函数,但它使用起来非常不便,必须指定标题、句柄和样式等,否则选用 CWnd::MessageBox () 或 AfxMessageBox,它们使用起来方便。
2)MessageBox 是模态对话框,你不进行确认时程序不往下运行,它会阻塞你当前的线程为,除非你程序是多线程的程序,否则只有等待模态对话框被确认。
3)如果用 MFC 的话,请尽量使用 afxmessagebox,因为这个全局的对话框最安全,也最方便。