private static PARAFORMAT HandleTextAndWhitespace(RichTextBox rtb, PARAFORMAT pf, CHARFORMAT cf, string strData) { bool bNewParagraph = (strData.IndexOf("\r\n", 0) >= 0) || (strData.IndexOf("\n", 0) >= 0); if (strData.Length > 0) { // now, add text to control int nStartCache = rtb.SelectionStart; rtb.SelectedText = strData; rtb.Select(nStartCache, strData.Length); // apply format rtb.SetParaFormat(pf); rtb.SetCharFormat(cf); } // reposition to final rtb.Select(rtb.TextLength + 1, 0); // new paragraph requires to reset alignment if (bNewParagraph) { pf.dwMask = PFM.ALIGNMENT | PFM.NUMBERING; pf.wAlignment = PFA.LEFT; pf.wNumbering = 0; } return pf; }
private static extern int SendMessage( HandleRef hWnd, int msg, int wParam, ref CHARFORMAT lp );
private static void applyLinkStyle(RichTextBox rtb, List<KeyValuePair<int, int>> links) { // apply links style CHARFORMAT ncf = new CHARFORMAT(CFM.LINK, CFE.LINK); foreach (var pair in links) { rtb.Select(pair.Key, pair.Value); rtb.SetCharFormat(ncf); } }
private static CHARFORMAT HandleFontElement(XmlReader reader, CHARFORMAT cf, Stack<CHARFORMAT> scf) { scf.Push(cf); ; string strFont = cf.szFaceName; int crFont = cf.crTextColor; int yHeight = cf.yHeight; while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "face": cf.dwMask |= CFM.FACE; strFont = reader.Value; break; case "size": cf.dwMask |= CFM.SIZE; yHeight = int.Parse(reader.Value); yHeight *= (20*5); break; case "color": cf.dwMask |= CFM.COLOR; string text = reader.Value; if (text.StartsWith("#")) { string strCr = text.Substring(1); int nCr = Convert.ToInt32(strCr, 16); Color color = Color.FromArgb(nCr); crFont = GetCOLORREF(color); } else if (!int.TryParse(text, out crFont)) { Color color = Color.FromName(text); crFont = GetCOLORREF(color); } break; } } reader.MoveToElement(); cf.szFaceName = strFont; cf.crTextColor = crFont; cf.yHeight = yHeight; cf.dwEffects &= ~CFE.AUTOCOLOR; return cf; }
internal static extern int SendMessage(HandleRef hWnd, UInt32 msg, IntPtr wParam, ref CHARFORMAT lp);
private static bool ProcessSignificantWhitespace(RichTextBox rtb, ref CHARFORMAT cf, ref PARAFORMAT pf, string strData) { bool bNewParagraph = (strData.IndexOf("\r\n", 0) >= 0) || (strData.IndexOf("\n", 0) >= 0); if (strData.Length > 0) { // now, add text to control int nStartCache = rtb.SelectionStart; rtb.SelectedText = strData; rtb.Select(nStartCache, strData.Length); // apply format rtb.SetParaFormat(pf); rtb.SetCharFormat(cf); } // reposition to final rtb.Select(rtb.TextLength + 1, 0); return bNewParagraph; }
internal static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, ref CHARFORMAT lp);
public static void SetDefaultCharFormat(this RichTextBox rtb, CFM mask, CFE effects) { CHARFORMAT cf = new CHARFORMAT(mask, effects); rtb.SetDefaultCharFormat(cf); }
private void ApplyStyle(uint style, bool value) { CHARFORMAT fmt = new CHARFORMAT(); fmt.cbSize = Marshal.SizeOf(fmt); fmt.dwMask = style; if (value) fmt.dwEffects = style; SetCharFormatMessage(ref fmt); }
public static void SetXHTMLText(this RichTextBox rtb, string xhtmlText) { if (EnvUtils.IsMonoRuntime()) { SetXHTMLTextAsPlainText(rtb, xhtmlText); return; } rtb.Clear(); RTFCurrentState cs = new RTFCurrentState(); var handleRef = new HandleRef(rtb, rtb.Handle); cs.cf = GetDefaultCharFormat(handleRef); // to apply character formatting cs.pf = GetDefaultParaFormat(handleRef); // to apply paragraph formatting IntPtr oldMask = BeginUpdate(handleRef); SetHideSelectionInternal(handleRef, true); XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.CheckCharacters = false; try { using (StringReader stringreader = new StringReader(EscapeNonXMLChars(xhtmlText))) { XmlReader reader = XmlReader.Create(stringreader, settings); while (reader.Read()) ProcessNode(rtb, handleRef, reader, cs); } } catch (System.Xml.XmlException ex) { Debug.WriteLine(ex.Message); } // apply links style CHARFORMAT ncf = new CHARFORMAT(CFM.LINK, CFE.LINK); ncf.cbSize = Marshal.SizeOf(ncf); foreach (var pair in cs.links) { rtb.Select(pair.Key, pair.Value); SetCharFormat(handleRef, ncf); } SetHideSelectionInternal(handleRef, false); // reposition to first rtb.Select(0, 0); EndUpdate(handleRef, oldMask); rtb.Invalidate(); }
private void SetCharFormatMessage(ref CHARFORMAT char_format) { SendMessage(new HandleRef(this, Handle), EM_SETCHARFORMAT, SCF_SELECTION, ref char_format); }
private static void ProcessXmlEndElement(RichTextBox rtb, Stack<CHARFORMAT> scf, Stack<PARAFORMAT> spf, List<KeyValuePair<int, int>> links, ref CHARFORMAT cf, ref PARAFORMAT pf, ref int hyperlinkStart, string hyperlink, XmlReader reader) { switch (reader.Name) { case "b": cf.dwEffects &= ~CFE.BOLD; cf.wWeight = FW.NORMAL; break; case "i": cf.dwEffects &= ~CFE.ITALIC; break; case "u": cf.dwEffects &= ~CFE.UNDERLINE; break; case "s": cf.dwEffects &= ~CFE.STRIKEOUT; break; case "sup": cf.dwEffects &= ~CFE.SUPERSCRIPT; break; case "sub": cf.dwEffects &= ~CFE.SUBSCRIPT; break; case "a": int length = rtb.TextLength - hyperlinkStart; if (hyperlink != null) { rtb.Select(hyperlinkStart, length); if (hyperlink != rtb.SelectedText) { string rtfText = rtb.SelectedRtf; int idx = rtfText.LastIndexOf('}'); if (idx != -1) { string head = rtfText.Substring(0, idx); string tail = rtfText.Substring(idx); rtb.SelectedRtf = head + @"\v #" + hyperlink + @"\v0" + tail; length = rtb.TextLength - hyperlinkStart; } } // reposition to final rtb.Select(rtb.TextLength + 1, 0); } links.Add(new KeyValuePair<int, int>(hyperlinkStart, length)); hyperlinkStart = -1; break; case "p": pf = spf.Pop(); break; case "li": pf = spf.Pop(); break; case "font": cf = scf.Pop(); break; } }
private static void ProcessXmlElement(RichTextBox rtb, Stack<CHARFORMAT> scf, Stack<PARAFORMAT> spf, ref CHARFORMAT cf, ref PARAFORMAT pf, ref int hyperlinkStart, ref string hyperlink, XmlReader reader) { switch (reader.Name.ToLower()) { case "b": cf.dwMask |= CFM.WEIGHT | CFM.BOLD; cf.dwEffects |= CFE.BOLD; cf.wWeight = FW.BOLD; break; case "i": cf.dwMask |= CFM.ITALIC; cf.dwEffects |= CFE.ITALIC; break; case "u": cf.dwMask |= CFM.UNDERLINE | CFM.UNDERLINETYPE; cf.dwEffects |= CFE.UNDERLINE; cf.bUnderlineType = CFU.UNDERLINE; break; case "s": cf.dwMask |= CFM.STRIKEOUT; cf.dwEffects |= CFE.STRIKEOUT; break; case "sup": cf.dwMask |= CFM.SUPERSCRIPT; cf.dwEffects |= CFE.SUPERSCRIPT; break; case "sub": cf.dwMask |= CFM.SUBSCRIPT; cf.dwEffects |= CFE.SUBSCRIPT; break; case "a": hyperlinkStart = rtb.TextLength; hyperlink = null; while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "href": hyperlink = reader.Value; break; } } reader.MoveToElement(); break; case "p": spf.Push(pf); while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "align": if (reader.Value == "left") { pf.dwMask |= PFM.ALIGNMENT; pf.wAlignment = PFA.LEFT; } else if (reader.Value == "right") { pf.dwMask |= PFM.ALIGNMENT; pf.wAlignment = PFA.RIGHT; } else if (reader.Value == "center") { pf.dwMask |= PFM.ALIGNMENT; pf.wAlignment = PFA.CENTER; } break; } } reader.MoveToElement(); break; case "li": spf.Push(pf); if (pf.wNumbering != PFN.BULLET) { pf.dwMask |= PFM.NUMBERING; pf.wNumbering = PFN.BULLET; } break; case "font": scf.Push(cf); string strFont = cf.szFaceName; int crFont = cf.crTextColor; int yHeight = cf.yHeight; while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "face": cf.dwMask |= CFM.FACE; strFont = reader.Value; break; case "size": cf.dwMask |= CFM.SIZE; yHeight = int.Parse(reader.Value); yHeight *= (20 * 5); break; case "color": cf.dwMask |= CFM.COLOR; string text = reader.Value; if (text.StartsWith("#")) { string strCr = text.Substring(1); int nCr = Convert.ToInt32(strCr, 16); Color color = Color.FromArgb(nCr); crFont = GetCOLORREF(color); } else if (!int.TryParse(text, out crFont)) { Color color = Color.FromName(text); crFont = GetCOLORREF(color); } break; } } reader.MoveToElement(); cf.szFaceName = strFont; cf.crTextColor = crFont; cf.yHeight = yHeight; cf.dwEffects &= ~CFE.AUTOCOLOR; break; } }
public static CHARFORMAT GetDefaultCharFormat(this RichTextBox rtb) { CHARFORMAT cf = new CHARFORMAT(); cf.cbSize = Marshal.SizeOf(cf); // Get the alignment. NativeMethods.SendMessage(rtb, NativeMethods.EM_GETCHARFORMAT, NativeMethods.SCF_ALL, ref cf); return cf; }
private static CHARFORMAT GetDefaultCharFormat(HandleRef handleRef) { CHARFORMAT cf = new CHARFORMAT(); cf.cbSize = Marshal.SizeOf(cf); // Get the alignment. NativeMethods.SendMessage(handleRef, NativeMethods.EM_GETCHARFORMAT, (IntPtr)NativeMethods.SCF_ALL, ref cf); return cf; }
public static void SetDefaultCharFormat(this RichTextBox rtb, CHARFORMAT value) { CHARFORMAT cf = value; cf.cbSize = Marshal.SizeOf(cf); // Set the alignment. NativeMethods.SendMessage(rtb, NativeMethods.EM_SETCHARFORMAT, NativeMethods.SCF_ALL, ref cf); }
private static void SetDefaultCharFormat(HandleRef handleRef, CHARFORMAT value) { Debug.Assert(value.cbSize == Marshal.SizeOf(value)); // Set the alignment. NativeMethods.SendMessage(handleRef, NativeMethods.EM_SETCHARFORMAT, (IntPtr)NativeMethods.SCF_ALL, ref value); }
public static void SetXHTMLText(this RichTextBox rtb, string xhtmlText) { rtb.Clear(); Stack<CHARFORMAT> scf = new Stack<CHARFORMAT>(); Stack<PARAFORMAT> spf = new Stack<PARAFORMAT>(); List<KeyValuePair<int, int>> links = new List<KeyValuePair<int, int>>(); CHARFORMAT cf = rtb.GetDefaultCharFormat(); // to apply character formatting PARAFORMAT pf = rtb.GetDefaultParaFormat(); // to apply paragraph formatting rtb.HideSelection = true; int oldMask = rtb.BeginUpdate(); int hyperlinkStart = -1; string hyperlink = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; try { using (XmlReader reader = XmlReader.Create(new StringReader(xhtmlText), settings)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: switch (reader.Name.ToLower()) { case "b": cf.dwMask |= CFM.WEIGHT | CFM.BOLD; cf.dwEffects |= CFE.BOLD; cf.wWeight = FW.BOLD; break; case "i": cf.dwMask |= CFM.ITALIC; cf.dwEffects |= CFE.ITALIC; break; case "u": cf.dwMask |= CFM.UNDERLINE | CFM.UNDERLINETYPE; cf.dwEffects |= CFE.UNDERLINE; cf.bUnderlineType = CFU.UNDERLINE; break; case "s": cf.dwMask |= CFM.STRIKEOUT; cf.dwEffects |= CFE.STRIKEOUT; break; case "sup": cf.dwMask |= CFM.SUPERSCRIPT; cf.dwEffects |= CFE.SUPERSCRIPT; break; case "sub": cf.dwMask |= CFM.SUBSCRIPT; cf.dwEffects |= CFE.SUBSCRIPT; break; case "a": hyperlinkStart = rtb.TextLength; hyperlink = null; while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "href": hyperlink = reader.Value; break; } } reader.MoveToElement(); break; case "p": spf.Push(pf); while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "align": if (reader.Value == "left") { pf.dwMask |= PFM.ALIGNMENT; pf.wAlignment = PFA.LEFT; } else if (reader.Value == "right") { pf.dwMask |= PFM.ALIGNMENT; pf.wAlignment = PFA.RIGHT; } else if (reader.Value == "center") { pf.dwMask |= PFM.ALIGNMENT; pf.wAlignment = PFA.CENTER; } break; } } reader.MoveToElement(); break; case "li": spf.Push(pf); if (pf.wNumbering != PFN.BULLET) { pf.dwMask |= PFM.NUMBERING; pf.wNumbering = PFN.BULLET; } break; case "font": scf.Push(cf); string strFont = cf.szFaceName; int crFont = cf.crTextColor; int yHeight = cf.yHeight; while (reader.MoveToNextAttribute()) { switch (reader.Name.ToLower()) { case "face": cf.dwMask |= CFM.FACE; strFont = reader.Value; break; case "size": cf.dwMask |= CFM.SIZE; yHeight = int.Parse(reader.Value); yHeight *= (20 * 5); break; case "color": cf.dwMask |= CFM.COLOR; string text = reader.Value; if (text.StartsWith("#")) { string strCr = text.Substring(1); int nCr = Convert.ToInt32(strCr, 16); Color color = Color.FromArgb(nCr); crFont = GetCOLORREF(color); } else if (!int.TryParse(text, out crFont)) { Color color = Color.FromName(text); crFont = GetCOLORREF(color); } break; } } reader.MoveToElement(); cf.szFaceName = strFont; cf.crTextColor = crFont; cf.yHeight = yHeight; cf.dwEffects &= ~CFE.AUTOCOLOR; break; } break; case XmlNodeType.EndElement: switch (reader.Name) { case "b": cf.dwEffects &= ~CFE.BOLD; cf.wWeight = FW.NORMAL; break; case "i": cf.dwEffects &= ~CFE.ITALIC; break; case "u": cf.dwEffects &= ~CFE.UNDERLINE; break; case "s": cf.dwEffects &= ~CFE.STRIKEOUT; break; case "sup": cf.dwEffects &= ~CFE.SUPERSCRIPT; break; case "sub": cf.dwEffects &= ~CFE.SUBSCRIPT; break; case "a": int length = rtb.TextLength - hyperlinkStart; if (hyperlink != null) { rtb.Select(hyperlinkStart, length); if (hyperlink != rtb.SelectedText) { string rtfText = rtb.SelectedRtf; int idx = rtfText.LastIndexOf('}'); if (idx != -1) { string head = rtfText.Substring(0, idx); string tail = rtfText.Substring(idx); rtb.SelectedRtf = head + @"\v #" + hyperlink + @"\v0" + tail; length = rtb.TextLength - hyperlinkStart; } } // reposition to final rtb.Select(rtb.TextLength + 1, 0); } links.Add(new KeyValuePair<int, int>(hyperlinkStart, length)); hyperlinkStart = -1; break; case "p": pf = spf.Pop(); break; case "li": pf = spf.Pop(); break; case "font": cf = scf.Pop(); break; } break; case XmlNodeType.Text: case XmlNodeType.Whitespace: case XmlNodeType.SignificantWhitespace: string strData = reader.Value; bool bNewParagraph = (strData.IndexOf("\r\n", 0) >= 0) || (strData.IndexOf("\n", 0) >= 0); if (strData.Length > 0) { // now, add text to control int nStartCache = rtb.SelectionStart; rtb.SelectedText = strData; rtb.Select(nStartCache, strData.Length); // apply format rtb.SetParaFormat(pf); rtb.SetCharFormat(cf); } // reposition to final rtb.Select(rtb.TextLength + 1, 0); // new paragraph requires to reset alignment if (bNewParagraph) { pf.dwMask = PFM.ALIGNMENT | PFM.NUMBERING; pf.wAlignment = PFA.LEFT; pf.wNumbering = 0; } break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: break; case XmlNodeType.Comment: break; default: break; } } } } catch (System.Xml.XmlException ex) { Debug.WriteLine(ex.Message); } rtb.HideSelection = false; // apply links style CHARFORMAT ncf = new CHARFORMAT(CFM.LINK, CFE.LINK); foreach (var pair in links) { rtb.Select(pair.Key, pair.Value); rtb.SetCharFormat(ncf); } // reposition to final rtb.Select(rtb.TextLength + 1, 0); rtb.EndUpdate(oldMask); }
public static void SetDefaultCharFormat(this RichTextBox rtb, CHARFORMAT value) { var handleRef = new HandleRef(rtb, rtb.Handle); SetDefaultCharFormat(handleRef, value); }
internal static void SendMessage(RichTextBox rtb, UInt32 msg, int wParam, ref CHARFORMAT pf) { SendMessage(new HandleRef(rtb, rtb.Handle), msg, (IntPtr)wParam, ref pf); }
public static void SetXHTMLText(this RichTextBox rtb, string xhtmlText) { rtb.Clear(); Stack<CHARFORMAT> scf = new Stack<CHARFORMAT>(); Stack<PARAFORMAT> spf = new Stack<PARAFORMAT>(); List<KeyValuePair<int, int>> links = new List<KeyValuePair<int, int>>(); CHARFORMAT cf = rtb.GetDefaultCharFormat(); // to apply character formatting PARAFORMAT pf = rtb.GetDefaultParaFormat(); // to apply paragraph formatting rtb.HideSelection = true; int oldMask = rtb.BeginUpdate(); int hyperlinkStart = -1; string hyperlink = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; try { using (XmlReader reader = XmlReader.Create(new StringReader(xhtmlText), settings)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: ProcessXmlElement(rtb, scf, spf, ref cf, ref pf, ref hyperlinkStart, ref hyperlink, reader); break; case XmlNodeType.EndElement: ProcessXmlEndElement(rtb, scf, spf, links, ref cf, ref pf, ref hyperlinkStart, hyperlink, reader); break; case XmlNodeType.Text: case XmlNodeType.Whitespace: case XmlNodeType.SignificantWhitespace: string strData = reader.Value; bool bNewParagraph = ProcessSignificantWhitespace(rtb, ref cf, ref pf, strData); // new paragraph requires to reset alignment if (bNewParagraph) { pf.dwMask = PFM.ALIGNMENT | PFM.NUMBERING; pf.wAlignment = PFA.LEFT; pf.wNumbering = 0; } break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: break; case XmlNodeType.Comment: break; default: break; } } } } catch (System.Xml.XmlException ex) { Debug.WriteLine(ex.Message); } rtb.HideSelection = false; // apply links style CHARFORMAT ncf = new CHARFORMAT(CFM.LINK, CFE.LINK); foreach (var pair in links) { rtb.Select(pair.Key, pair.Value); rtb.SetCharFormat(ncf); } // reposition to final rtb.Select(rtb.TextLength + 1, 0); rtb.EndUpdate(oldMask); }