这是由 Windows Desktop Application
创建的 Visual Studio
项目。
我有一个从 Dialog
创建的 Resource View
资源,该资源具有 Static Text
。
我使用此对话框是为了向用户显示错误:
DialogBox(hInst, MAKEINTRESOURCE(IDD_MY_MESSAGE_BOX), hWnd, MyMessageBoxProc);
我使用 DialogBox
的原因是我需要它来停止代码执行,因为下一行代码将关闭应用程序我的意思是用户应该在应用程序退出之前知道错误消息.我知道一种更改 Static Text
的方法:
HWND myMessageBox = CreateDialog(hInst, MAKEINTRESOURCE(IDD_MY_MESSAGE_BOX), nullptr, MyMessageBoxProc);
HWND staticText = GetDlgItem(myMessageBox, IDC_STATIC);
SetWindowText(staticText, L"Text changed.");
但是这种方法不会停止代码执行。
由于它是一个 Windows Desktop Application
项目,我无法创建 MFC
类并尝试以下方法:
// Find the Static Text.
// If called from within MyMessageBox class.
CWnd *staticText = GetDlgItem(IDC_STATIC);
staticText->SetWindowText("Text changed.");
// If called from elsewhere.
MyMessageBox myMessageBox;
CWnd *staticText = myMessageBox.GetDlgItem(IDC_STATIC);
staticText->SetWindowText("Text changed.");
那么,为了使用 Static Text
更改 DialogBox
而无需 MFC
类或什至另一种允许我更改 {{1} } 并且仍然像 Static Text
一样停止代码执行。
答案 0 :(得分:2)
只需通过处理 WM_INITDIALOG 消息来更改窗口过程 (MyMessageBoxProc
) 中的文本。如果您希望向对话框提供文本,请改用 DialogBoxParam
创建它,然后可以通过 lParam
参数访问它。
例如
INT_PTR MyMessageBoxProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_INITDIALOG) {
HWND hCtrl = GetDlgItem(hWnd, IDC_STATIC);
SetWindowText(hCtrl, reinterpret_cast<LPCTSTR>(lParam));
}
return FALSE;
}
创作将是这样的:
LPCTSTR text = _T("Text changed.");
DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_MY_MESSAGE_BOX), hWnd, MyMessageBoxProc,
reinterpret_cast<LPARAM>(text));
请注意,Windows 附带了一个标准消息框,您可能希望使用它而不是编写自己的消息框。这可以通过函数 flock(2)
获得