private RichText(string s, RichTextParameters parameters, IList parts) { myParameters = parameters; myString = s; myParts = new ArrayList(parts.Count); for (int i = 0; i < parts.Count; i++) { myParts.Add(((RichString)parts[i]).Clone()); } }
/// <summary> /// Creates a new rich text block /// </summary> /// <param name="s">Text content</param> /// <param name="parameters">Parameters to use</param> public RichText(string s, RichTextParameters parameters) { myParameters = parameters; if (s != null && s.Length > 0) { myString = s; myParts.Add(new RichString(0, s.Length, parameters.Style, this)); } }
/// <summary> /// Draws the formatted string on a given graphics /// </summary> /// <param name="hdc">The device context to draw the string in.</param> /// <param name="parameters">Text formatting parameters</param> /// <exception cref="ArgumentNullException"><i>g</i> is null</exception> /// <exception cref="ArgumentNullException"><i>font</i> is null</exception> public int Draw(IntPtr hdc, Rectangle rect, RichTextParameters parameters) { Font hFont = GetParametrizedFont(parameters); RECT rc = new RECT(); rc.left = rect.Left; rc.top = rect.Top; rc.bottom = rect.Bottom; RectangleF bounds; IntPtr oldFont = Win32Declarations.SelectObject(hdc, ourFontCache.GetHFont(hFont)); int oldColor = Win32Declarations.SetTextColor(hdc, Win32Declarations.ColorToRGB(myStyle.ForegroundColor)); int oldBkColor = Win32Declarations.SetBkColor(hdc, Win32Declarations.ColorToRGB(myStyle.BackgroundColor)); BackgroundMode oldBkMode = Win32Declarations.SetBkMode(hdc, myStyle.BackgroundColor == Color.Transparent ? BackgroundMode.TRANSPARENT : BackgroundMode.OPAQUE); Win32Declarations.DrawText(hdc, PartText, PartText.Length, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_VCENTER | DrawTextFormatFlags.DT_NOCLIP); if (rc.bottom > rect.Bottom) { rc.bottom = rect.Bottom; } if (rc.right > rect.Right) { rc.right = rect.Right; } bounds = new RectangleF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top); Win32Declarations.DrawText(hdc, PartText, PartText.Length, ref rc, DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_VCENTER | DrawTextFormatFlags.DT_NOCLIP); Win32Declarations.SetBkMode(hdc, oldBkMode); Win32Declarations.SetBkColor(hdc, oldBkColor); Win32Declarations.SetTextColor(hdc, oldColor); Win32Declarations.SelectObject(hdc, oldFont); switch (myStyle.Effect) { case TextStyle.EffectStyle.StrikeOut: StrikeOut(hdc, bounds); break; case TextStyle.EffectStyle.StraightUnderline: UnderlineStraight(hdc, bounds); break; case TextStyle.EffectStyle.WeavyUnderline: UnderlineWeavy(hdc, bounds); break; } return(rc.right - rc.left); }
/// <summary> /// Gets size of the string in the given graphics /// </summary> /// <param name="hdc">The device context to calculate size in</param> /// <param name="parameters">Formatting parameters to use</param> /// <returns>Size of the string when drawn in a given graphics</returns> /// <exception cref="ArgumentNullException"><i>g</i> is null.</exception> public SizeF GetSize(IntPtr hdc, RichTextParameters parameters) { Font hFont = GetParametrizedFont(parameters); RECT rc = new RECT(); rc.left = 0; rc.top = 0; IntPtr oldFont = Win32Declarations.SelectObject(hdc, ourFontCache.GetHFont(hFont)); Win32Declarations.DrawText(hdc, PartText, PartText.Length, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX); Win32Declarations.SelectObject(hdc, oldFont); return(new SizeF(rc.right - rc.left, rc.bottom - rc.top)); }
public int GetSymbolByOffset(int x, RichTextParameters parameters, IntPtr hdc) { if (x < 0) { return(-1); } Font hFont = GetParametrizedFont(parameters); RECT rc = new RECT(); rc.left = 0; rc.top = 0; int currentX = 0; IntPtr oldFont = Win32Declarations.SelectObject(hdc, ourFontCache.GetHFont(hFont)); try { for (int i = 0; i < PartText.Length; i++) { Win32Declarations.DrawText(hdc, PartText.Substring(i, 1), 1, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_SINGLELINE | DrawTextFormatFlags.DT_NOPREFIX | DrawTextFormatFlags.DT_NOCLIP); currentX += rc.right - rc.left; if (currentX > x) { return(i); } } return(-1); } finally { Win32Declarations.SelectObject(hdc, oldFont); } }
/// <summary> /// Returns parametrized font /// </summary> /// <param name="parameters">Formatting parameters to use</param> private Font GetParametrizedFont(RichTextParameters parameters) { return(ourFontCache.GetFont(parameters.Font, myStyle.FontStyle)); }