【问题标题】:How do I set the RichTextBox.SelectionFont FontFamily without changing the Style?如何在不更改样式的情况下设置 RichTextBox.SelectionFont FontFamily?
【发布时间】:2014-06-21 18:57:55
【问题描述】:

我的应用程序中的一个控件限制用户只能更改字体样式(B、I、U)和文本颜色。为此,我创建了一个从 RichTextBox 继承的自定义控件。我能够拦截CTRL-V,并将粘贴文本的字体设置为SystemFonts.DefaultFont。我目前面临的问题是,如果粘贴的文本包含,例如,半粗体半常规样式 - 粗体丢失。

即“Foo Bar”只会粘贴为“Foo Bar”。

我目前唯一的想法是逐个字符地浏览文本(非常慢),然后执行以下操作:

public class MyRichTextBox : RichTextBox
{

private RichTextBox hiddenBuffer = new RichTextBox();

/// <summary>
/// This paste will strip the font size, family and alignment from the text being pasted.
/// </summary>
public void PasteUnformatted()
{
    this.hiddenBuffer.Clear();
    this.hiddenBuffer.Paste();

    for (int x = 0; x < this.hiddenBuffer.TextLength; x++)
    {
        // select the next character
        this.hiddenBuffer.Select(x, 1);

        // Set the font family and size to default
        this.hiddenBuffer.SelectionFont = new Font(SystemFonts.DefaultFont.FontFamily, SystemFonts.DefaultFont.Size, this.hiddenBuffer.SelectionFont.Style);
    }

    // Reset the alignment
    this.hiddenBuffer.SelectionAlignment = HorizontalAlignment.Left;

    base.SelectedRtf = this.hiddenBuffer.SelectedRtf;
    this.hiddenBuffer.Clear();
}

}

谁能想到更清洁(更快)的解决方案?

【问题讨论】:

    标签: c# .net richtextbox rtf


    【解决方案1】:

    MSDN 论坛上的“nobugz”为我回答了这个问题(我需要一个快速的答案,所以经过 SO 的风滚草几乎一天后,我不得不去别处寻找 - 不要评判我!):

    using System.Runtime.InteropServices;
    ...
            public static bool SetRtbFace(RichTextBox rtb, Font font, bool selectionOnly) {
                CHARFORMATW fmt = new CHARFORMATW();
                fmt.cbSize = Marshal.SizeOf(fmt);
                fmt.szFaceName = font.FontFamily.Name;
                fmt.dwMask = 0x20000000;  // CFM_FACE
                return IntPtr.Zero != SendMessage(rtb.Handle, 0x444, (IntPtr)(selectionOnly ? 1 : 4), fmt);
            }
            [StructLayout(LayoutKind.Sequential, Pack = 4)]
            private class CHARFORMATW {
                public int cbSize;
                public int dwMask;
                public int dwEffects;
                public int yHeight;
                public int yOffset;
                public int crTextColor;
                public byte bCharSet;
                public byte bPitchAndFamily;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)]
                public string szFaceName;
            }
    
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, CHARFORMATW lParam);
    

    【讨论】:

      【解决方案2】:

      对于那些想要 Delphi 答案的人,摘录给你基本的想法:

      using RichEdit; //reqd. for the constants and types
      
      var
        chformat : TCharFormat2;
        fontname : string;
      
      begin
        FillChar(chformat,sizeof(chformat),0);
        chformat.cbSize := sizeof(chformat);
        //only modify the szFaceName field, height etc. left alone
        chformat.dwMask :=  CFM_FACE; 
        //get the fontname set by the user
        fontname := AdvFontSelector1.Text;
        strpcopy(chformat.szFaceName,fontname);
        RichEdit1.Perform(EM_SETCHARFORMAT, SCF_SELECTION, lparam(@chformat));
      end;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-21
        • 2013-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        • 2013-11-19
        相关资源
        最近更新 更多