我在项目中使用ToolTip控件。我想把它的背景颜色设置为红色。我已将ownerdraw属性更改为true,将backcolor更改为红色。但没有结果。有什么建议吗?
此致 skpaul。
答案 0 :(得分:17)
设置这些属性:
yourTooltip.OwnerDraw = true;
yourTooltip.BackColor = System.Drawing.Color.Red;
然后在Draw事件中使用:
private void yourTooltip_Draw(object sender, DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
e.DrawText();
}
答案 1 :(得分:7)
将事件添加到工具栏并将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));
}
答案 2 :(得分:1)
当您将Control设置为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
干杯