public static int GetTextMetrics(HandleRef hDC, ref NativeMethods.TEXTMETRIC lptm) { if (Marshal.SystemDefaultCharSize == 1) { NativeMethods.TEXTMETRICA lptm2 = default(NativeMethods.TEXTMETRICA); int textMetricsA = GetTextMetricsA(hDC, ref lptm2); lptm.tmHeight = lptm2.tmHeight; lptm.tmAscent = lptm2.tmAscent; lptm.tmDescent = lptm2.tmDescent; lptm.tmInternalLeading = lptm2.tmInternalLeading; lptm.tmExternalLeading = lptm2.tmExternalLeading; lptm.tmAveCharWidth = lptm2.tmAveCharWidth; lptm.tmMaxCharWidth = lptm2.tmMaxCharWidth; lptm.tmWeight = lptm2.tmWeight; lptm.tmOverhang = lptm2.tmOverhang; lptm.tmDigitizedAspectX = lptm2.tmDigitizedAspectX; lptm.tmDigitizedAspectY = lptm2.tmDigitizedAspectY; lptm.tmFirstChar = (char)lptm2.tmFirstChar; lptm.tmLastChar = (char)lptm2.tmLastChar; lptm.tmDefaultChar = (char)lptm2.tmDefaultChar; lptm.tmBreakChar = (char)lptm2.tmBreakChar; lptm.tmItalic = lptm2.tmItalic; lptm.tmUnderlined = lptm2.tmUnderlined; lptm.tmStruckOut = lptm2.tmStruckOut; lptm.tmPitchAndFamily = lptm2.tmPitchAndFamily; lptm.tmCharSet = lptm2.tmCharSet; return(textMetricsA); } return(GetTextMetricsW(hDC, ref lptm)); }
// Since MessageBox will grow taller than the screen if there are too many lines do // a rough calculation to make it fit. private static string TruncateMessageToFitScreen(string message) { const int MaxCharsPerLine = 80; IntPtr hFont = SafeNativeMethods.GetStockObject(NativeMethods.DEFAULT_GUI_FONT); IntPtr hdc = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef); NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); hFont = UnsafeNativeMethods.SelectObject(new HandleRef(null, hdc), new HandleRef(null, hFont)); SafeNativeMethods.GetTextMetrics(new HandleRef(null, hdc), tm); UnsafeNativeMethods.SelectObject(new HandleRef(null, hdc), new HandleRef(null, hFont)); UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, hdc)); hdc = IntPtr.Zero; int cy = UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CYSCREEN); int maxLines = cy / tm.tmHeight - 15; int lineCount = 0; int lineCharCount = 0; int i = 0; while (lineCount < maxLines && i < message.Length - 1) { char ch = message[i]; lineCharCount++; if (ch == '\n' || ch == '\r' || lineCharCount > MaxCharsPerLine) { lineCount++; lineCharCount = 0; } // treat \r\n or \n\r as a single line break if (ch == '\r' && message[i + 1] == '\n') { i += 2; } else if (ch == '\n' && message[i + 1] == '\r') { i += 2; } else { i++; } } if (i < message.Length - 1) { message = message.Substring(0, i) + "...\r\n<truncated>"; } return(message); }
/// <summary> /// Identifes where the text baseline for our control which should be based on bounds, padding, font, and textalignment. /// </summary> public static int GetTextBaseline(Control ctrl, ContentAlignment alignment) { //determine the actual client area we are working in (w/padding) Rectangle face = ctrl.ClientRectangle; //get the font metrics via gdi int fontAscent = 0; int fontHeight = 0; using (Graphics g = ctrl.CreateGraphics()) { IntPtr dc = g.GetHdc(); IntPtr hFont = ctrl.Font.ToHfont(); IntPtr hFontOld; try { hFontOld = SafeNativeMethods.SelectObject(new HandleRef(ctrl, dc), new HandleRef(ctrl, hFont)); NativeMethods.TEXTMETRIC metrics = new NativeMethods.TEXTMETRIC(); SafeNativeMethods.GetTextMetrics(new HandleRef(ctrl, dc), metrics); //add the font ascent to the baseline fontAscent = metrics.tmAscent + 1; fontHeight = metrics.tmHeight; SafeNativeMethods.SelectObject(new HandleRef(ctrl, dc), new HandleRef(ctrl, hFontOld)); } finally { SafeNativeMethods.DeleteObject(new HandleRef(ctrl.Font, hFont)); g.ReleaseHdc(dc); } } //now add it all up if ((alignment & anyTopAlignment) != 0) { return(face.Top + fontAscent); } else if ((alignment & anyMiddleAlignment) != 0) { return(face.Top + (face.Height / 2) - (fontHeight / 2) + fontAscent); } else { return(face.Bottom - fontHeight + fontAscent); } }
private static string TruncateMessageToFitScreen(string message) { IntPtr stockObject = Microsoft.Win32.SafeNativeMethods.GetStockObject(0x11); IntPtr dC = Microsoft.Win32.UnsafeNativeMethods.GetDC(IntPtr.Zero); NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); stockObject = Microsoft.Win32.UnsafeNativeMethods.SelectObject(dC, stockObject); Microsoft.Win32.SafeNativeMethods.GetTextMetrics(dC, tm); Microsoft.Win32.UnsafeNativeMethods.SelectObject(dC, stockObject); Microsoft.Win32.UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, dC); dC = IntPtr.Zero; int num2 = (Microsoft.Win32.UnsafeNativeMethods.GetSystemMetrics(1) / tm.tmHeight) - 15; int num3 = 0; int num4 = 0; int length = 0; while ((num3 < num2) && (length < (message.Length - 1))) { char ch = message[length]; num4++; if (((ch == '\n') || (ch == '\r')) || (num4 > 80)) { num3++; num4 = 0; } if ((ch == '\r') && (message[length + 1] == '\n')) { length += 2; } else { if ((ch == '\n') && (message[length + 1] == '\r')) { length += 2; continue; } length++; } } if (length < (message.Length - 1)) { message = SR.GetString("DebugMessageTruncated", new object[] { message.Substring(0, length) }); } return(message); }
private void ActivateDropDown() { if (this._editor != null) { try { object newValue = this._editor.EditValue(base.TypeDescriptorContext, this, base.Value); base.SetValue(newValue); } catch (Exception exception) { base.ActionPanel.ShowError(System.Design.SR.GetString("DesignerActionPanel_ErrorActivatingDropDown", new object[] { exception.Message })); } } else { ListBox wrapper = new ListBox { BorderStyle = BorderStyle.None, IntegralHeight = false, Font = base.ActionPanel.Font }; wrapper.SelectedIndexChanged += new EventHandler(this.OnListBoxSelectedIndexChanged); wrapper.KeyDown += new KeyEventHandler(this.OnListBoxKeyDown); TypeConverter.StandardValuesCollection standardValues = base.GetStandardValues(); if (standardValues != null) { foreach (object obj3 in standardValues) { string item = base.PropertyDescriptor.Converter.ConvertToString(base.TypeDescriptorContext, CultureInfo.CurrentCulture, obj3); wrapper.Items.Add(item); if ((obj3 != null) && obj3.Equals(base.Value)) { wrapper.SelectedItem = item; } } } int num = 0; IntPtr dC = UnsafeNativeMethods.GetDC(new HandleRef(wrapper, wrapper.Handle)); IntPtr handle = wrapper.Font.ToHfont(); NativeMethods.CommonHandles.GdiHandleCollector.Add(); NativeMethods.TEXTMETRIC lptm = new NativeMethods.TEXTMETRIC(); try { handle = SafeNativeMethods.SelectObject(new HandleRef(wrapper, dC), new HandleRef(wrapper.Font, handle)); if (wrapper.Items.Count > 0) { NativeMethods.SIZE size = new NativeMethods.SIZE(); foreach (string str2 in wrapper.Items) { SafeNativeMethods.GetTextExtentPoint32(new HandleRef(wrapper, dC), str2, str2.Length, size); num = Math.Max(size.cx, num); } } SafeNativeMethods.GetTextMetrics(new HandleRef(wrapper, dC), ref lptm); num += (2 + lptm.tmMaxCharWidth) + SystemInformation.VerticalScrollBarWidth; handle = SafeNativeMethods.SelectObject(new HandleRef(wrapper, dC), new HandleRef(wrapper.Font, handle)); } finally { SafeNativeMethods.DeleteObject(new HandleRef(wrapper.Font, handle)); UnsafeNativeMethods.ReleaseDC(new HandleRef(wrapper, wrapper.Handle), new HandleRef(wrapper, dC)); } wrapper.Height = Math.Max(lptm.tmHeight + 2, Math.Min(200, wrapper.PreferredHeight)); wrapper.Width = Math.Max(num, base.EditRegionSize.Width); this._ignoreDropDownValue = false; try { this.ShowDropDown(wrapper, SystemColors.ControlDark); } finally { wrapper.SelectedIndexChanged -= new EventHandler(this.OnListBoxSelectedIndexChanged); wrapper.KeyDown -= new KeyEventHandler(this.OnListBoxKeyDown); } if (!this._ignoreDropDownValue && (wrapper.SelectedItem != null)) { base.SetValue(wrapper.SelectedItem); } } }
public static extern int GetTextMetricsW(HandleRef hDC, ref NativeMethods.TEXTMETRIC lptm);
public static extern bool GetTextMetrics(HandleRef hdc, NativeMethods.TEXTMETRIC tm);
private SizeF GetFontAutoScaleDimensions() { SizeF retval = SizeF.Empty; // Windows uses CreateCompatibleDC(NULL) to get a // memory DC for the monitor the application is currently // on. IntPtr dc = UnsafeNativeMethods.CreateCompatibleDC(NativeMethods.NullHandleRef); if (dc == IntPtr.Zero) { throw new Win32Exception(); } HandleRef hdc = new HandleRef(this, dc); try { // We clone the Windows scaling function here as closely as // possible. They use textmetric for height, and textmetric // for width of fixed width fonts. For variable width fonts // they use GetTextExtentPoint32 and pass in a long a-Z string. // We must do the same here if our dialogs are to scale in a // similar fashion. HandleRef hfont = new HandleRef(this, FontHandle); HandleRef hfontOld = new HandleRef(this, SafeNativeMethods.SelectObject(hdc, hfont)); try { NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); SafeNativeMethods.GetTextMetrics(hdc, ref tm); retval.Height = tm.tmHeight; if ((tm.tmPitchAndFamily & NativeMethods.TMPF_FIXED_PITCH) != 0) { IntNativeMethods.SIZE size = new IntNativeMethods.SIZE(); IntUnsafeNativeMethods.GetTextExtentPoint32(hdc, fontMeasureString, size); // Note: intentional integer round off here for Win32 compat //retval.Width = (float)(((size.cx / 26) + 1) / 2); retval.Width = (int)Math.Round(((float)size.cx) / ((float)fontMeasureString.Length)); } else { retval.Width = tm.tmAveCharWidth; } } finally { SafeNativeMethods.SelectObject(hdc, hfontOld); } } finally { UnsafeNativeMethods.DeleteCompatibleDC(hdc); } return retval; }
public virtual void PopupDialog(int row) { Debug.WriteLineIf(CompModSwitches.DebugGridView.TraceVerbose, "PropertyGridView:PopupDialog"); GridEntry gridEntry = GetGridEntryFromRow(row); if (gridEntry != null) { if (dropDownHolder != null && dropDownHolder.GetUsed()) { CloseDropDown(); return; } bool fBtnDropDown = gridEntry.NeedsDropDownButton; bool fEnum = gridEntry.Enumerable; bool fBtnDialog = gridEntry.NeedsCustomEditorButton; if (fEnum && !fBtnDropDown) { DropDownListBox.Items.Clear(); object value = gridEntry.PropertyValue; object[] rgItems = gridEntry.GetPropertyValueList(); int maxWidth = 0; // The listbox draws with GDI, not GDI+. So, we // use a normal DC here. // IntPtr hdc = UnsafeNativeMethods.GetDC(new HandleRef(DropDownListBox, DropDownListBox.Handle)); IntPtr hFont = Font.ToHfont(); System.Internal.HandleCollector.Add(hFont, NativeMethods.CommonHandles.GDI); NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC(); int iSel = -1; try { hFont = SafeNativeMethods.SelectObject(new HandleRef(DropDownListBox, hdc), new HandleRef(Font, hFont)); iSel = GetCurrentValueIndex(gridEntry); if (rgItems != null && rgItems.Length > 0) { string s; IntNativeMethods.SIZE textSize = new IntNativeMethods.SIZE(); for (int i = 0; i < rgItems.Length; i++) { s = gridEntry.GetPropertyTextValue(rgItems[i]); DropDownListBox.Items.Add(s); IntUnsafeNativeMethods.GetTextExtentPoint32(new HandleRef(DropDownListBox, hdc), s, textSize); maxWidth = Math.Max((int) textSize.cx, maxWidth); } } SafeNativeMethods.GetTextMetrics(new HandleRef(DropDownListBox, hdc), ref tm); // border + padding + scrollbar maxWidth += 2 + tm.tmMaxCharWidth + SystemInformation.VerticalScrollBarWidth; hFont = SafeNativeMethods.SelectObject(new HandleRef(DropDownListBox, hdc), new HandleRef(Font, hFont)); } finally { SafeNativeMethods.DeleteObject(new HandleRef(Font, hFont)); UnsafeNativeMethods.ReleaseDC(new HandleRef(DropDownListBox, DropDownListBox.Handle), new HandleRef(DropDownListBox, hdc)); } // [....], 4/25/1998 - must check for -1 and not call the set... if (iSel != -1) { DropDownListBox.SelectedIndex = iSel; } SetFlag(FlagDropDownCommit, false); DropDownListBox.Height = Math.Max(tm.tmHeight + 2, Math.Min(maxListBoxHeight, DropDownListBox.PreferredHeight)); DropDownListBox.Width = Math.Max(maxWidth, GetRectangle(row,ROWVALUE).Width); try { bool resizable = DropDownListBox.Items.Count > (DropDownListBox.Height / DropDownListBox.ItemHeight); SetFlag(FlagResizableDropDown, resizable); DropDownControl(DropDownListBox); } finally { SetFlag(FlagResizableDropDown, false); } Refresh(); } else if (fBtnDialog || fBtnDropDown) { try { SetFlag(FlagInPropertySet, true); Edit.DisableMouseHook = true; try { SetFlag(FlagResizableDropDown, gridEntry.UITypeEditor.IsDropDownResizable); gridEntry.EditPropertyValue(this); } finally { SetFlag(FlagResizableDropDown, false); } } finally { SetFlag(FlagInPropertySet, false); Edit.DisableMouseHook = false; } Refresh(); // as/urt 31468 -- we can't do this because // some dialogs are non-modal, and // this will pull focus from them. // See ASURT 31468. // //if (fBtnDialog) { // this.Focus(); //} if (FocusInside) { SelectGridEntry(gridEntry, false); } } } }
public static extern bool GetTextMetrics(IntPtr hDC, [In][Out] NativeMethods.TEXTMETRIC tm);