Main.cpp
#include <Windows.h> #include "resource.h" INT_PTR CALLBACK dialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (WM_INITDIALOG == uMsg) { RECT rect; GetWindowRect(hwndDlg, &rect); int windowWidth = rect.right - rect.left; int windowHeight = rect.bottom - rect.top; int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); MoveWindow(hwndDlg, (screenWidth - windowWidth) / 2, (screenHeight - windowHeight) / 2, windowWidth, windowHeight, FALSE); SetDlgItemText(hwndDlg, IDC_EDIT_CONTENT, TEXT("这是一个简单的示例!")); return TRUE; } else if (WM_CLOSE == uMsg) { EndDialog(hwndDlg, 0); return TRUE; } else if (WM_COMMAND == uMsg) { WORD controlId = LOWORD(wParam); switch (controlId) { case IDC_BUTTON_SAY: { TCHAR content[100]; GetDlgItemText(hwndDlg, IDC_EDIT_CONTENT, content, 100); MessageBox(hwndDlg, content, TEXT("消息框"), MB_ICONINFORMATION); } break; case IDC_BUTTON_YES: EndDialog(hwndDlg, IDC_BUTTON_YES); break; case IDC_BUTTON_NO: EndDialog(hwndDlg, IDC_BUTTON_NO); break; } return TRUE; } return FALSE; } int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { INT_PTR returnValue = DialogBox(hInstance, reinterpret_cast<const TCHAR *>(IDD_DIALOG_MAIN), nullptr, dialogProc); if (IDC_BUTTON_YES == returnValue) { MessageBox(nullptr, TEXT("You clicked the Yes button!"), TEXT("Message Box"), MB_ICONINFORMATION); } else if (IDC_BUTTON_NO == returnValue) { MessageBox(nullptr, TEXT("You clicked the No button!"), TEXT("Message Box"), MB_ICONINFORMATION); } else { MessageBox(nullptr, TEXT("You closed the dialog!"), TEXT("Message Box"), MB_ICONINFORMATION); } return 0; }