Get the text of a combo box Win32 API C++ (NO MFC)
I am trying to setup a combo box so a user can select an option from the dropdown menu and then retrieve what the user selected using Win32 API C++ programming, not MFC. I read John's post here and I could not get anything to work. I can set the text for the combo box, but I cannot retrieve what the user selected. Here are a few methods I have tried:
LPTSTR buf;
ComboBox_GetText(hwnd, buf, 9);
MessageBox(NULL, bu开发者_如何学编程f, NULL, MB_OK);
And
char* buf;
GetDlgItemText(hwnd, IDC_COMBO1, buf, 9);
MessageBox(NULL, buf, NULL, MB_OK);
IDC_COMBO1
is the ID of the combo box and hwnd
is the HWND of the current dialog box.
The code for my dialog box with the combo box is:
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG4 DIALOG 0, 0, 424, 181
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Ms Shell Dlg"
{
COMBOBOX IDC_COMBO1, 113, 31, 119, 19, CBS_DROPDOWN | CBS_HASSTRINGS
PUSHBUTTON "Button1", IDC_BUTTON1, 188, 112, 50, 14
}
I am using a resource file to do this. Thanks in advance.
For the call to ComboBox_GetText the hwnd parameter must be the handle to the combo box itself, not the dialog. You can get that HWND with GetDlgItem(hwnd, IDC_COMBO1);
Also, you can't pass an uninitialized pointer to either function; you must pass a pointer to a buffer which you have created.
char buf[10];
GetDlgItemText(hwnd, IDC_COMBO1, buf, 9);
MessageBox(NULL, buf, NULL, MB_OK);
If I remember correctly, you need to use ComboBox_GetCurSel to determine which item is selected and then you can use ComboBox_GetLBText to get the actual text.
精彩评论