获取组合框的Win32 API C的文本++(NO MFC) [英] Get the text of a combo box Win32 API C++ (NO MFC)

查看:291
本文介绍了获取组合框的Win32 API C的文本++(NO MFC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个组合框使用户可以从下拉菜单中选择一个选项,然后检索使用Win32 API的C ++编程,不是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, buf, NULL, MB_OK);

                char* buf;
                GetDlgItemText(hwnd, IDC_COMBO1, buf, 9);
                MessageBox(NULL, buf, NULL, MB_OK);

IDC_COMBO1 是组合框的ID和 HWND 是当前对话框的HWND。
在code代表我的组合框对话框:

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:

语言LANG_NEUTRAL,SUBLANG_NEUTRAL
IDD_DIALOG4 DIALOG 0,0,424,181
风格DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
标题对话框
FONT 8,宋体
{
    组合框IDC_COMBO1,113,31,119,19,CBS_DROPDOWN | CBS_HASSTRINGS
    PUSHBUTTONButton1的,IDC_BUTTON1,188,112,50,14
}

我使用一个资源文件做到这一点。先谢谢了。

I am using a resource file to do this. Thanks in advance.

推荐答案

有关调用ComboBox_GetText的HWND参数必须是句柄到组合框本身,而不是对话。你可以得到与HWND 函数GetDlgItem(HWND,IDC_COMBO1);

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); 

这篇关于获取组合框的Win32 API C的文本++(NO MFC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆