复制/粘贴树视图节点标签 [英] Copy / Paste Treeview Node Label

查看:28
本文介绍了复制/粘贴树视图节点标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想支持使用 treeView 进行复制/粘贴.如果选择节点,则必须复制/粘贴节点.如果您正忙于重命名标签,则必须复制/粘贴标签中的文本.还有复制/粘贴按钮,它们必须具有与快捷键 Ctrl+C 和 Ctrl+V 相同的功能.

I want to support copy / paste with a treeView. If you select the Node, then it must copy / paste the node. If you are busy renaming the label, it must copy / paste the text in the label. There are also copy / paste buttons and they must do the same function as the shortcut keys Ctrl+C and Ctrl+V.

我想了两个选择:

1) 为控件添加 keydown 方法.您如何为标签添加对复制/粘贴的支持?

1) Add a keydown method for the Controls. How do you add support for Copy / Paste for the Label?

TreeNode selectedNode = trvProjects.SelectedNode;    
if (selectedNode.IsEditing)
{
    // Copy Label
    selectedNode.Copy(); // .Copy / .Paste() are not supported. I need help with this
}
else
{
    // Copy Node
    CopyNode(selectedNode); // Got this working
}

2) 有没有一种方法,对于标签编辑,它使用复制/粘贴中的构建,如果选择了节点,则启动自定义代码?

2) Is there a way that for Label edit, it use the build in Copy / Paste, and if the Node is selected, custom code is launched?

有更好的方法吗?

我不介意示例是 C# 还是 VB.

I do not mind if the sample is in C# or VB.

谢谢!

推荐答案

TreeView 控件使用动态创建的 TextBox 来编辑标签.您可以获得该文本框的句柄并将 WM_CUT、WM_PASTE 和 WM_COPY 消息发送给它.向您的项目添加一个新类并粘贴如下所示的代码.编译.将新控件从工具箱顶部拖放到表单上.您可以使用其 IsEditing 属性或其 BeforeLabelEdit 和 AfterLabelEdit 事件来检查您的快捷方式是否有效.

The TreeView control uses a dynamically created TextBox to edit the label. You can get a handle to that text box and send it the WM_CUT, WM_PASTE and WM_COPY messages. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. You can use its IsEditing property or its BeforeLabelEdit and AfterLabelEdit events to check if your shortcuts are going to work.

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);
}

这篇关于复制/粘贴树视图节点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆