3

当我编辑树节点(名称很长)时,它有一个背景颜色:

在此处输入图像描述

如何去除背景?如何更改背景颜色?

我试图在 OnDrawNode 中删除它,但这是不可能的。接下来,我尝试使用 TVM_GETEDITCONTROL 消息并发送 WM_CTLCOLOREDIT,但它也不起作用:

    [DllImport("user32.dll")]
    internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    internal const int WM_CTLCOLOREDIT = 0x0133;
    internal const int TVM_GETEDITCONTROL = 0x110F;

private void NodeTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
    {
        TreeNode nodeEditing = e.Node;
        IntPtr editControlHandle = SendMessage(NodeTree.Handle, (uint)TVM_GETEDITCONTROL, IntPtr.Zero, IntPtr.Zero);
        if (editControlHandle != IntPtr.Zero)
        {
            SendMessage(editControlHandle, (uint)WM_CTLCOLOREDIT, ColorTranslator.ToWin32(Color.Red), new IntPtr(1));
        }
    }
4

1 回答 1

2

此背景 - 所选节点的背景。在事件 BeforeLabelEdit 我从 AfterSelect 退订时,记住选定的节点并取消选择节点:

private TreeNode SelectedNodeBeforeEdit;

private void NodeTree_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
    NodeTree.AfterSelect -= NodeTree_AfterSelect;
    SelectedNodeBeforeEdit = NodeTree.SelectedNode;
    NodeTree.SelectedNode = null;
}

在 AfterLabelEdit 事件中,我订阅 AfterSelect 并展示所选节点:

private void NodeTree_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
    NodeTree.SelectedNode = SelectedNodeBeforeEdit;
    NodeTree.AfterSelect += NodeTree_AfterSelect;
}
于 2013-10-29T05:18:05.027 回答