Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
all i want is a transparent (less opacity tool tip)

here is what I've so far

C#
public partial class CustomToolTip : ToolTip    
{
    public CustomToolTip()
    {
        InitializeComponent();
        this.BackColor = Color.FromArgb(127, 0, 0, 0);
        this.OwnerDraw = true;
        this.Popup += new PopupEventHandler(this.OnPopup);
        this.Draw += new DrawToolTipEventHandler(this.OnDraw);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams CP = base.CreateParams;
            CP.ExStyle = CP.ExStyle | 0x20;
            return CP;
        }
    }

    private void OnPopup(object sender, PopupEventArgs e)
    {
        e.ToolTipSize = new Size(200, 100);
    }

    private void OnDraw(object sender, DrawToolTipEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.Bounds);
    }
}




but i end up having a black rectangle at setting the tooltip size which i can't get ride of

any idea how could i draw without setting size or even remove that black rectangle ?
Posted

1 solution

answer by Loathing, maybe you folks wanna have a look how it was done

C#
public class CustomToolTip : ToolTip {
    public CustomToolTip() {
        this.BackColor = Color.FromArgb(127, 255, 0, 0);
        this.OwnerDraw = true;
        this.Popup += new PopupEventHandler(this.OnPopup);
        this.Draw += new DrawToolTipEventHandler(this.OnDraw);
    }

    private void OnPopup(Object sender, PopupEventArgs e) {
        e.ToolTipSize = new Size(200, 100);
        var window = typeof(ToolTip).GetField("window", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this) as NativeWindow;

        var Handle = window.Handle;
        SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
        SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
    }

    public const int GWL_EXSTYLE = -20;
    public const int WS_EX_LAYERED = 0x80000;
    public const int LWA_ALPHA = 0x2;
    public const int LWA_COLORKEY = 0x1;

    [DllImport("user32.dll")]
    static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey,byte bAlpha, uint dwFlags);
    [DllImport("user32.dll", SetLastError=true)]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    private void OnDraw(object sender, DrawToolTipEventArgs e) {
        e.Graphics.FillRectangle(new SolidBrush(this.BackColor), e.Bounds);
        e.Graphics.DrawString(e.ToolTipText, e.Font, Brushes.Black, e.Bounds);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900