private static IntTextFormatFlags GetIntTextFormatFlags(TextFormatFlags flags) { if (((uint)flags & WindowsGraphics.GdiUnsupportedFlagMask) == 0) { return((IntTextFormatFlags)flags); } // Clear TextRenderer custom flags. IntTextFormatFlags windowsGraphicsSupportedFlags = (IntTextFormatFlags)(((uint)flags) & ~WindowsGraphics.GdiUnsupportedFlagMask); return(windowsGraphicsSupportedFlags); }
public Size MeasureText(string text, WindowsFont font, Size proposedSize, IntTextFormatFlags flags) { if (string.IsNullOrEmpty(text)) { return(Size.Empty); } IntNativeMethods.DRAWTEXTPARAMS lpDTParams = null; if (MeasurementDCInfo.IsMeasurementDC(this.DeviceContext)) { lpDTParams = MeasurementDCInfo.GetTextMargins(this, font); } if (lpDTParams == null) { lpDTParams = this.GetTextMargins(font); } int num = (1 + lpDTParams.iLeftMargin) + lpDTParams.iRightMargin; if (proposedSize.Width <= num) { proposedSize.Width = num; } if (proposedSize.Height <= 0) { proposedSize.Height = 1; } IntNativeMethods.RECT lpRect = IntNativeMethods.RECT.FromXYWH(0, 0, proposedSize.Width, proposedSize.Height); HandleRef hDC = new HandleRef(null, this.dc.Hdc); if (font != null) { this.dc.SelectFont(font); } if ((proposedSize.Height >= MaxSize.Height) && ((flags & IntTextFormatFlags.SingleLine) != IntTextFormatFlags.Default)) { flags &= ~(IntTextFormatFlags.Bottom | IntTextFormatFlags.VerticalCenter); } if (proposedSize.Width == MaxSize.Width) { flags &= ~IntTextFormatFlags.WordBreak; } flags |= IntTextFormatFlags.CalculateRectangle; IntUnsafeNativeMethods.DrawTextEx(hDC, text, ref lpRect, (int)flags, lpDTParams); return(lpRect.Size); }
public static Rectangle AdjustForVerticalAlignment(HandleRef hdc, string text, Rectangle bounds, IntTextFormatFlags flags, IntNativeMethods.DRAWTEXTPARAMS dtparams) { if (((((flags & IntTextFormatFlags.Bottom) == IntTextFormatFlags.Default) && ((flags & IntTextFormatFlags.VerticalCenter) == IntTextFormatFlags.Default)) || ((flags & IntTextFormatFlags.SingleLine) != IntTextFormatFlags.Default)) || ((flags & IntTextFormatFlags.CalculateRectangle) != IntTextFormatFlags.Default)) { return bounds; } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); flags |= IntTextFormatFlags.CalculateRectangle; int num = IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int) flags, dtparams); if (num > bounds.Height) { return bounds; } Rectangle rectangle = bounds; if ((flags & IntTextFormatFlags.VerticalCenter) != IntTextFormatFlags.Default) { rectangle.Y = (rectangle.Top + (rectangle.Height / 2)) - (num / 2); return rectangle; } rectangle.Y = rectangle.Bottom - num; return rectangle; }
/// <devdoc> /// Draws the text at the specified point, using the given Font and foreColor, and according to the /// specified flags. /// </devdoc> public void DrawText(string text, WindowsFont font, Point pt, Color foreColor, IntTextFormatFlags flags) { DrawText(text, font, pt, foreColor, Color.Empty, flags); }
/// <devdoc> /// <para> /// The GDI DrawText does not do multiline alignment when IntTextFormatFlags.SingleLine is not set. This /// adjustment is to workaround that limitation. We don't want to duplicate SelectObject calls here, /// so put your Font in the dc before calling this. /// /// AdjustForVerticalAlignment is only used when the text is multiline and it fits inside the bounds passed in. /// In that case we want the horizontal center of the multiline text to be at the horizontal center of the bounds. /// /// If the text is multiline and it does not fit inside the bounds passed in, then return the bounds that were passed in. /// This way we paint the top of the text at the top of the bounds passed in. /// </para> /// </devdoc> public static Rectangle AdjustForVerticalAlignment(HandleRef hdc, string text, Rectangle bounds, IntTextFormatFlags flags, IntNativeMethods.DRAWTEXTPARAMS dtparams) { Debug.Assert( ((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!" ); // Ok if any Top (Cannot test IntTextFormatFlags.Top because it is 0), single line text or measuring text. bool isTop = (flags & IntTextFormatFlags.Bottom) == 0 && (flags & IntTextFormatFlags.VerticalCenter) == 0; if( isTop ||((flags & IntTextFormatFlags.SingleLine) != 0) || ((flags & IntTextFormatFlags.CalculateRectangle) != 0) ) { return bounds; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); // Get the text bounds. flags |= IntTextFormatFlags.CalculateRectangle; int textHeight = IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int) flags, dtparams); // if the text does not fit inside the bounds then return the bounds that were passed in if (textHeight > bounds.Height) { return bounds; } Rectangle adjustedBounds = bounds; if( (flags & IntTextFormatFlags.VerticalCenter) != 0 ) // Middle { adjustedBounds.Y = adjustedBounds.Top + adjustedBounds.Height / 2 - textHeight / 2; } else // Bottom. { adjustedBounds.Y = adjustedBounds.Bottom - textHeight; } return adjustedBounds; }
/// <devdoc> /// Returns the Size in logical units of the given text using the given Font, and according to the formatting flags. /// The proposed size is used to create a bounding rectangle as follows: /// - If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by /// the lpRect parameter and extends the base of the rectangle to bound the last line of text. /// - If the largest word is wider than the rectangle, the width is expanded. /// - If the text is less than the width of the rectangle, the width is reduced. /// - If there is only one line of text, DrawText modifies the right side of the rectangle so that /// it bounds the last character in the line. /// If the font is null, the hdc's current font will be used. /// /// Note for vertical fonts (if ever supported): DrawTextEx uses GetTextExtentPoint32 for measuring the text and this /// function has the following limitation (from MSDN): /// - This function assumes that the text is horizontal, that is, that the escapement is always 0. This is true for both /// the horizontal and vertical measurements of the text. The application must convert it explicitly. /// </devdoc> public Size MeasureText(string text, WindowsFont font, Size proposedSize, IntTextFormatFlags flags) { Debug.Assert( ((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!" ); if (string.IsNullOrEmpty(text)) { return Size.Empty; } // // DrawText returns a rectangle useful for aligning, but not guaranteed to encompass all // pixels (its not a FitBlackBox, if the text is italicized, it will overhang on the right.) // So we need to account for this. // IntNativeMethods.DRAWTEXTPARAMS dtparams = null; #if OPTIMIZED_MEASUREMENTDC // use the cache if we've got it if (MeasurementDCInfo.IsMeasurementDC(this.DeviceContext)) { dtparams = MeasurementDCInfo.GetTextMargins(this,font); } #endif if (dtparams == null) { dtparams = GetTextMargins(font); } // // If Width / Height are < 0, we need to make them larger or DrawText will return // an unbounded measurement when we actually trying to make it very narrow. // int minWidth = 1 + dtparams.iLeftMargin + dtparams.iRightMargin; if( proposedSize.Width <= minWidth ) { proposedSize.Width = minWidth; } if( proposedSize.Height <= 0 ) { proposedSize.Height = 1; } IntNativeMethods.RECT rect = IntNativeMethods.RECT.FromXYWH(0, 0, proposedSize.Width, proposedSize.Height); HandleRef hdc = new HandleRef(null, this.dc.Hdc); if (font != null) { this.dc.SelectFont(font); } // If proposedSize.Height >= MaxSize.Height it is assumed bounds needed. If flags contain SingleLine and // VerticalCenter or Bottom options, DrawTextEx does not bind the rectangle to the actual text height since // it assumes the text is to be vertically aligned; we need to clear the VerticalCenter and Bottom flags to // get the actual text bounds. if (proposedSize.Height >= MaxSize.Height && (flags & IntTextFormatFlags.SingleLine) != 0) { // Clear vertical-alignment flags. flags &= ~(IntTextFormatFlags.Bottom | IntTextFormatFlags.VerticalCenter); } if (proposedSize.Width == MaxSize.Width) { // PERF: No constraining width means no word break. // in this case, we dont care about word wrapping - there should be enough room to fit it all flags &= ~(IntTextFormatFlags.WordBreak); } flags |= IntTextFormatFlags.CalculateRectangle; IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * if( hOldFont != IntPtr.Zero ) { this.dc.SelectObject(hOldFont); } */ return rect.Size; }
public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (!string.IsNullOrEmpty(text) && (foreColor != Color.Transparent)) { HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); if (this.dc.TextAlignment != DeviceContextTextAlignment.Top) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Top); } if (!foreColor.IsEmpty && (foreColor != this.dc.TextColor)) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newMode = (backColor.IsEmpty || (backColor == Color.Transparent)) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newMode) { this.dc.SetBackgroundMode(newMode); } if ((newMode != DeviceContextBackgroundMode.Transparent) && (backColor != this.dc.BackgroundColor)) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS textMargins = this.GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, textMargins); if (bounds.Width == MaxSize.Width) { bounds.Width -= bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height -= bounds.Y; } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int)flags, textMargins); } }
private void DrawGroupBox(PaintEventArgs e) { Graphics graphics = e.Graphics; Rectangle textRectangle = ClientRectangle; // Max text bounding box passed to drawing methods to support RTL. int textOffset = 8; // Offset from the left bound. Color backColor = DisabledColor; Pen light = new Pen(ControlPaint.Light(backColor, 1.0f)); Pen dark = new Pen(ControlPaint.Dark(backColor, 0f)); Size textSize; textRectangle.X += textOffset; textRectangle.Width -= 2 * textOffset; try { if (UseCompatibleTextRendering) { using (Brush textBrush = new SolidBrush(ForeColor)){ using (StringFormat format = new StringFormat()){ format.HotkeyPrefix = ShowKeyboardCues ? System.Drawing.Text.HotkeyPrefix.Show : System.Drawing.Text.HotkeyPrefix.Hide; // Adjust string format for Rtl controls if (RightToLeft == RightToLeft.Yes) { format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } textSize = Size.Ceiling(graphics.MeasureString(Text, Font, textRectangle.Width, format)); if (Enabled) { graphics.DrawString(Text, Font, textBrush, textRectangle, format); } else { ControlPaint.DrawStringDisabled(graphics, Text, Font, backColor, textRectangle, format); } } } } else { using (WindowsGraphics wg = WindowsGraphics.FromGraphics(graphics)){ IntTextFormatFlags flags = IntTextFormatFlags.WordBreak | IntTextFormatFlags.TextBoxControl; if (!ShowKeyboardCues) { flags |= IntTextFormatFlags.HidePrefix; } if (RightToLeft == RightToLeft.Yes) { flags |= IntTextFormatFlags.RightToLeft; flags |= IntTextFormatFlags.Right; } using (WindowsFont wfont = WindowsGraphicsCacheManager.GetWindowsFont(this.Font)) { textSize = wg.MeasureText(Text, wfont, new Size(textRectangle.Width, int.MaxValue), flags); if (Enabled) { wg.DrawText(Text, wfont, textRectangle, ForeColor, flags); } else { ControlPaint.DrawStringDisabled(wg, Text, Font, backColor, textRectangle, ((TextFormatFlags)flags)); } } } } int textLeft = textOffset; // Left side of binding box (independent on RTL). if (RightToLeft == RightToLeft.Yes) { textLeft += textRectangle.Width - textSize.Width; } // Math.Min to assure we paint at least a small line. int textRight = Math.Min(textLeft + textSize.Width, Width - 6); int boxTop = FontHeight / 2; if (SystemInformation.HighContrast && AccessibilityImprovements.Level1) { Color boxColor; if (Enabled) { boxColor = ForeColor; } else { boxColor = SystemColors.GrayText; } bool needToDispose = !boxColor.IsSystemColor; Pen boxPen = null; try { if (needToDispose) { boxPen = new Pen(boxColor); } else { boxPen = SystemPens.FromSystemColor(boxColor); } // left graphics.DrawLine(boxPen, 0, boxTop, 0, Height); //bottom graphics.DrawLine(boxPen, 0, Height - 1, Width, Height - 1); //top-left graphics.DrawLine(boxPen, 0, boxTop, textLeft, boxTop); //top-right graphics.DrawLine(boxPen, textRight, boxTop, Width - 1, boxTop); //right graphics.DrawLine(boxPen, Width - 1, boxTop, Width - 1, Height - 1); } finally { if (needToDispose && boxPen != null) { boxPen.Dispose(); } } } else { // left graphics.DrawLine(light, 1, boxTop, 1, Height - 1); graphics.DrawLine(dark, 0, boxTop, 0, Height - 2); // bottom graphics.DrawLine(light, 0, Height - 1, Width, Height - 1); graphics.DrawLine(dark, 0, Height - 2, Width - 1, Height - 2); // top-left graphics.DrawLine(dark, 0, boxTop - 1, textLeft, boxTop - 1); graphics.DrawLine(light, 1, boxTop, textLeft, boxTop); // top-right graphics.DrawLine(dark, textRight, boxTop - 1, Width - 2, boxTop - 1); graphics.DrawLine(light, textRight, boxTop, Width - 1, boxTop); // right graphics.DrawLine(light, Width - 1, boxTop - 1, Width - 1, Height - 1); graphics.DrawLine(dark, Width - 2, boxTop, Width - 2, Height - 2); } } finally { light.Dispose(); dark.Dispose(); } }
/// <devdoc> /// <para> /// The GDI DrawText does not do multiline alignment when IntTextFormatFlags.SingleLine is not set. This /// adjustment is to workaround that limitation. We don't want to duplicate SelectObject calls here, /// so put your Font in the dc before calling this. /// /// AdjustForVerticalAlignment is only used when the text is multiline and it fits inside the bounds passed in. /// In that case we want the horizontal center of the multiline text to be at the horizontal center of the bounds. /// /// If the text is multiline and it does not fit inside the bounds passed in, then return the bounds that were passed in. /// This way we paint the top of the text at the top of the bounds passed in. /// </para> /// </devdoc> public static Rectangle AdjustForVerticalAlignment(HandleRef hdc, string text, Rectangle bounds, IntTextFormatFlags flags, IntNativeMethods.DRAWTEXTPARAMS dtparams) { Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!"); // Ok if any Top (Cannot test IntTextFormatFlags.Top because it is 0), single line text or measuring text. bool isTop = (flags & IntTextFormatFlags.Bottom) == 0 && (flags & IntTextFormatFlags.VerticalCenter) == 0; if (isTop || ((flags & IntTextFormatFlags.SingleLine) != 0) || ((flags & IntTextFormatFlags.CalculateRectangle) != 0)) { return(bounds); } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); // Get the text bounds. flags |= IntTextFormatFlags.CalculateRectangle; int textHeight = IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); // if the text does not fit inside the bounds then return the bounds that were passed in if (textHeight > bounds.Height) { return(bounds); } Rectangle adjustedBounds = bounds; if ((flags & IntTextFormatFlags.VerticalCenter) != 0) // Middle { adjustedBounds.Y = adjustedBounds.Top + adjustedBounds.Height / 2 - textHeight / 2; } else // Bottom. { adjustedBounds.Y = adjustedBounds.Bottom - textHeight; } return(adjustedBounds); }
/// <devdoc> /// Draws the text in the given bounds, using the given Font, foreColor and backColor, and according to the specified /// TextFormatFlags flags. /// If font is null, the font currently selected in the hdc is used. /// If foreColor and/or backColor are Color.Empty, the hdc current text and/or background color are used. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent) { return; } Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!"); Debug.Assert((flags & IntTextFormatFlags.CalculateRectangle) == 0, "CalculateRectangle flag is set, text won't be drawn"); HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); // DrawText requires default text alignment. if (this.dc.TextAlignment != DeviceContextTextAlignment.Default) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Default); } // color empty means use the one currently selected in the dc. if (!foreColor.IsEmpty && foreColor != this.dc.TextColor) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newBackGndMode) { this.dc.SetBackgroundMode(newBackGndMode); } if (newBackGndMode != DeviceContextBackgroundMode.Transparent && backColor != this.dc.BackgroundColor) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS dtparams = GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, dtparams); // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation. if (bounds.Width == MaxSize.Width) { bounds.Width = bounds.Width - bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height = bounds.Height - bounds.Y; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * * if (hOldFont != IntPtr.Zero) * { * IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef( null, hOldFont)); * } * * if( foreColor != textColor ) * { * this.dc.SetTextColor(textColor); * } * * if( backColor != bkColor ) * { * this.dc.SetBackgroundColor(bkColor); * } * * if( bckMode != newMode ) * { * this.dc.SetBackgroundMode(bckMode); * } * * if( align != DeviceContextTextAlignment.Default ) * { * // Default text alignment required by DrewText. * this.dc.SetTextAlignment(align); * } */ }
private void DrawGroupBox(PaintEventArgs e) { Graphics graphics = e.Graphics; Rectangle clientRectangle = base.ClientRectangle; int num = 8; Color disabledColor = SystemColors.Control; // base.DisabledColor; //Pen pen = new Pen(ControlPaint.Light(disabledColor, 1f)); Pen pen = new Pen(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black : Shared.ControlBorderBackColor, 0.3f); //Pen pen2 = new Pen(ControlPaint.Dark(disabledColor, 0f)); Pen pen2 = new Pen(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black : Shared.ControlBorderBackColor, 0f); clientRectangle.X += num; clientRectangle.Width -= 2 * num; try { Size size; int num2; //if (this.UseCompatibleTextRendering) //{ // using (Brush brush = new SolidBrush(this.ForeColor)) // { // using (StringFormat format = new StringFormat()) // { // format.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide; // if (this.RightToLeft == RightToLeft.Yes) // { // format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; // } // size = Size.Ceiling(graphics.MeasureString(this.Text, this.Font, clientRectangle.Width, format)); // if (base.Enabled) // { // graphics.DrawString(this.Text, this.Font, brush, clientRectangle, format); // } // else // { // ControlPaint.DrawStringDisabled(graphics, this.Text, this.Font, disabledColor, clientRectangle, format); // } // } // goto Label_01E7; // } //} IntTextFormatFlags flags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak; if (!this.ShowKeyboardCues) { flags |= IntTextFormatFlags.HidePrefix; } if (this.RightToLeft == RightToLeft.Yes) { flags |= IntTextFormatFlags.RightToLeft; flags |= IntTextFormatFlags.Right; } Graphics graphics2 = e.Graphics; size = graphics2.MeasureString(this.Text, this.Font).ToSize(); if (base.Enabled) { // graphics2.DrawString(this.Text, this.Font, new SolidBrush(Shared.ControlBorderBackColor == SystemColors.Control ? Color.Black : Shared.ControlBorderBackColor), 8, 0); graphics2.DrawString(this.Text, this.Font, new SolidBrush(Shared.FontColor), 8, 0); } else { ControlPaint.DrawStringDisabled(graphics2, this.Text, this.Font, disabledColor, clientRectangle, (TextFormatFlags)flags); } Label_01E7: num2 = num; if (this.RightToLeft == RightToLeft.Yes) { num2 += size.Width; } int num3 = Math.Min((int)(num2 + size.Width), (int)(base.Width - 6)); int num4 = base.FontHeight / 2; graphics.DrawLine(pen, 1, num4, 1, base.Height - 1); //graphics.DrawLine(pen2, 0, num4, 0, base.Height - 2); graphics.DrawLine(pen, 1, base.Height - 1, base.Width - 1, base.Height - 1); //graphics.DrawLine(pen2, 0, base.Height - 2, base.Width - 1, base.Height - 2); graphics.DrawLine(pen2, 1, num4 - 1, num2 - 1, num4 - 1); ////graphics.DrawLine(pen, 1, num4, num2, num4); graphics.DrawLine(pen2, num3, num4 - 1, base.Width - 2, num4 - 1); ////graphics.DrawLine(pen, num3, num4, base.Width - 1, num4); graphics.DrawLine(pen, (int)(base.Width - 1), (int)(num4 - 1), (int)(base.Width - 1), (int)(base.Height - 1)); ////graphics.DrawLine(pen2, base.Width - 2, num4, base.Width - 2, base.Height - 2); } finally { pen.Dispose(); pen2.Dispose(); } }
public Size MeasureText(string text, WindowsFont font, Size proposedSize, IntTextFormatFlags flags) { if (string.IsNullOrEmpty(text)) { return Size.Empty; } IntNativeMethods.DRAWTEXTPARAMS lpDTParams = null; if (MeasurementDCInfo.IsMeasurementDC(this.DeviceContext)) { lpDTParams = MeasurementDCInfo.GetTextMargins(this, font); } if (lpDTParams == null) { lpDTParams = this.GetTextMargins(font); } int num = (1 + lpDTParams.iLeftMargin) + lpDTParams.iRightMargin; if (proposedSize.Width <= num) { proposedSize.Width = num; } if (proposedSize.Height <= 0) { proposedSize.Height = 1; } IntNativeMethods.RECT lpRect = IntNativeMethods.RECT.FromXYWH(0, 0, proposedSize.Width, proposedSize.Height); HandleRef hDC = new HandleRef(null, this.dc.Hdc); if (font != null) { this.dc.SelectFont(font); } if ((proposedSize.Height >= MaxSize.Height) && ((flags & IntTextFormatFlags.SingleLine) != IntTextFormatFlags.Default)) { flags &= ~(IntTextFormatFlags.Bottom | IntTextFormatFlags.VerticalCenter); } if (proposedSize.Width == MaxSize.Width) { flags &= ~IntTextFormatFlags.WordBreak; } flags |= IntTextFormatFlags.CalculateRectangle; IntUnsafeNativeMethods.DrawTextEx(hDC, text, ref lpRect, (int) flags, lpDTParams); return lpRect.Size; }
public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (!string.IsNullOrEmpty(text) && (foreColor != Color.Transparent)) { HandleRef hdc = new HandleRef(this.dc, this.dc.Hdc); if (this.dc.TextAlignment != DeviceContextTextAlignment.Top) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Top); } if (!foreColor.IsEmpty && (foreColor != this.dc.TextColor)) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newMode = (backColor.IsEmpty || (backColor == Color.Transparent)) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if (this.dc.BackgroundMode != newMode) { this.dc.SetBackgroundMode(newMode); } if ((newMode != DeviceContextBackgroundMode.Transparent) && (backColor != this.dc.BackgroundColor)) { this.dc.SetBackgroundColor(backColor); } IntNativeMethods.DRAWTEXTPARAMS textMargins = this.GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, textMargins); if (bounds.Width == MaxSize.Width) { bounds.Width -= bounds.X; } if (bounds.Height == MaxSize.Height) { bounds.Height -= bounds.Y; } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int) flags, textMargins); } }
public void DrawText(string text, WindowsFont font, Point pt, Color foreColor, Color backColor, IntTextFormatFlags flags) { Rectangle bounds = new Rectangle(pt.X, pt.Y, 0x7fffffff, 0x7fffffff); this.DrawText(text, font, bounds, foreColor, backColor, flags); }
public static Rectangle AdjustForVerticalAlignment(HandleRef hdc, string text, Rectangle bounds, IntTextFormatFlags flags, IntNativeMethods.DRAWTEXTPARAMS dtparams) { if (((((flags & IntTextFormatFlags.Bottom) == IntTextFormatFlags.Default) && ((flags & IntTextFormatFlags.VerticalCenter) == IntTextFormatFlags.Default)) || ((flags & IntTextFormatFlags.SingleLine) != IntTextFormatFlags.Default)) || ((flags & IntTextFormatFlags.CalculateRectangle) != IntTextFormatFlags.Default)) { return(bounds); } IntNativeMethods.RECT lpRect = new IntNativeMethods.RECT(bounds); flags |= IntTextFormatFlags.CalculateRectangle; int num = IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref lpRect, (int)flags, dtparams); if (num > bounds.Height) { return(bounds); } Rectangle rectangle = bounds; if ((flags & IntTextFormatFlags.VerticalCenter) != IntTextFormatFlags.Default) { rectangle.Y = (rectangle.Top + (rectangle.Height / 2)) - (num / 2); return(rectangle); } rectangle.Y = rectangle.Bottom - num; return(rectangle); }
/// <devdoc> /// Draws the text at the specified point, using the given Font, foreColor and backColor, and according /// to the specified flags. /// </devdoc> public void DrawText(string text, WindowsFont font, Point pt, Color foreColor, Color backColor, IntTextFormatFlags flags) { Rectangle bounds = new Rectangle(pt.X, pt.Y, int.MaxValue, int.MaxValue); DrawText(text, font, bounds, foreColor, backColor, flags); }
/// <devdoc> /// Draws the text in the given bounds, using the given Font and foreColor, and according to the specified flags. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color color, IntTextFormatFlags flags) { DrawText(text, font, bounds, color, Color.Empty, flags); }
/// <devdoc> /// Returns the Size in logical units of the given text using the given Font, and according to the formatting flags. /// The proposed size is used to create a bounding rectangle as follows: /// - If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by /// the lpRect parameter and extends the base of the rectangle to bound the last line of text. /// - If the largest word is wider than the rectangle, the width is expanded. /// - If the text is less than the width of the rectangle, the width is reduced. /// - If there is only one line of text, DrawText modifies the right side of the rectangle so that /// it bounds the last character in the line. /// If the font is null, the hdc's current font will be used. /// /// Note for vertical fonts (if ever supported): DrawTextEx uses GetTextExtentPoint32 for measuring the text and this /// function has the following limitation (from MSDN): /// - This function assumes that the text is horizontal, that is, that the escapement is always 0. This is true for both /// the horizontal and vertical measurements of the text. The application must convert it explicitly. /// </devdoc> public Size MeasureText(string text, WindowsFont font, Size proposedSize, IntTextFormatFlags flags) { Debug.Assert(((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!"); if (string.IsNullOrEmpty(text)) { return(Size.Empty); } // // DrawText returns a rectangle useful for aligning, but not guaranteed to encompass all // pixels (its not a FitBlackBox, if the text is italicized, it will overhang on the right.) // So we need to account for this. // IntNativeMethods.DRAWTEXTPARAMS dtparams = null; #if OPTIMIZED_MEASUREMENTDC // use the cache if we've got it if (MeasurementDCInfo.IsMeasurementDC(this.DeviceContext)) { dtparams = MeasurementDCInfo.GetTextMargins(this, font); } #endif if (dtparams == null) { dtparams = GetTextMargins(font); } // // If Width / Height are < 0, we need to make them larger or DrawText will return // an unbounded measurement when we actually trying to make it very narrow. // int minWidth = 1 + dtparams.iLeftMargin + dtparams.iRightMargin; if (proposedSize.Width <= minWidth) { proposedSize.Width = minWidth; } if (proposedSize.Height <= 0) { proposedSize.Height = 1; } IntNativeMethods.RECT rect = IntNativeMethods.RECT.FromXYWH(0, 0, proposedSize.Width, proposedSize.Height); HandleRef hdc = new HandleRef(null, this.dc.Hdc); if (font != null) { this.dc.SelectFont(font); } // If proposedSize.Height >= MaxSize.Height it is assumed bounds needed. If flags contain SingleLine and // VerticalCenter or Bottom options, DrawTextEx does not bind the rectangle to the actual text height since // it assumes the text is to be vertically aligned; we need to clear the VerticalCenter and Bottom flags to // get the actual text bounds. if (proposedSize.Height >= MaxSize.Height && (flags & IntTextFormatFlags.SingleLine) != 0) { // Clear vertical-alignment flags. flags &= ~(IntTextFormatFlags.Bottom | IntTextFormatFlags.VerticalCenter); } if (proposedSize.Width == MaxSize.Width) { // PERF: No constraining width means no word break. // in this case, we dont care about word wrapping - there should be enough room to fit it all flags &= ~(IntTextFormatFlags.WordBreak); } flags |= IntTextFormatFlags.CalculateRectangle; IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int)flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * * if( hOldFont != IntPtr.Zero ) * { * this.dc.SelectObject(hOldFont); * } */ return(rect.Size); }
/// <devdoc> /// Draws the text at the specified point, using the given Font, foreColor and backColor, and according /// to the specified flags. /// </devdoc> public void DrawText(string text, WindowsFont font, Point pt, Color foreColor, Color backColor, IntTextFormatFlags flags) { Rectangle bounds = new Rectangle( pt.X, pt.Y, Int32.MaxValue, Int32.MaxValue ); DrawText( text, font, bounds, foreColor, backColor, flags ); }
/// <devdoc> /// Draws the text in the given bounds, using the given Font and foreColor, and according to the specified flags. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color color, IntTextFormatFlags flags) { DrawText( text, font, bounds, color, Color.Empty, flags ); }
/// <devdoc> /// Draws the text in the given bounds, using the given Font, foreColor and backColor, and according to the specified /// TextFormatFlags flags. /// If font is null, the font currently selected in the hdc is used. /// If foreColor and/or backColor are Color.Empty, the hdc current text and/or background color are used. /// </devdoc> public void DrawText(string text, WindowsFont font, Rectangle bounds, Color foreColor, Color backColor, IntTextFormatFlags flags) { if (string.IsNullOrEmpty(text) || foreColor == Color.Transparent) { return; } Debug.Assert( ((uint)flags & GdiUnsupportedFlagMask) == 0, "Some custom flags were left over and are not GDI compliant!" ); Debug.Assert( (flags & IntTextFormatFlags.CalculateRectangle) == 0, "CalculateRectangle flag is set, text won't be drawn" ); HandleRef hdc = new HandleRef( this.dc, this.dc.Hdc); // DrawText requires default text alignment. if( this.dc.TextAlignment != DeviceContextTextAlignment.Default ) { this.dc.SetTextAlignment(DeviceContextTextAlignment.Default ); } // color empty means use the one currently selected in the dc. if( !foreColor.IsEmpty && foreColor != this.dc.TextColor) { this.dc.SetTextColor(foreColor); } if (font != null) { this.dc.SelectFont(font); } DeviceContextBackgroundMode newBackGndMode = (backColor.IsEmpty || backColor == Color.Transparent) ? DeviceContextBackgroundMode.Transparent : DeviceContextBackgroundMode.Opaque; if( this.dc.BackgroundMode != newBackGndMode ) { this.dc.SetBackgroundMode( newBackGndMode ); } if( newBackGndMode != DeviceContextBackgroundMode.Transparent && backColor != this.dc.BackgroundColor ) { this.dc.SetBackgroundColor( backColor ); } IntNativeMethods.DRAWTEXTPARAMS dtparams = GetTextMargins(font); bounds = AdjustForVerticalAlignment(hdc, text, bounds, flags, dtparams); // Adjust unbounded rect to avoid overflow since Rectangle ctr does not do param validation. if( bounds.Width == MaxSize.Width ) { bounds.Width = bounds.Width - bounds.X; } if( bounds.Height == MaxSize.Height ) { bounds.Height = bounds.Height - bounds.Y; } IntNativeMethods.RECT rect = new IntNativeMethods.RECT(bounds); IntUnsafeNativeMethods.DrawTextEx(hdc, text, ref rect, (int) flags, dtparams); /* No need to restore previous objects into the dc (see comments on top of the class). * if (hOldFont != IntPtr.Zero) { IntUnsafeNativeMethods.SelectObject(hdc, new HandleRef( null, hOldFont)); } if( foreColor != textColor ) { this.dc.SetTextColor(textColor); } if( backColor != bkColor ) { this.dc.SetBackgroundColor(bkColor); } if( bckMode != newMode ) { this.dc.SetBackgroundMode(bckMode); } if( align != DeviceContextTextAlignment.Default ) { // Default text alignment required by DrewText. this.dc.SetTextAlignment(align); } */ }
private void DrawGroupBox(PaintEventArgs e) { Graphics graphics = e.Graphics; Rectangle clientRectangle = base.ClientRectangle; int num = 8; Color disabledColor = base.DisabledColor; Pen pen = new Pen(ControlPaint.Light(disabledColor, 1f)); Pen pen2 = new Pen(ControlPaint.Dark(disabledColor, 0f)); clientRectangle.X += num; clientRectangle.Width -= 2 * num; try { Size size; int num2; if (this.UseCompatibleTextRendering) { using (Brush brush = new SolidBrush(this.ForeColor)) { using (StringFormat format = new StringFormat()) { format.HotkeyPrefix = this.ShowKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide; if (this.RightToLeft == RightToLeft.Yes) { format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } size = Size.Ceiling(graphics.MeasureString(this.Text, this.Font, clientRectangle.Width, format)); if (base.Enabled) { graphics.DrawString(this.Text, this.Font, brush, clientRectangle, format); } else { ControlPaint.DrawStringDisabled(graphics, this.Text, this.Font, disabledColor, clientRectangle, format); } } goto Label_01E7; } } using (WindowsGraphics graphics2 = WindowsGraphics.FromGraphics(graphics)) { IntTextFormatFlags flags = IntTextFormatFlags.TextBoxControl | IntTextFormatFlags.WordBreak; if (!this.ShowKeyboardCues) { flags |= IntTextFormatFlags.HidePrefix; } if (this.RightToLeft == RightToLeft.Yes) { flags |= IntTextFormatFlags.RightToLeft; flags |= IntTextFormatFlags.Right; } using (WindowsFont font = WindowsGraphicsCacheManager.GetWindowsFont(this.Font)) { size = graphics2.MeasureText(this.Text, font, new Size(clientRectangle.Width, 0x7fffffff), flags); if (base.Enabled) { graphics2.DrawText(this.Text, font, clientRectangle, this.ForeColor, flags); } else { ControlPaint.DrawStringDisabled(graphics2, this.Text, this.Font, disabledColor, clientRectangle, (TextFormatFlags)flags); } } } Label_01E7: num2 = num; if (this.RightToLeft == RightToLeft.Yes) { num2 += clientRectangle.Width - size.Width; } int num3 = Math.Min((int)(num2 + size.Width), (int)(base.Width - 6)); int num4 = base.FontHeight / 2; graphics.DrawLine(pen, 1, num4, 1, base.Height - 1); graphics.DrawLine(pen2, 0, num4, 0, base.Height - 2); graphics.DrawLine(pen, 0, base.Height - 1, base.Width, base.Height - 1); graphics.DrawLine(pen2, 0, base.Height - 2, base.Width - 1, base.Height - 2); graphics.DrawLine(pen2, 0, num4 - 1, num2, num4 - 1); graphics.DrawLine(pen, 1, num4, num2, num4); graphics.DrawLine(pen2, num3, num4 - 1, base.Width - 2, num4 - 1); graphics.DrawLine(pen, num3, num4, base.Width - 1, num4); graphics.DrawLine(pen, (int)(base.Width - 1), (int)(num4 - 1), (int)(base.Width - 1), (int)(base.Height - 1)); graphics.DrawLine(pen2, base.Width - 2, num4, base.Width - 2, base.Height - 2); } finally { pen.Dispose(); pen2.Dispose(); } }