Change winform ToolTip backcolor

前端 未结 3 1931
萌比男神i
萌比男神i 2020-12-19 02:18

I am using a ToolTip control in my project. I want to set its backcolor red. I have changed ownerdraw property to true and backcolor to red. But no result. Any suggestion?

相关标签:
3条回答
  • 2020-12-19 02:49

    Add Event to toolstrip and set OwnerDraw to true:

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

    Then do add a method for 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));
     }
    
    0 讨论(0)
  • 2020-12-19 02:50

    Set these propeties:

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

    then on the Draw event use this :

    private void yourTooltip_Draw(object sender, DrawToolTipEventArgs e)
    {
        e.DrawBackground();
        e.DrawBorder();
        e.DrawText();
    }
    
    0 讨论(0)
  • 2020-12-19 03:08

    When you set a Control to OwnerDraw, you have to handle the drawing of the control yourself.

    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

    0 讨论(0)
提交回复
热议问题