Ant*_*nov 6 c# treeview winforms
我在winform上有TreeView控件.我希望让几个节点无法选择.我怎么能得到这个.
在我看来只有一个想法 - 自定义绘制节点,但可能更容易存在?请指教
我已经在BeforeSelect事件处理程序中尝试了这样的代码:
private void treeViewServers_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Parent != null)
{
e.Cancel = true;
}
}
Run Code Online (Sandbox Code Playgroud)
但它获得的效果是不合适的.当我按住鼠标左键时,节点临时获取选择.
提前致谢!
如果单击不可选择的节点,则可以完全禁用鼠标事件.
为此,您必须覆盖TreeView以下代码中显示的内容
public class MyTreeView : TreeView
{
int WM_LBUTTONDOWN = 0x0201; //513
int WM_LBUTTONUP = 0x0202; //514
int WM_LBUTTONDBLCLK = 0x0203; //515
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN ||
m.Msg == WM_LBUTTONUP ||
m.Msg == WM_LBUTTONDBLCLK)
{
//Get cursor position(in client coordinates)
Int16 x = (Int16)m.LParam;
Int16 y = (Int16)((int)m.LParam >> 16);
// get infos about the location that will be clicked
var info = this.HitTest(x, y);
// if the location is a node
if (info.Node != null)
{
// if is not a root disable any click event
if(info.Node.Parent != null)
return;//Dont dispatch message
}
}
//Dispatch as usual
base.WndProc(ref m);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3995 次 |
| 最近记录: |