简体   繁体   English

更改winform ToolTip backcolor

[英]Change winform ToolTip backcolor

I am using a ToolTip control in my project. 我在我的项目中使用ToolTip控件。 I want to set its backcolor red. 我想把它的背景颜色设置为红色。 I have changed ownerdraw property to true and backcolor to red. 我已将ownerdraw属性更改为true,将backcolor更改为红色。 But no result. 但没有结果。 Any suggestion? 有什么建议吗?

Regards, skpaul. 此致,skpaul。

Set these propeties: 设置这些功能:

yourTooltip.OwnerDraw = true; 
yourTooltip.BackColor = System.Drawing.Color.Red;

then on the Draw event use this : 然后在Draw事件上使用这个:

private void yourTooltip_Draw(object sender, DrawToolTipEventArgs e)
{
    e.DrawBackground();
    e.DrawBorder();
    e.DrawText();
}

Add Event to toolstrip and set OwnerDraw to true: 将事件添加到toolstrip并将OwnerDraw设置为true:

public Form1() {
     InitializeComponent();
     toolTip1.OwnerDraw = true;
     toolTip1.Draw += new DrawToolTipEventHandler(toolTip1_Draw);          
 }

Then do add a method for Draw Event: 然后为Draw事件添加一个方法:

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

When you set a Control to OwnerDraw, you have to handle the drawing of the control yourself. 将Control设置为OwnerDraw时,您必须自己处理控件的绘制。

Here's a quick and dirty example (adapt to your taste): 这是一个快速而肮脏的例子(适应你的口味):

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

Cheers 干杯

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

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