简体   繁体   中英

How do you change the font used for inline editing of the node text in a WinForms Treeview control?

I am populating a WinForms TreeView control and setting the font attributes of each node differently as they are loaded.

The nodes also allow inline editing (changing the text by pressing F2, or clicking once selected like folder names in Windows Explorer).

When the node goes into edit mode though, the font used when editing reverts to the default font of the TreeView control, not that specific node's font.

Is it possible to set the font of the edit control used when editing each node, to match the font used for displaying that TreeView node? (If so, how?)

As you said, an examination of the TreeNode source reveals that the node is using an Edit Control (from Windows UI Controls, not .NET Forms) when it goes into edit mode. I don't see anything in the class that will set the font in edit mode, so I think you will need to post messages directly to the Edit Control. Use TVM_GETEDITCONTROL to get a handle to it, and WM_SETFONT to set the font. You will probably want Font.ToHfont() , as well.

Edit: here's an example of how you can invoke SendMessage to accomplish the font change.

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

internal const int WM_SETFONT = 0x0030;
internal const int TVM_GETEDITCONTROL = 0x110F;

private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
    TreeNode nodeEditing = e.Node;
    IntPtr editControlHandle = SendMessage(treeView1.Handle, (uint)TVM_GETEDITCONTROL, IntPtr.Zero, IntPtr.Zero);
    if (editControlHandle != IntPtr.Zero)
    {
        SendMessage(editControlHandle, (uint)WM_SETFONT, nodeEditing.NodeFont.ToHfont(), New IntPtr(1));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM