开发者

Win32 - Retrieving control of DialogBox using GetDlgItem fails

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜