简体   繁体   English

在添加到子窗口的列表框控件中获取文本

[英]Get text in a listbox control added to a child window

I want to add the text in a listbox control to the child of my main window. 我想将列表框控件中的文本添加到主窗口的子窗口中。 The child is essentially an edit control, but is not a dialog. 子级实质上是一个编辑控件,但不是对话框。 I have tried a few different functions already with no success, I believe my problem is that I need to somehow switch focus from the dialog window to the child window before adding the text. 我已经尝试了几种不同的功能,但均未成功,我相信我的问题是在添加文本之前,我需要以某种方式将焦点从对话框窗口切换到子窗口。 I would prefer not to get an answer with specific code, but if I could be pointed to a helpful function or concept, that would be great! 我不希望获得特定代码的答案,但是如果我可以指出一个有用的功能或概念,那就太好了!

EDIT: The listbox is part of a larger dialog window that allows the user to enter text and then add it to the list. 编辑:列表框是一个较大的对话框窗口的一部分,允许用户输入文本,然后将其添加到列表中。 These functions are working very well. 这些功能运行良好。 What I'd like to do is get the text that is added to the list moved into the child window when the user clicks a button on the dialog, preferably without the user having to select the items before clicking the button. 我想做的是,当用户单击对话框上的按钮时,最好将添加到列表中的文本移到子窗口中,最好用户无需在单击按钮之前选择项目。

There's a lot of code, but I think these pieces are relevant: 有很多代码,但是我认为这些是相关的:

Child window: 子窗口:

case WM_CREATE:
        {
        hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 
            WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
            0, 0, 100, 100, w, (HMENU) IDC_EDIT, NULL, NULL);

        if (hEdit == NULL){
            MessageBox(NULL, "Could not create child window :(", "ERROR", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    }
        break;

    case WM_SIZE:
        {
            RECT wSize;

            GetClientRect(w, &wSize);
            SetWindowPos(hEdit, NULL, 0, 0, wSize.right, wSize.bottom, NULL);
        }

Function to add text to child window by clicking a button on the dialog (HWND hEdit, the child window, is defined globally): 通过单击对话框上的按钮将文本添加到子窗口的功能(子窗口HWND hEdit是全局定义的):

case ID_ADDMAIN:
            {
            HWND hList = GetDlgItem(w, IDC_LIST1);
            int count = SendMessage(hList, LB_GETCOUNT, NULL, NULL);

            if (count > 0){

                DWORD textLength = GetWindowTextLength(hList);

                LPSTR alloc;

                alloc = (LPSTR) GlobalAlloc(GPTR, textLength + 1);
                if(GetWindowText(hList, alloc, textLength + 1)){

                    SendMessage(hEdit, WM_SETTEXT, NULL, (LPARAM) alloc);
                }

                GlobalFree(alloc);
            }
            else{
                MessageBox(NULL, "There's nothing to add!", "???", MB_ICONINFORMATION | MB_OK);
            }
        }
            break;

Besides the SendMessage function, I also tried SetWindowText and I tried getting each string in the listbox separately using a for loop rather than GetWindowText. 除了SendMessage函数外,我还尝试了SetWindowText,并尝试使用for循环而不是GetWindowText分别获取列表框中的每个字符串。 Thank you in advance for your help. 预先感谢您的帮助。

You misunderstand the list box. 您误解了列表框。 The LB_GETCOUNT message is used to get the number of items (rows) in the list box. LB_GETCOUNT消息用于获取列表框中的项目(行)数。 (Not the number of characters.) (不是字符数。)

GetWindowText is not appropriate for a list box: It gets the titlebar text, but a list box has no titlebar. GetWindowText不适用于列表框:它获取标题栏文本,但列表框没有标题栏。 What you can do with a list box is find out which row is selected (LB_GETCURSEL) and then get the text from that row (LB_GETTEXT). 使用列表框可以执行的操作是找到选定的行(LB_GETCURSEL),然后从该行获取文本(LB_GETTEXT)。

Here is a list of functions I wrote for my WINAPI class library.. 这是我为WINAPI类库编写的函数列表。

    int ListBox::GetItemCount() const
    {
        return SendMessage(Control::GetHandle(), LB_GETCOUNT, 0, 0);
    }

    int ListBox::GetSelectedIndex() const
    {
        return SendMessage(Control::GetHandle(), LB_GETCURSEL, 0, 0);
    }

    void ListBox::SetSelectedIndex(int Index)
    {
        SendMessage(Control::GetHandle(), LB_SETCURSEL, Index, 0);
    }

    void ListBox::AddItem(const tstring &Item, int Index)
    {
        SendMessage(Control::GetHandle(), Index == 0 ? LB_ADDSTRING : LB_INSERTSTRING, Index, reinterpret_cast<LPARAM>(Item.c_str()));
    }

    void ListBox::RemoveItem(int Index)
    {
        SendMessage(Control::GetHandle(), LB_DELETESTRING, Index, 0);
    }

    void ListBox::Clear()
    {
        SendMessage(Control::GetHandle(), LB_RESETCONTENT, 0, 0);
    }

    int ListBox::GetIndexOf(const tstring &Item)
    {
        return SendMessage(Control::GetHandle(), LB_FINDSTRINGEXACT, -1, reinterpret_cast<LPARAM>(Item.c_str()));
    }

    int ListBox::GetIndexPartial(const tstring &Item)
    {
        return SendMessage(Control::GetHandle(), LB_FINDSTRING, -1, reinterpret_cast<LPARAM>(Item.c_str()));
    }

    void ListBox::SetColumnWidth(int Width)
    {
        SendMessage(Control::GetHandle(), LB_SETCOLUMNWIDTH, Width, 0);
    }

    tstring ListBox::GetItem(int Index) const
    {
        std::vector<char> Buffer(SendMessage(Control::GetHandle(), LB_GETTEXTLEN, Index, 0) + 1);
        SendMessage(Control::GetHandle(), LB_GETTEXT, Index, reinterpret_cast<LPARAM>(Buffer.data()));
        return tstring(Buffer.begin(), Buffer.end());
    }

You'll need: 你需要:

ListBox::GetItem(ListBox::GetSelectedIndex());

to get the text of the current item in the list box.. 在列表框中获取当前项目的文本。

For the Edit control, I have: 对于Edit控件,我有:

void TextBox::SetReadOnly(bool ReadOnly)
{
    SendMessage(Control::GetHandle(), EM_SETREADONLY, ReadOnly, 0);
}

void TextBox::SetPassword(bool Enabled, char PasswordChar)
{
    SendMessage(Control::GetHandle(), EM_SETPASSWORDCHAR, Enabled ? PasswordChar : 0, 0);
}

std::uint32_t TextBox::GetTextLength() const
{
    return GetWindowTextLength(Control::GetHandle());
}

void TextBox::ShowScrollBar(bool Show, int wBar)
{
    ::ShowScrollBar(Control::GetHandle(), wBar, true);
}

void TextBox::AppendText(const tstring &Text) const
{
    SendMessage(Control::GetHandle(), EM_SETSEL, -1, -1);
    SendMessage(Control::GetHandle(), EM_REPLACESEL, 0, reinterpret_cast<LPARAM>(Text.c_str()));
}

tstring TextBox::GetText() const
{
    std::vector<TCHAR> Buffer(GetWindowTextLength(Control::GetHandle()) + 1);
    GetWindowText(Control::GetHandle(), Buffer.data(), Buffer.size());
    return tstring(Buffer.begin(), Buffer.end());
}

void TextBox::SetText(const tstring &Text)
{
    this->Title = Text;
    SetWindowText(Handle, Text.c_str());
}

You can get the text from the edit box easily with these.. I hope these aid you greatly.. 有了这些,您可以轻松地从编辑框中获取文本。.希望这些对您有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM