Joshua_0101
Joshua_0101

Reputation: 57

GetWindowTextA unable to get the text of ComboBox MFC C++

I'm using GetWindowTextA to get the text of ComboBox but it will be an empty string "" even the hwnd is correct.

No problem when using GetWindowTextA to get the text from other class, but it won't work for the class ComboBox. Is it something related to this that need other function to get text from ComboBox?

Thanks.


Editied:

The combobox is from a control in some other app's window

#include<windows.h>
#include<iostream>
using namespace std;

int main() {

        POINT pt;
        Sleep(3000);
        GetCursorPos(&pt);

        HWND hWnd = WindowFromPoint(pt); 

        char class_name[100];
        char title[100];

        GetClassNameA(hWnd,class_name, sizeof(class_name));
        GetWindowTextA(hWnd,title,sizeof(title));

        cout <<"Window name : "<<title<<endl;
        cout <<"Class name  : "<<class_name<<endl;

        return 0;

}

Upvotes: 0

Views: 511

Answers (2)

Joshua_0101
Joshua_0101

Reputation: 57

Adding TCHAR szBuf[100]; and using SendMessage will be done.

#include<windows.h>
#include<iostream>
using namespace std;

int main() {

        POINT pt;
        Sleep(3000);
        GetCursorPos(&pt);

        HWND hWnd = WindowFromPoint(pt); 

        char class_name[100];
        char title[100];
        TCHAR szBuf[100];

        GetClassNameA(hWnd,class_name, sizeof(class_name));
        GetWindowTextA(hWnd,title,sizeof(title));
        SendMessage(hWnd, WM_GETTEXT, 100, (LPARAM)szBuf);

        //cout <<"Window name : "<<title<<endl;
        cout <<"Class name  : "<<class_name<<endl;
        wcout <<"Window name  : "<<szBuf<<endl;

        system("PAUSE");
        return 0;

}

Upvotes: 0

Ed Dore
Ed Dore

Reputation: 2119

If you've added a CComboBox variable to your dialog class, using the "Add Control Variable" wizard, as described here Add a member variable, you can readily use the CComboBox methods to retrieve the text of the selected combo item, as illustrated below:

void CMFCDlgAppDlg::OnBnClickedButton1()
{
    CString itemText;
    m_Combo.GetLBText(m_Combo.GetCurSel(), itemText);
    AfxMessageBox(itemText);
}

Upvotes: 1

Related Questions