Search code examples
c++cwinapicomboboxsubclassing

How to suppress auto-searching when subclassing combobox (Win32/WinAPI)


I want to make a custom search but it's crippled by auto search. Here is what I mean,

This is my subclassing function:

LRESULT CALLBACK X(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam,UINT_PTR r1,DWORD_PTR r2)
{
    switch(message)
    {
        case WM_KEYDOWN:
        {
            if(wParam>64&&wParam<91) //if A-Z hit
            {
                char a[2];
                a[0]=wParam;
                insearch.append(a);
            }
            else if(wParam==27) insearch=""; //esc key
            else if(wParam==8&&insearch.length()) insearch.erase(insearch.length()-1,1); //backspace
            SendMessage(hwnd,CB_SETCURSEL,SendMessage(hwnd,CB_FINDSTRING,-1,LPARAM(insearch.c_str())),0);
            break;
        }
    }
    return DefSubclassProc(hwnd,message,wParam,lParam);
}

And this is main WM_CREATE message switch,

case WM_CREATE:
{
    CreateWindowEx(0,"combobox",0,WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST|WS_VSCROLL,5,5,90,125,hwnd,HMENU(1),0,0);
    char o[][20]={"Fauna","Fiat","Fold","Folk","Fall","Fires","Ant"};
    for(char i=0; i<7; i++) SendDlgItemMessage(hwnd,1,CB_ADDSTRING,0,LPARAM(o[i]));
    SendDlgItemMessage(hwnd,1,CB_SETCURSEL,0,0);
    SetWindowSubclass(GetDlgItem(hwnd,1),X,0,0);
    break;
}

I have a global variable std::string insearch; and each time a letter key is hit, it is changed and used for searching. It works well backwards but it doesn't work as intended forwards. For example when insearch=="FIR" it selects "Fires" and once is hit insearch=="FI" and it highlights "Fiat" correctly and once again is hit insearch=="F" and it highlights "Fauna" as intended. Because doesn't interfere with autosearch but letter keys do. If then A is pressed and insearch=="FA", it highlights "Ant" by default. Because auto searching depends on just one char and it is "A" and it searches for something starts with "A", but I want it to skip that default search and go for my custom way which should highlight "Fauna" because "FA" is in search. So this is the situation, please suggest me a way to suppress the automatic search.


Solution

  • Per Default Combo Box Behavior, the default search behavior is triggered in response to WM_CHAR, not WM_KEY(DOWN|UP):

    WM_CHAR
    Processes character input. In drop-down list boxes, this message is passed to the list window, which moves the selection to the first item beginning with the specified character. In simple and drop-down combo boxes, this message is passed to the edit control.