Win32 - 使用 GetDlgItem 检索 DialogBox 的控制失败

发布于 2024-10-09 06:02:52 字数 1476 浏览 0 评论 0原文

在我的主窗口中,单击菜单项后,会出现一个对话框,要求用户输入。然后将检索该输入。

这是对话框的资源文件:

//
// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-10-16 06:02:52

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文