Tooltips on disabled buttons

After searching the web quite extensively looking for a solution to this problem, i found many other people were having the same problem. But none had an answer, so after a few hours of hacking around i have managed to come up with a solution. It’s not pretty (it uses win32 api calls), but it does the job.
 int WM_MOUSEMOVE = 0x0200;

[StructLayout(LayoutKind.Sequential)]
 public class POINT
 {
public long x;
public long y;
 }

[DllImport("user32", CharSet=CharSet.Auto)]
 public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, POINT lParam);
 
When a button is disabled it does not receive any mouse related messages. The parent of the button does receive them. So i put a MouseMove event handler in place for my form. I still use the standard Winforms tooltip control to hold the tooltip text for the button.
 private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
 {
Point pt = PointToClient(Cursor.Position);
Control control = GetChildAtPoint(pt);
if (control != null)
{
if (control is Button && !control.Enabled)
{
string tip = toolTip1.GetToolTip(control);
if (tip.Length > 0)
{
POINT point = new POINT();
                  point.x = Cursor.Position.X;
                  point.y = Cursor.Position.Y;
                  SendMessage(control.Handle, (int) WindowsMessages.WM_MOUSEMOVE, 0, point);
              }
          }
}
 }
 
This seems to work perfectly. Hope it helps someone else out
Advertisements

2 responses to “Tooltips on disabled buttons

  1. It did indeed! I found a number of gross, complicated ways to get disabled UI objects to display their tooltips, before I found this – still gross, perhaps, given it uses a pinvoke, but we use loads of pinvoke calls already, and at least it’s simple and obvious what it’s doing.

  2. Hi Antony, thanks for the sample. I can’t disagree that, this piece of of information is very rare to be found on net. Although, i am trying to get a working sample out of it, but havn’t been able to.

    Do you have any source that you can share, which compiles perfectly?

    Looking forward for your help.

    Regards,
    Abby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s