我在我的项目中使用了 ToolTip 控件。我想将其背景色设置为红色。我已将 ownerdraw 属性更改为 true 并将背景色更改为红色。但没有结果。有什么建议吗?
问候,skpaul。
设置这些属性:
yourTooltip.OwnerDraw = true;
yourTooltip.BackColor = System.Drawing.Color.Red;
然后在 Draw 事件中使用:
private void yourTooltip_Draw(object sender, DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
e.DrawText();
}
将 Event 添加到工具条并将 OwnerDraw 设置为 true:
public Form1() {
InitializeComponent();
toolTip1.OwnerDraw = true;
toolTip1.Draw += new DrawToolTipEventHandler(toolTip1_Draw);
}
然后为 Draw Event 添加一个方法:
void toolTip1_Draw(object sender, DrawToolTipEventArgs e) {
Font f = new Font("Arial", 10.0f);
toolTip1.BackColor = System.Drawing.Color.Red;
e.DrawBackground();
e.DrawBorder();
e.Graphics.DrawString(e.ToolTipText, f, Brushes.Black, new PointF(2, 2));
}
当您将控件设置为 OwnerDraw 时,您必须自己处理控件的绘制。
这是一个快速而肮脏的示例(适应您的口味):
Private Sub ToolTip1_Draw(sender As Object, e As DrawToolTipEventArgs) Handles ToolTip1.Draw
Dim tt As ToolTip = CType(sender, ToolTip)
Dim b As Brush = New SolidBrush(tt.BackColor)
e.Graphics.FillRectangle(b, e.Bounds)
Dim sf As StringFormat = New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(e.ToolTipText, SystemFonts.DefaultFont, SystemBrushes.ActiveCaptionText, e.Bounds, sf)
sf.Dispose()
b.Dispose()
End Sub
干杯