1

我想将列表框控件中的文本添加到我的主窗口的子窗口中。child 本质上是一个编辑控件,但不是一个对话框。我已经尝试了一些不同的功能但没有成功,我相信我的问题是我需要在添加文本之前以某种方式将焦点从对话框窗口切换到子窗口。我不希望得到特定代码的答案,但如果我能指出一个有用的功能或概念,那就太好了!

编辑:列表框是一个较大的对话框窗口的一部分,允许用户输入文本,然后将其添加到列表中。这些功能运行良好。我想做的是当用户单击对话框上的按钮时,将添加到列表中的文本移到子窗口中,最好无需用户在单击按钮之前选择项目。

有很多代码,但我认为这些部分是相关的:

子窗口:

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

通过单击对话框上的按钮向子窗口添加文本的功能(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;

除了 SendMessage 函数之外,我还尝试了 SetWindowText,并尝试使用 for 循环而不是 GetWindowText 分别获取列表框中的每个字符串。预先感谢您的帮助。

4

2 回答 2

1

您误解了列表框。LB_GETCOUNT 消息用于获取列表框中的项目(行)数。(不是字符数。)

GetWindowText 不适用于列表框:它获取标题栏文本,但列表框没有标题栏。您可以对列表框执行的操作是找出选择了哪一行(LB_GETCURSEL),然后从该行获取文本(LB_GETTEXT)。

于 2013-10-27T02:22:47.503 回答
1

这是我为我的 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());
    }

你需要:

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

获取列表框中当前项目的文本..

对于编辑控件,我有:

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

您可以使用这些轻松地从编辑框中获取文本.. 我希望这些对您有很大帮助..

于 2013-10-27T03:32:38.253 回答