Win32 - 使用 GetDlgItem 检索 DialogBox 的控制失败
在我的主窗口中,单击菜单项后,会出现一个对话框,要求用户输入。然后将检索该输入。
这是对话框的资源文件:
//
// Dialog resources
//
IDD_PID DIALOG 0, 0, 158, 84
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP
FONT 8, "MS Sans Serif"
{
PUSHBUTTON "&Ok", ID_PID_RET, 14, 60, 58, 14
PUSHBUTTON "&Cancel", ID_PID_CANCEL, 86, 60, 52, 14
CTEXT "Enter the PID of the process", -1, 32, 16, 91, 15, SS_CENTER
CONTROL "", ID_PID_INPUT, RICHEDIT_CLASS, WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 47, 33, 59, 14, WS_EX_CLIENTEDGE
}
这是使用对话框的 WindowProc:
//
// Message handling for main window
//
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILE_PID:
int choice = DialogBox(thishInstance, MAKEINTRESOURCE(IDD_PID), hWnd, AboutDlgProc);
if (choice == ID_PID_RET)
{
HWND temp = GetDlgItem(hWnd, ID_PID_INPUT);
HRESULT HR = GetLastError();
SendMessage(temp, WM_GETTEXT, sizeof(buff), (LPARAM)&buff);
[...]
}
break;
}
}
}
如果我在资源文件。这应该以某种方式起作用,对吧? hWnd
是管理对话框,ID_PID_RET
是Rich Edit框的控件ID,用于用户输入,所以不知道为什么GetDlgItem
失败。它返回“未找到控件ID”,但它是在我的resource.h 中定义的。有什么想法吗?
In my main window, after clicking a menu item, a dialog box appears which asks the user for input. That input is then to be retrieved.
Here is the resource file for the dialog box:
//
// Dialog resources
//
IDD_PID DIALOG 0, 0, 158, 84
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP
FONT 8, "MS Sans Serif"
{
PUSHBUTTON "&Ok", ID_PID_RET, 14, 60, 58, 14
PUSHBUTTON "&Cancel", ID_PID_CANCEL, 86, 60, 52, 14
CTEXT "Enter the PID of the process", -1, 32, 16, 91, 15, SS_CENTER
CONTROL "", ID_PID_INPUT, RICHEDIT_CLASS, WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 47, 33, 59, 14, WS_EX_CLIENTEDGE
}
Here is the WindowProc where the dialog is used:
//
// Message handling for main window
//
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_FILE_PID:
int choice = DialogBox(thishInstance, MAKEINTRESOURCE(IDD_PID), hWnd, AboutDlgProc);
if (choice == ID_PID_RET)
{
HWND temp = GetDlgItem(hWnd, ID_PID_INPUT);
HRESULT HR = GetLastError();
SendMessage(temp, WM_GETTEXT, sizeof(buff), (LPARAM)&buff);
[...]
}
break;
}
}
}
I am a little uncertain about how it works when I try to get the HWND
if I create the windows in the resource files. This should work somehow, right? hWnd
is managing the dialog box, and ID_PID_RET
is the control ID of the Rich Edit box for user input, so I don't know why GetDlgItem
fails. It returns "Control ID not found", but it is defined in my resource.h. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DialogBox 仅在调用 EndDialog 并且对话框已被销毁后才返回。当您调用 GetDlgItem 时,该对话框不再存在。您可以使用 DialogBoxParam 并传入一个缓冲区/结构/类,以便对话框在调用 EndDialog 之前填充 Rich Edit 字段的内容。
DialogBox only returns after EndDialog has been called and the dialog has been destroyed. The dialog no longer exists when you call GetDlgItem. You can use DialogBoxParam and pass in a buffer/struct/class for the dialog to fill in with the content of the Rich Edit field, before it calls EndDialog.