TreeView 控件使用动态创建的 TextBox 来编辑标签。您可以获得该文本框的句柄并向其发送 WM_CUT、WM_PASTE 和 WM_COPY 消息。向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。您可以使用其 IsEditing 属性或其 BeforeLabelEdit 和 AfterLabelEdit 事件来检查您的快捷方式是否可以工作。
using System;
using System.Windows.Forms;
class MyTreeView : TreeView {
public bool IsEditing { get; private set; }
public void Cut() { SendMessage(GetEditControl(), 0x300, IntPtr.Zero, IntPtr.Zero); }
public void Copy() { SendMessage(GetEditControl(), 0x301, IntPtr.Zero, IntPtr.Zero); }
public void Paste() { SendMessage(GetEditControl(), 0x302, IntPtr.Zero, IntPtr.Zero); }
protected override void OnBeforeLabelEdit(NodeLabelEditEventArgs e) {
IsEditing = true;
base.OnBeforeLabelEdit(e);
}
protected override void OnAfterLabelEdit(NodeLabelEditEventArgs e) {
IsEditing = false;
base.OnAfterLabelEdit(e);
}
private IntPtr GetEditControl() {
// Use TVM_GETEDITCONTROL to get the handle of the edit box
IntPtr hEdit = SendMessage(this.Handle, 0x1100 + 15, IntPtr.Zero, IntPtr.Zero);
if (hEdit == IntPtr.Zero) throw new InvalidOperationException("Not currently editing a label");
return hEdit;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}