public void Paint(Graphics g) { if (Visibility != Visibility.Visible || string.IsNullOrEmpty(Text)) { return; } IntPtr hdcTemp = IntPtr.Zero; IntPtr oldFont = IntPtr.Zero; IntPtr currentFont = IntPtr.Zero; try { hdcTemp = g.GetHdc(); if (hdcTemp != IntPtr.Zero) { currentFont = Font.ToFont().ToHfont(); oldFont = CoreDll.SelectObject(hdcTemp, currentFont); var rect = new Rect { Left = (int)Location.X, Top = (int)Location.Y, Right = (int)(Location.X + Size.Width), Bottom = (int)(Location.Y + Size.Height), }; var color = (Highlight ? HighlightColor : ForegroundColor).ToColor(); CoreDll.SetTextColor(hdcTemp, color.R | (color.G << 8) | (color.B << 16)); CoreDll.SetBkMode(hdcTemp, 1); var flags = CoreDll.DT_END_ELLIPSIS | CoreDll.DT_NOPREFIX; if (Lines != 1) { flags += CoreDll.DT_WORDBREAK; } CoreDll.DrawText(hdcTemp, Text, Text.Length, ref rect, flags); } } finally { if (oldFont != IntPtr.Zero) { CoreDll.SelectObject(hdcTemp, oldFont); } if (hdcTemp != IntPtr.Zero) { g.ReleaseHdc(hdcTemp); } if (currentFont != IntPtr.Zero) { CoreDll.DeleteObject(currentFont); } } }
public void FitText() { if (!_hasMeasured) { return; } const string ellipsisChars = "... "; var constraints = Size.ToSize(); Size s = CoreDll.MeasureString(" " + _title + " ", Font.ToFont(), constraints, false, true) + _margins; // control is large enough to display the whole text if (s.Width <= Width) { return; } int len = 0; int seg = _title.Length; string fit = string.Empty; // find the longest string that fits into the control boundaries using bisection method while (seg > 1) { seg -= seg / 2; int left = len + seg; int right = _title.Length; if (left > right) { continue; } // build and measure a candidate string with ellipsis string tst = " " + _title.Substring(0, left).TrimEnd() + ellipsisChars; s = CoreDll.MeasureString(tst, Font.ToFont(), constraints, false, true) + _margins; // candidate string fits into control boundaries, // try a longer string // stop when seg <= 1 if (s.Width <= Width) { len += seg; fit = tst; } } base.Text = len == 0 ? ellipsisChars : fit; }
public Size Measure(Size constraints) { var measure = CoreDll.MeasureString(" " + _title + " ", Font.ToFont(), constraints, false, true) + _margins; return(new Size(measure.Width, Math.Max(measure.Height, 33 * CompactFactory.Instance.DpiScale))); }
protected override double GetLineHeight(Font font) { return(CoreDll.MeasureString("Wq", font.ToFont(), new Size(1000, 1000), false, true).Height); }
public Size Measure(Size constraints) { return(CoreDll.MeasureString(Text, Font.ToFont(), constraints, Lines != 1, false)); }