You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Framework 1.1 WinForms中自定义Tooltip UI?

Customizing ToolTip UI in .NET Framework 1.1 WinForms

Hey there! I totally get the frustration with the sealed ToolTip class in .NET Framework 1.1—customizing it feels like hitting a wall at first. But don't worry, there are a couple of workarounds to get that custom UI you need, including custom background/foreground colors and embedded images.

Option 1: Build a Custom Floating Control (Simplest & Most Reliable)

Since you can't inherit from ToolTip, the easiest approach is to create your own floating form/usercontrol that mimics ToolTip behavior. You have full control over its appearance and content.

Step 1: Create the Custom ToolTip Form

Make a new Form (or UserControl) tailored to your needs. Set styles to make it look like a tooltip, then handle painting to add your custom elements:

public class CustomToolTip : Form
{
    private string _tipText;
    private Image _tipImage;

    public CustomToolTip()
    {
        // Configure form to act like a tooltip
        FormBorderStyle = FormBorderStyle.None;
        ShowInTaskbar = false;
        StartPosition = FormStartPosition.Manual;
        BackColor = Color.LightSalmon; // Your custom background
        ForeColor = Color.DarkRed;     // Your custom text color
        Size = new Size(220, 90);      // Base size (adjust later if needed)
    }

    // Method to update tooltip content
    public void SetContent(string text, Image image)
    {
        _tipText = text;
        _tipImage = image;
        Invalidate(); // Trigger repaint with new content
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        
        // Draw custom image if provided
        if (_tipImage != null)
        {
            // Position image at top-left with padding
            e.Graphics.DrawImage(_tipImage, 12, 12, 48, 48);
        }

        // Draw tooltip text
        using (var textBrush = new SolidBrush(ForeColor))
        {
            // Offset text if image exists, else start at left padding
            int textX = _tipImage != null ? 70 : 12;
            e.Graphics.DrawString(_tipText, Font, textBrush, textX, 25);
        }
    }
}

Step 2: Use the Custom ToolTip with Your Controls

Attach mouse events to the controls where you want the tooltip to appear, then show/hide your custom form:

private CustomToolTip _myCustomTip;

private void MyButton_MouseHover(object sender, EventArgs e)
{
    var targetControl = sender as Control;
    if (targetControl == null) return;

    // Initialize tooltip if needed
    if (_myCustomTip == null)
    {
        _myCustomTip = new CustomToolTip();
    }

    // Set your desired content
    _myCustomTip.SetContent(
        "This is my custom tooltip text!",
        Properties.Resources.MyCustomImage // Replace with your image resource
    );

    // Position tooltip below/right of the target control
    var screenPos = targetControl.PointToScreen(new Point(targetControl.Width, targetControl.Height));
    _myCustomTip.Location = screenPos;
    _myCustomTip.Show();
}

private void MyButton_MouseLeave(object sender, EventArgs e)
{
    // Hide tooltip when mouse leaves the control
    if (_myCustomTip != null && _myCustomTip.Visible)
    {
        _myCustomTip.Hide();
    }
}

Option 2: Subclass ToolTip via NativeWindow (Advanced)

For more advanced scenarios, you can subclass the underlying ToolTip window using NativeWindow to intercept Windows messages (like WM_PAINT) and customize drawing. This requires knowledge of Win32 APIs, but lets you reuse the built-in ToolTip triggering logic.

However, this approach is more complex and error-prone in .NET 1.1. The custom floating control method is almost always the better choice for most use cases.

Key Notes

  • You can add extra logic like auto-hiding after a delay, adjusting form size based on text length, or adding rounded corners (via region clipping) to make it look more polished.
  • Make sure to handle edge cases (like the tooltip going off-screen) by adjusting the position if needed.

内容的提问来源于stack exchange,提问作者Vignesh Nethaji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:36:34