MFC(Microsoft Foundation Classes)中 MessageBox

In MFC (Microsoft Foundation Classes), MessageBox is a commonly used dialog box class used to display message boxes and interact with users. The MessageBox class provides a variety of usages and options. Here are some common usages and examples:

  1. Display a simple message box:
CString message = _T("Hello, World!");
MessageBox(message);

This will display a simple message box containing the text "Hello, World!".

  1. Display a message box with a title:
CString message = _T("Error occurred.");
CString title = _T("Error");
MessageBox(message, title);

This will display a message box with the title "Error" and the text "Error occurred."

  1. Show a message box with buttons and icons:
CString message = _T("Do you want to save changes?");
CString title = _T("Confirmation");
UINT style = MB_YESNOCANCEL | MB_ICONQUESTION;
int result = MessageBox(message, title, style);
if (result == IDYES) {
    
    
    // 用户选择了"是"按钮
    // 执行保存操作
} else if (result == IDNO) {
    
    
    // 用户选择了"否"按钮
    // 不保存,直接关闭
} else if (result == IDCANCEL) {
    
    
    // 用户选择了"取消"按钮
    // 取消关闭操作
}

This will display a message box with "Yes", "No", "Cancel" buttons and a question mark icon containing the text "Do you want to save changes?" and the title "Confirmation". According to the user's selection, the corresponding operation can be performed.

  1. Display a message box with a default button and the focused button:
CString message = _T("Are you sure you want to delete?");
CString title = _T("Confirmation");
UINT style = MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2;
int result = MessageBox(message, title, style);
if (result == IDYES) {
    
    
    // 用户选择了"是"按钮
    // 执行删除操作
} else if (result == IDNO) {
    
    
    // 用户选择了"否"按钮
    // 取消删除操作
}

This will display a message box with "Yes" and "No" buttons and a warning icon, containing the text "Are you sure you want to delete?" and the title "Confirmation" . The MB_DEFBUTTON2 option makes the "No" button the default button and has focus.

These examples illustrate some common uses of the MessageBox class. MessageBox also provides other options, such as setting default buttons, customizing button text, setting timeouts, etc. Depending on your specific needs, you can choose the appropriate option to use the MessageBox class.

Guess you like

Origin blog.csdn.net/ultramand/article/details/134984208