private void OnTextChanged(EventArgs e) { DrawControlTexture = !string.IsNullOrEmpty(Text); TextureValid = false; if (AutoSize) { Size = DXFont.MeasureString(null, Text, DrawFormat, ForeColor).Size; if (OutLine && Size != Size.Empty) { Size = new Size(Size.Width + 2, Size.Height + 2); } } if (TextChanged != null) { TextChanged.Invoke(this, e); } }
public override void Init(Device device) { base.Init(device); font = new Microsoft.DirectX.Direct3D.Font(device, fontType); // update dimensions if (dimensions.IsEmpty) { Rectangle strRect = font.MeasureString(null, text, DrawTextFormat.Left, clr); dimensions = new Size(strRect.Width, strRect.Height); } }
/// <summary> /// Override this method to change the way bounds are computed. For example /// this is where you can control how lines are wrapped. /// </summary> public virtual void RecomputeBounds() { if (text != null && (ConstrainWidthToTextWidth || ConstrainHeightToTextHeight)) { currLeftPadding = currRightPadding = currTopPadding = currBottomPadding = 0; // Calculate the padding values in display units float scaleFactor = displayFontSize / DEFAULT_FONT_SIZE; currLeftPadding = (int)(scaleFactor * LEFT_PADDING); currRightPadding = (int)(scaleFactor * RIGHT_PADDING); currTopPadding = (int)(scaleFactor * TOP_PADDING); currBottomPadding = (int)(scaleFactor * BOTTOM_PADDING); // Set the scale factor relative to the current font size scaleFactor = displayFontSize / Font.Size; float textWidth; float textHeight; if (ConstrainWidthToTextWidth) { Rectangle rect = measureFont.MeasureString(null, Text, DrawTextFormat.None, (textBrush as SolidBrush).Color); textWidth = rect.Width * scaleFactor + currLeftPadding + currRightPadding; textHeight = rect.Height * scaleFactor + currTopPadding + currBottomPadding; } else { textWidth = Width; int scaledWidth = (int)((textWidth - (currLeftPadding + currRightPadding)) * (1 / scaleFactor)); float scaledHeight = P3Util.MeasureString(measureFont, null, Text, scaledWidth, (textBrush as SolidBrush).Color).Height; textHeight = scaledHeight * scaleFactor + currTopPadding + currBottomPadding;; } float newWidth = Width; float newHeight = Height; if (ConstrainWidthToTextWidth) { newWidth = textWidth; } if (ConstrainHeightToTextHeight) { newHeight = textHeight; } base.SetBounds(X, Y, newWidth, newHeight); } }