如何在继承组合框时抑制自动搜索 (Win32/WinAPI)
How to suppress auto-searching when subclassing combobox (Win32/WinAPI)
我想进行自定义搜索,但它被自动搜索削弱了。这就是我的意思,
这是我的子类函数:
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);
}
这是主要的 WM_CREATE 消息开关,
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;
}
我有一个全局变量std::string insearch;
,每次按下一个字母键,它就会改变并用于搜索。它向后运行良好,但不能按预期向前运行。例如,当 insearch=="FIR"
它选择 "Fires"
并且一次 ← 被击中 insearch=="FI"
并且它正确地突出显示 "Fiat"
并再次 ← 被击中 insearch=="F"
并按预期突出显示 "Fauna"
。因为 ← 不会干扰自动搜索,但字母键会。如果然后按下 A 和 insearch=="FA"
,它会默认突出显示 "Ant"
。因为自动搜索只依赖于一个字符,它是 "A"
并且它搜索以 "A"
开头的内容,但我希望它跳过默认搜索并使用我的自定义方式,它应该突出显示 "Fauna"
因为 "FA"
正在搜索中。原来是这种情况,请教我一个抑制自动搜索的方法。
根据 Default Combo Box Behavior,默认搜索行为是响应 WM_CHAR
而触发的,而不是 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.
我想进行自定义搜索,但它被自动搜索削弱了。这就是我的意思,
这是我的子类函数:
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);
}
这是主要的 WM_CREATE 消息开关,
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;
}
我有一个全局变量std::string insearch;
,每次按下一个字母键,它就会改变并用于搜索。它向后运行良好,但不能按预期向前运行。例如,当 insearch=="FIR"
它选择 "Fires"
并且一次 ← 被击中 insearch=="FI"
并且它正确地突出显示 "Fiat"
并再次 ← 被击中 insearch=="F"
并按预期突出显示 "Fauna"
。因为 ← 不会干扰自动搜索,但字母键会。如果然后按下 A 和 insearch=="FA"
,它会默认突出显示 "Ant"
。因为自动搜索只依赖于一个字符,它是 "A"
并且它搜索以 "A"
开头的内容,但我希望它跳过默认搜索并使用我的自定义方式,它应该突出显示 "Fauna"
因为 "FA"
正在搜索中。原来是这种情况,请教我一个抑制自动搜索的方法。
根据 Default Combo Box Behavior,默认搜索行为是响应 WM_CHAR
而触发的,而不是 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.