// helper function for generating information about a particular control // use AssertControlInformation if sticking in an assert - then the work // to figure out the control info will only be done when the assertion is false. internal static string GetControlInformation(IntPtr hwnd) { if (hwnd == IntPtr.Zero) { return("Handle is IntPtr.Zero"); } string ret = ""; // in RETAIL just return empty string #if DEBUG try { int textLen = SafeNativeMethods.GetWindowTextLength(new HandleRef(null, hwnd)); StringBuilder sb = new StringBuilder(textLen + 1); UnsafeNativeMethods.GetWindowText(new HandleRef(null, hwnd), sb, sb.Capacity); string typeOfControl = "Unknown"; string nameOfControl = "Name: "; Control c = Control.FromHandle(hwnd); if (c != null) { typeOfControl = c.GetType().ToString(); if (!string.IsNullOrEmpty(c.Name)) { nameOfControl += c.Name; } else { nameOfControl += "Unknown"; // some extra debug info for toolstripdropdowns... if (c is ToolStripDropDown) { ToolStripDropDown dd = c as ToolStripDropDown; if (dd.OwnerItem != null) { nameOfControl += "\r\n\tOwnerItem: " + dd.OwnerItem.ToString(); } } } } ret = sb.ToString() + "\r\n\tType: " + typeOfControl + "\r\n\t" + nameOfControl + "\r\n"; } catch (SecurityException) { // some suites run under DEBUG - just eat this exception } #endif return(ret); }
/// <summary> /// helper function for generating information about a particular control /// use AssertControlInformation if sticking in an assert - then the work /// to figure out the control info will only be done when the assertion is false. /// </summary> internal static string GetControlInformation(IntPtr hwnd) { if (hwnd == IntPtr.Zero) { return("Handle is IntPtr.Zero"); } #if DEBUG int textLen = SafeNativeMethods.GetWindowTextLength(new HandleRef(null, hwnd)); StringBuilder sb = new StringBuilder(textLen + 1); UnsafeNativeMethods.GetWindowText(new HandleRef(null, hwnd), sb, sb.Capacity); string typeOfControl = "Unknown"; string nameOfControl = "Name: "; Control c = Control.FromHandle(hwnd); if (c != null) { typeOfControl = c.GetType().ToString(); if (!string.IsNullOrEmpty(c.Name)) { nameOfControl += c.Name; } else { nameOfControl += "Unknown"; // Add some extra debug info for ToolStripDropDowns. if (c is ToolStripDropDown dd && dd.OwnerItem != null) { nameOfControl += Environment.NewLine + "\tOwnerItem: " + dd.OwnerItem.ToString(); } } } return(sb.ToString() + Environment.NewLine + "\tType: " + typeOfControl + Environment.NewLine + "\t" + nameOfControl + Environment.NewLine); #else return(string.Empty); #endif }