// TODO: remove? /* * private void HighlightRtfWords( WordPtr[] words ) * { * // we use plain text for indexing, so the offsets will not match => * // use token-based highlighting * * _wordsToHighlight = words; * _lastHighlightOffset = 0; * for( _lastHighlightIndex = 0; _lastHighlightIndex < words.Length; _lastHighlightIndex++ ) * { * // if there are too many words to highlight - use async highlighting, in * // order not to lock up the UI for too long * if ( _lastHighlightIndex == 5 ) * { * _wordHighlightTimer.Start(); * break; * } * HighlightNextWord(); * } * } */ private void HighlightNextWord() { if (_wordsToHighlight [_lastHighlightIndex].Section != DocumentSection.BodySection) { return; } int offset = _richTextBox.Find(_wordsToHighlight [_lastHighlightIndex].Text, _lastHighlightOffset, RichTextBoxFinds.WholeWord); if (offset >= 0) { int highlightLength = _wordsToHighlight [_lastHighlightIndex].Text.Length; _lastHighlightOffset = offset + highlightLength; _richTextBox.Select(offset, highlightLength); CHARFORMAT2 fmt = new CHARFORMAT2(); fmt.cbSize = Marshal.SizeOf(fmt); fmt.dwMask = CFM.BACKCOLOR | CFM.COLOR; fmt.crBackColor = ColorTranslator.ToWin32(SystemColors.Highlight); fmt.crTextColor = ColorTranslator.ToWin32(SystemColors.HighlightText); Win32Declarations.SendMessage(_richTextBox.Handle, EditMessage.SETCHARFORMAT, SCF.SELECTION, ref fmt); } }
private void HighlightDescriptionLinks() { _linkTextMap = new Dictionary <string, string>(); foreach (LinkRegex res in Core.ResourceStore.GetAllResources(LinkRegex.ResourceType)) { string regexMatch = res.RegexMatch; string regexReplace = res.RegexReplace; if (String.IsNullOrEmpty(regexMatch) || String.IsNullOrEmpty(regexReplace)) { continue; } Regex rxMatch = new Regex(regexMatch); foreach (Match m in rxMatch.Matches(_edtDescription.Text)) { _edtDescription.Select(m.Index, m.Length); CHARFORMAT2 fmt = new CHARFORMAT2(); fmt.cbSize = Marshal.SizeOf(fmt); fmt.dwMask = CFM.EFFECTS; fmt.dwEffects = (uint)CFM.LINK; SendMessage(_edtDescription.Handle, EditMessage.SETCHARFORMAT, SCF.SELECTION, ref fmt); _linkTextMap [m.Value] = rxMatch.Replace(m.Value, regexReplace); } } _edtDescription.Select(0, 0); }
private void SetColor(Color backColor, Color foreColor) { CHARFORMAT2 charFormat = new CHARFORMAT2(); charFormat.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2)); charFormat.dwMask = NuGenConstants.CFM_BACKCOLOR | NuGenConstants.CFM_COLOR; charFormat.crBackColor = MakeColorRef(backColor.R, backColor.G, backColor.B); charFormat.crTextColor = MakeColorRef(foreColor.R, foreColor.G, foreColor.B); IntPtr lparam = IntPtr.Zero; try { lparam = Marshal.AllocHGlobal(charFormat.cbSize); IntPtr wparam = new IntPtr(NuGenConstants.SCF_SELECTION); Marshal.StructureToPtr(charFormat, lparam, false); SendMessage(Handle, NuGenConstants.EM_SETCHARFORMAT, wparam, lparam); } catch { throw; } finally { if (lparam != IntPtr.Zero) { Marshal.DestroyStructure(lparam, typeof(CHARFORMAT2)); Marshal.FreeHGlobal(lparam); } } }
public static void ClearAllFormatting(this RichTextBox te, Font font) { CHARFORMAT2 fmt = new CHARFORMAT2(); fmt.cbSize = Marshal.SizeOf(fmt); fmt.dwMask = CFM_ALL2; fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR; fmt.szFaceName = font.FontFamily.Name; double size = font.Size; size /= 72; //logical dpi (pixels per inch) size *= 1440.0; //twips per inch fmt.yHeight = (int)size; //165 fmt.yOffset = 0; fmt.crTextColor = 0; fmt.bCharSet = 1; // DEFAULT_CHARSET; fmt.bPitchAndFamily = 0; // DEFAULT_PITCH; fmt.wWeight = 400; // FW_NORMAL; fmt.sSpacing = 0; fmt.crBackColor = 0; //fmt.lcid = ??? fmt.dwMask &= ~CFM_LCID;//don't know how to get this... fmt.dwReserved = 0; fmt.sStyle = 0; fmt.wKerning = 0; fmt.bUnderlineType = 0; fmt.bAnimation = 0; fmt.bRevAuthor = 0; fmt.bReserved1 = 0; SendMessage(te.Handle, EM_SETCHARFORMAT, SCF_ALL, ref fmt); }
public static bool IsUnderlined(this RichTextBox te) { var CharFormat = new CHARFORMAT2(); CharFormat.cbSize = Marshal.SizeOf(CharFormat); SendMessage(te.Handle, EM_GETCHARFORMAT, SCF_SELECTION, ref CharFormat); return((CharFormat.dwMask & CFM_UNDERLINETYPE) != 0 && CharFormat.bUnderlineType == CFU_UNDERLINEWAVE); }
/// <summary> /// Initializes the object to an indeterminate state. /// </summary> public AsyncHighlightState(WordPtr[] words) { Words = words; Fmt = new CHARFORMAT2(); Fmt.cbSize = Marshal.SizeOf(Fmt); Fmt.dwMask = CFM.BACKCOLOR | CFM.COLOR; HashSources = new Hashtable(); }
public static void CreateLink(RichTextBox rtb, int n_begin, int n_length) { rtb.Select(n_begin, n_length); CHARFORMAT2 cf2 = new CHARFORMAT2(); cf2.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2)); cf2.dwMask = CFM_LINK; cf2.dwEffects = CFE_LINK; SendMessageCHARFORMAT(rtb.Handle, EM_SETCHARFORMAT, SCF_SELECTION, ref cf2); rtb.Select(rtb.TextLength, 0); }
public void GetRTHighlight(ref Color back, ref Color fore, Color AlphaBackColor) { CHARFORMAT2 structure = default(CHARFORMAT2); structure.cbSize = Marshal.SizeOf(structure); IntPtr intPtr = Marshal.AllocCoTaskMem(structure.cbSize); Marshal.StructureToPtr(structure, intPtr, fDeleteOld: false); intPtr = (IntPtr)SendMessageToMaster(EM_GETCHARFORMAT, (IntPtr)SCF_SELECTION, intPtr, 3); structure = (CHARFORMAT2)Marshal.PtrToStructure(intPtr, typeof(CHARFORMAT2)); back = ((structure.crBackColor == 0) ? AlphaBackColor : ColorTranslator.FromWin32(structure.crBackColor)); fore = ColorTranslator.FromWin32(structure.crTextColor); Marshal.FreeCoTaskMem(intPtr); }
public void SetRTHighlight(Color back, Color fore) { CHARFORMAT2 structure = default(CHARFORMAT2); structure.dwMask = (CFM_BACKCOLOR | CFM_COLOR); structure.cbSize = Marshal.SizeOf(structure); structure.crBackColor = ColorTranslator.ToWin32(back); structure.crTextColor = ColorTranslator.ToWin32(fore); IntPtr intPtr = Marshal.AllocCoTaskMem(structure.cbSize); Marshal.StructureToPtr(structure, intPtr, fDeleteOld: false); SendMessageToMaster(EM_SETCHARFORMAT, (IntPtr)SCF_SELECTION, intPtr, 1); Marshal.FreeCoTaskMem(intPtr); }
private static CHARFORMAT2 GetCharFormat(this RichTextBox richTextBox, bool fSelection) { // Either CHARFORMAT or CHARFORMAT2 can be used as long as the cbSize is set correctly. // https://msdn.microsoft.com/en-us/library/windows/desktop/bb774230.aspx // Force handle creation if (!richTextBox.IsHandleCreated) { var handle = richTextBox.Handle; } var cf2 = new CHARFORMAT2(); UnsafeNativeMethods.SendMessage(new HandleRef(richTextBox, richTextBox.Handle), RichTextBoxConstants.EM_GETCHARFORMAT, fSelection ? RichTextBoxConstants.SCF_SELECTION : RichTextBoxConstants.SCF_DEFAULT, cf2); return(cf2); }
public static void SetSelectionCharOffsetInTwips(this RichTextBox richTextBox, int offsetInTwips) { // Adapted from http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/RichTextBox.cs,7765c7694b8e433f // Force handle creation if (!richTextBox.IsHandleCreated) { var handle = richTextBox.Handle; } var cf2 = new CHARFORMAT2(); cf2.dwMask = RichTextBoxConstants.CFM_OFFSET; cf2.yOffset = offsetInTwips; UnsafeNativeMethods.SendMessage(new HandleRef(richTextBox, richTextBox.Handle), RichTextBoxConstants.EM_SETCHARFORMAT, RichTextBoxConstants.SCF_SELECTION, cf2); }
public static void SetUnderline(this RichTextBox te, bool Underline) { var CharFormat = new CHARFORMAT2(); CharFormat.cbSize = Marshal.SizeOf(CharFormat); SendMessage(te.Handle, EM_GETCHARFORMAT, SCF_SELECTION, ref CharFormat); CHARFORMAT2 fmt = new CHARFORMAT2(); fmt.cbSize = Marshal.SizeOf(fmt); fmt.dwMask = CFM_UNDERLINETYPE; fmt.bUnderlineType = Underline ? CFU_UNDERLINEWAVE : CFU_UNDERLINENONE; SendMessage(te.Handle, EM_SETCHARFORMAT, SCF_SELECTION, ref fmt); }
public static void SetSelectionBackColor(this RichTextBox richTextBox, Color value) { if (richTextBox.IsHandleCreated && value == Color.Empty) { var cf2 = new CHARFORMAT2(); cf2.dwEffects = RichTextBoxConstants.CFE_AUTOBACKCOLOR; cf2.dwMask = RichTextBoxConstants.CFM_BACKCOLOR; cf2.crBackColor = ColorTranslator.ToWin32(value); UnsafeNativeMethods.SendMessage(new HandleRef(richTextBox, richTextBox.Handle), RichTextBoxConstants.EM_SETCHARFORMAT, RichTextBoxConstants.SCF_SELECTION, cf2); } else { richTextBox.SelectionBackColor = value; } }
/// <summary> /// Sets the font name for the selected text of the RichTextBox /// </summary> /// <param name="richTextBox">RichTextBox control</param> /// <param name="fontName">Name of the font to use</param> /// <returns>Returns true on success, false on failure</returns> public static bool SelectionFontName(RichTextBox richTextBox, string fontName) { var cf = new CHARFORMAT2(); cf.cbSize = Marshal.SizeOf(cf); cf.dwMask = CFM_FACE; cf.szFaceName = fontName.Length > 32 ? fontName.Remove(32) : fontName; IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf)); Marshal.StructureToPtr(cf, lParam, false); int res = SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam); return(res == 0); }
/// <summary> /// Sets the font size for the selected text of the RichTextBox /// </summary> /// <param name="richTextBox">RichTextBox control</param> /// <param name="fontSize">Font size to use</param> /// <returns>Returns true on success, false on failure</returns> public static bool SelectionFontSize(RichTextBox richTextBox, int fontSize) { var cf = new CHARFORMAT2(); cf.cbSize = Marshal.SizeOf(cf); cf.dwMask = CFM_SIZE; cf.yHeight = fontSize * 20; // yHeight is in 1/20 pt IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf)); Marshal.StructureToPtr(cf, lParam, false); int res = SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam); return(res == 0); }
/// <summary> /// Sets the font style for the selected text of the RichTextBox /// </summary> /// <param name="richTextBox">RichTextBox control</param> /// <param name="mask">Mask to determine which styles will be modified</param> /// <param name="effect">New values for the styles</param> /// <returns>Returns true on success, false on failure</returns> private static bool SetSelectionStyle(RichTextBox richTextBox, Color fontColor, Color backColor, UInt32 mask, UInt32 effect) { var cf = new CHARFORMAT2(); cf.cbSize = Marshal.SizeOf(cf); cf.crTextColor = ColorTranslator.ToWin32(fontColor); cf.crBackColor = ColorTranslator.ToWin32(backColor); cf.dwMask = mask; cf.dwEffects = effect; IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf)); Marshal.StructureToPtr(cf, lParam, false); int res = SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam); return(res == 0); }
/// <summary> /// Sets the font color for the word in the selected point of the RichTextBox /// </summary> /// <param name="richTextBox">RichTextBox control</param> /// <param name="color">Color to apply</param> /// <returns>Returns true on success, false on failure</returns> public static bool WordFontColor(RichTextBox richTextBox, Color color) { var cf = new CHARFORMAT2(); cf.cbSize = Marshal.SizeOf(cf); cf.crTextColor = ColorTranslator.ToWin32(color); cf.crBackColor = ColorTranslator.ToWin32(Color.Empty); cf.dwMask = CFM_COLOR; cf.dwEffects = 0; IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf)); Marshal.StructureToPtr(cf, lParam, false); int res = SendMessage(richTextBox.Handle, EM_SETCHARFORMAT, SCF_SELECTION | SCF_WORD, lParam); return(res == 0); }
internal static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CHARFORMAT2 format);
internal static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, ref CHARFORMAT2 lParam);
internal static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, ref CHARFORMAT2 lParam);
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, ref CHARFORMAT2 lParam);
internal static extern int SendMessage(HandleRef hWnd, int msg, int wParam, ref CHARFORMAT2 lParam);
private static extern int SendMessage(IntPtr hWnd, EditMessage msg, SCF wParam, ref CHARFORMAT2 fmt);
private static extern IntPtr SendMessageCHARFORMAT(IntPtr hWnd, uint Msg, int wParam, ref CHARFORMAT2 lParam);
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] CHARFORMAT2 lParam);
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref CHARFORMAT2 cf2);
private void SetColor(Color backColor, Color foreColor) { CHARFORMAT2 charFormat = new CHARFORMAT2(); charFormat.cbSize = Marshal.SizeOf(typeof(CHARFORMAT2)); charFormat.dwMask = NuGenConstants.CFM_BACKCOLOR | NuGenConstants.CFM_COLOR; charFormat.crBackColor = MakeColorRef(backColor.R, backColor.G, backColor.B); charFormat.crTextColor = MakeColorRef(foreColor.R, foreColor.G, foreColor.B); IntPtr lparam = IntPtr.Zero; try { lparam = Marshal.AllocHGlobal(charFormat.cbSize); IntPtr wparam = new IntPtr(NuGenConstants.SCF_SELECTION); Marshal.StructureToPtr(charFormat, lparam, false); SendMessage(Handle, NuGenConstants.EM_SETCHARFORMAT, wparam, lparam); } catch { throw; } finally { if (lparam != IntPtr.Zero) { Marshal.DestroyStructure(lparam, typeof(CHARFORMAT2)); Marshal.FreeHGlobal(lparam); } } }
internal static IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, ref CHARFORMAT2 cf) { IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf)); Marshal.StructureToPtr(cf, lParam, false); return NativeMethods.SendMessage(hWnd, msg, wParam, lParam); }
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref CHARFORMAT2 lParam);
internal static extern int SendMessage(HandleRef hWnd, int msg, int wParam, ref CHARFORMAT2 lParam);
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref CHARFORMAT2 cf2);