Jan*_*sen 14
谷歌搜索相同的错误时我发现了这个问题.NoodleFolk解决方案的问题在于它通过双击项目来禁用扩展三者.通过将NoodleFolk的答案与john arlens的答案结合起来,你会得到这样的结论:
class NewTreeView : TreeView
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x203) // identified double click
{
var localPos = PointToClient(Cursor.Position);
var hitTestInfo = HitTest(localPos);
if (hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
m.Result = IntPtr.Zero;
else
base.WndProc(ref m);
}
else base.WndProc(ref m);
}
}
Run Code Online (Sandbox Code Playgroud)
我(快速)测试了这个解决方案,它似乎工作.
Noo*_*olk 11
选项1:完全禁用双击事件.
创建客户控制
class MyTreeView : TreeView { protected override void WndProc(ref Message m) { if (m.Msg == 0x0203) { m.Result = IntPtr.Zero; } else { base.WndProc(ref m); } } }
在您的设计器文件(form.Designer.cs)中,查找控件的创建位置,并用新控件替换对TreeView构造函数的调用.
this.treeView1 = new MyTreeView();
选项2:将双击事件视为两个单击事件
class MyTreeView : TreeView { protected override void WndProc(ref Message m) { if (m.Msg == 0x0203) { m.Msg = 0x0201; } base.WndProc(ref m); } }
我个人认为选项2更直观.当用户单击两次复选框时,不会选中该复选框.
如果您只想知道CheckBox中发生了DoubleClick事件:
private void TreeViewDoubleClick(object sender, EventArgs e)
{
var localPosition = treeView.PointToClient(Cursor.Position);
var hitTestInfo = treeView.HitTest(localPosition);
if (hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
return;
// ... Do whatever other processing you want
}
Run Code Online (Sandbox Code Playgroud)
小智 5
结合上述答案,我发现这对我来说是最好的解决方案。双击节点以展开其子节点仍然有效,只有双击复选框会受到影响和修复:
class MyTreeView : TreeView
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0203 && this.CheckBoxes)
{
var localPos = this.PointToClient(Cursor.Position);
var hitTestInfo = this.HitTest(localPos);
if (hitTestInfo.Location == TreeViewHitTestLocations.StateImage)
{
m.Msg = 0x0201;
}
}
base.WndProc(ref m);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10665 次 |
最近记录: |