使下拉列表组合框的行为类似于“编辑"控件 [英] Make a dropdown type Combobox behave like an Edit control
问题描述
我将用下拉类型的组合框替换标准的编辑控件.因此,基本上,组合框的行为就像编辑控件一样.
I'm about to replace a standard edit control with a dropdown type combo box. So basically that combobox bahaves like exactly like an edit control.
到目前为止,一切正常,但只有一个明显的区别:
Everything works fine so far but there is just one notable difference:
- 当您单击已经包含一些文本且没有焦点的编辑控件时,光标仅位于您单击的位置.
- 但是,当您单击一个已经包含一些文本且没有焦点的组合框时,将选中整个文本.
这描述了当您单击组合框或编辑控件时红色箭头所指向的位置都没有焦点时发生的情况:
This depicts the situation what happens when you click on either the combobox or the edit control where the red arrow points when neither has the focus:
有没有办法使组合框像编辑控件一样起作用?
Is there a way to make the combo box behave like an edit control?
推荐答案
一种防止整个文本被选中的解决方案是通过子类Combo Box并在第一次单击鼠标左键时将焦点设置为其编辑控件.代码如下:
One of solution to prevent the whole text selected is via subclass a Combo Box and setting focus to its edit control when the mouse left button click on it at first time. Code as below:
子类过程:
LRESULT CALLBACK EditSubClassProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam,
UINT_PTR uIdSubclass,
DWORD_PTR dwRefData
)
{
switch (uMsg)
{
case WM_DESTROY:
{
RemoveWindowSubclass(hWnd, EditSubClassProc, 0);
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
case WM_LBUTTONDOWN:
{
if (GetFocus() != hWnd)
{
SetFocus(hWnd);
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
default:
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
}
找到组合框"的编辑控制窗口并安装子类回调:
// Get the edit window handle to combo box.
HWND comboEditHdl = NULL;
COMBOBOXINFO info = { 0 };
info.cbSize = sizeof(COMBOBOXINFO);
if (!GetComboBoxInfo(hwndCombo1, &info))
return 0;
comboEditHdl = info.hwndItem;
if (comboEditHdl)
{
SetWindowSubclass(comboEditHdl, EditSubClassProc, 0, NULL);
}
这篇关于使下拉列表组合框的行为类似于“编辑"控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!