internal static string GetItemToolTipText(IntPtr hwnd, IntPtr hwndToolTip, int item) { if (hwndToolTip != IntPtr.Zero) { // We've found the tooltip window, so we won't need to scan for it. // Got a tooltip window - use it. NativeMethods.TOOLINFO tool = new NativeMethods.TOOLINFO(); tool.Init(Marshal.SizeOf(typeof(NativeMethods.TOOLINFO))); tool.hwnd = hwnd; tool.uId = item; return XSendMessage.GetItemText(hwndToolTip, tool); } else { // Control doesn't know its tooltip window - instead scan for one... // Enum the top-level windows owned by this thread... uint processId; uint threadId = GetWindowThreadProcessId(hwnd, out processId); UnsafeNativeMethods.ENUMTOOLTIPWINDOWINFO info = new UnsafeNativeMethods.ENUMTOOLTIPWINDOWINFO(); info.hwnd = hwnd; info.id = item; info.name = ""; UnsafeNativeMethods.EnumThreadWndProc enumToolTipWindows = new UnsafeNativeMethods.EnumThreadWndProc(EnumToolTipWindows); GCHandle gch = GCHandle.Alloc(enumToolTipWindows); UnsafeNativeMethods.EnumThreadWindows(threadId, enumToolTipWindows, ref info); gch.Free(); return info.name; } }
// ------------------------------------------------------ // // Private Methods // // ------------------------------------------------------ #region Private Methods private static bool EnumToolTipWindows(IntPtr hwnd, ref UnsafeNativeMethods.ENUMTOOLTIPWINDOWINFO lParam) { // Use ProxyGetClassName here instead of GetClassName(), // since for a [....] tooltip the latter will return // "WindowsForms10.tooltips_class32.app.0.b7ab7b". // Instead, ProxyGetClassName uses WM_GETOBJECT with // OBJID_QUERYCLASSNAMEIDX, which will return the correct answer. if (!ProxyGetClassName(hwnd).Equals("tooltips_class32")) { return true; } NativeMethods.TOOLINFO tool = new NativeMethods.TOOLINFO(); tool.Init(Marshal.SizeOf(typeof(NativeMethods.TOOLINFO))); // For tooltips with ids of 0, MFC will create the tooltip with the flag of TTF_IDISHWND. // TTF_IDISHWND indicates that the uId member is the window handle to the tool. if (lParam.id == 0) { tool.hwnd = Misc.GetParent(lParam.hwnd); tool.uId = unchecked((int)lParam.hwnd); tool.uFlags = NativeMethods.TTF_IDISHWND; } else { tool.hwnd = lParam.hwnd; tool.uId = lParam.id; } string name = XSendMessage.GetItemText(hwnd, tool); // Didn't get anything - continue looking... if (string.IsNullOrEmpty(name)) { return true; } lParam.name = name; // Got it - can stop iterating now. return false; }