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);
}
請看看這個。 [http://stackoverflow.com/questions/4566388/copy-treeview-node][1]你可能從這裏得到一些幫助。 [1]:http://stackoverflow.com/questions/4566388/copy-treeview-node – mit
謝謝。不幸的是,只複製/粘貼整個標籤,而不是光標所在的位置/文本突出顯示。 –
SendKeys.Send(「^(c)」); //編程通過Ctrl + C編輯 – albert