如何格式化DataGridView在虚拟模式下使用的工具提示?

问题描述:

我需要将格式设置 - 特别是粗体文本 - 应用于DataGridView在虚拟模式下使用的工具提示。我可以在CellToolTipTextNeeded事件中设置文本,但不支持HTML标记;有没有其他的语法我应该使用?我不想自己重新实现工具提示支持。如何格式化DataGridView在虚拟模式下使用的工具提示?

可以使用HtmlToolTip从 http://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspx

To use it with DataGridView create a ToolTip (HtmlToolTip) and add this after the InitalizeComponent() in your form to replace the default tooltip: 

System.Reflection.FieldInfo toolTipControlFieldInfo= 
typeof(DataGridView).GetField("toolTipControl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); 

System.Reflection.FieldInfo toolTipFieldInfo= 
toolTipControlFieldInfo.FieldType.GetField("toolTip", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); 

object toolTipControlInstance = 
toolTipControlFieldInfo.GetValue(myDataGridView); 

toolTipFieldInfo.SetValue(toolTipControlInstance, myToolTip); 

工作对我来说与.NET 3.5

正如事件的名称所暗示的那样,它只是希望显示文本,而不是格式化。

如果您想要任何类似粗体或其他类型的格式,您将不得不自己处理工具提示的显示和绘制。您可以使用ToolTip控件来帮助您,将OwnerDraw属性设置为true并处理Draw事件,但请注意,您很可能必须以重要方式重写网格,以便在适当的时间进入该事件。