我们有一个示例应用程序,其中包含“DropDownList”模式下的组合框的此类处理程序:
private void comboBox1_Leave(object sender, EventArgs e)
{
comboBox1.SelectionStart = 0;
comboBox1.SelectionLength = 0;
}
上面的代码行为不同,具体取决于应用程序是否加载了 CALLWNDPROC 挂钩。如果应用程序中有一个 CALLWNDPROC 钩子——只要组合框失去焦点,上面的代码就会抛出异常。没有钩子 - 该代码不会抛出。
这些是异常描述中的几行:
System.ArgumentOutOfRangeException: InvalidArgument=Value of '-2136611475' is not valid for 'start'.
Parameter name: start
at System.Windows.Forms.ComboBox.Select(Int32 start, Int32 length)
at System.Windows.Forms.ComboBox.set_SelectionLength(Int32 value)
at ComboCrash.Form1.comboBox1_Leave(Object sender, EventArgs e) in T:\tmp.proj\ComboCrash\ComboCrash\Form1.cs:line 32
at System.Windows.Forms.Control.OnLeave(EventArgs e)
at System.Windows.Forms.Control.NotifyLeave()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
问题是:安装钩子后出现这种不同行为的原因可能是什么?
PS1:我不是 C# 开发人员,但在我看来,文本选择的概念不适用于 DropDownList 组合框(因为它们没有文本框),对吗?
PS2:安装钩子的应用程序和钩子DLL是用C++编写的。钩子函数很简单:
return (CallNextHookEx(hook_handle, code, wParam, lParam));