简体   繁体   English

如何更改WinForms Treeview控件中用于节点文本的内联编辑的字体?

[英]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. 我正在填充WinForms TreeView控件,并在加载每个节点时分别设置其字体属性。

The nodes also allow inline editing (changing the text by pressing F2, or clicking once selected like folder names in Windows Explorer). 节点还允许进行内联编辑(通过按F2或单击一次选定的文本来更改文本,例如Windows资源管理器中的文件夹名称)。

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. 但是,当节点进入编辑模式时,编辑时使用的字体将还原为TreeView控件的默认字体,而不是该特定节点的字体。

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? 是否可以设置在编辑每个节点时使用的编辑控件的字体,以匹配用于显示该TreeView节点的字体? (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. 如您所说,对TreeNode源的检查表明,当节点进入编辑模式时,该节点正在使用“编辑控件”(来自Windows UI控件,而不是.NET Forms)。 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. 使用TVM_GETEDITCONTROL来获取它的句柄,并使用WM_SETFONT来设置字体。 You will probably want Font.ToHfont() , as well. 您可能还需要Font.ToHfont()

Edit: here's an example of how you can invoke SendMessage to accomplish the font change. 编辑:这是如何调用SendMessage来完成字体更改的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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