1. <legend id='4CgSu'><style id='4CgSu'><dir id='4CgSu'><q id='4CgSu'></q></dir></style></legend>

      <tfoot id='4CgSu'></tfoot>

    2. <small id='4CgSu'></small><noframes id='4CgSu'>

          <bdo id='4CgSu'></bdo><ul id='4CgSu'></ul>
        <i id='4CgSu'><tr id='4CgSu'><dt id='4CgSu'><q id='4CgSu'><span id='4CgSu'><b id='4CgSu'><form id='4CgSu'><ins id='4CgSu'></ins><ul id='4CgSu'></ul><sub id='4CgSu'></sub></form><legend id='4CgSu'></legend><bdo id='4CgSu'><pre id='4CgSu'><center id='4CgSu'></center></pre></bdo></b><th id='4CgSu'></th></span></q></dt></tr></i><div id='4CgSu'><tfoot id='4CgSu'></tfoot><dl id='4CgSu'><fieldset id='4CgSu'></fieldset></dl></div>
      1. 更改richtextbox的字体而不丢失格式

        时间:2023-05-20
        <tfoot id='D87k4'></tfoot>

          • <legend id='D87k4'><style id='D87k4'><dir id='D87k4'><q id='D87k4'></q></dir></style></legend>
            <i id='D87k4'><tr id='D87k4'><dt id='D87k4'><q id='D87k4'><span id='D87k4'><b id='D87k4'><form id='D87k4'><ins id='D87k4'></ins><ul id='D87k4'></ul><sub id='D87k4'></sub></form><legend id='D87k4'></legend><bdo id='D87k4'><pre id='D87k4'><center id='D87k4'></center></pre></bdo></b><th id='D87k4'></th></span></q></dt></tr></i><div id='D87k4'><tfoot id='D87k4'></tfoot><dl id='D87k4'><fieldset id='D87k4'></fieldset></dl></div>

            <small id='D87k4'></small><noframes id='D87k4'>

              <bdo id='D87k4'></bdo><ul id='D87k4'></ul>
                    <tbody id='D87k4'></tbody>
                • 本文介绍了更改richtextbox的字体而不丢失格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  如何在不丢失格式的情况下更改richtextbox所有内容的字体?

                  How to change the font of all the contents of a richtextbox without losing formatting?

                  我正在尝试使用

                  rtb.SelectAll();
                  rtb.SelectionFont = new Font(fontName,...);
                  

                  但是字体构造器除了字体类型之外还必须采用字体样式(粗体、斜体、...)或字体大小.所以使用它会改变richtextbox所有内容的样式/大小.

                  but the font constructor has to take besides the font type either the font style (bold, italics, ...) or font size. So using this would change the style/size of all the content of the richtextbox.

                  当然,这同样适用于富文本框中的任何选择.

                  Of course the same applies for any selection in the richtextbox.

                  推荐答案

                  这是我以前用过的一个RichTextBox.它是从 Stack Overflow 和整个互联网上的代码拼接在一起的:

                  This is a RichTextBox that I have used in the past. It's spliced together from code found here at Stack Overflow and the internet at large:

                  public class RichBox : RichTextBox {
                    private const UInt32 CFM_BOLD = 0x00000001;
                    private const UInt32 CFM_ITALIC = 0x00000002;
                    private const UInt32 CFM_UNDERLINE = 0x00000004;
                    private const UInt32 CFM_STRIKE = 0x00000008;
                    private const UInt32 CFM_FACE = 0x20000000;
                    private const UInt32 CFM_SIZE = 0x80000000;
                  
                    private const int WM_PAINT = 0xF;
                    private const int WM_SETREDRAW = 0xB;
                    private const int WM_USER = 0x400;
                  
                    private const int EM_SETCHARFORMAT = (WM_USER + 68);
                    private const int SCF_SELECTION = 0x0001;
                    private const int EM_GETEVENTMASK = WM_USER + 59;
                    private const int EM_SETEVENTMASK = WM_USER + 69;
                    private const int EM_GETSCROLLPOS = WM_USER + 221;
                    private const int EM_SETSCROLLPOS = WM_USER + 222;
                  
                    [StructLayout(LayoutKind.Sequential)]
                    private struct CHARFORMAT {
                      public int cbSize;
                      public uint dwMask;
                      public uint dwEffects;
                      public int yHeight;
                      public int yOffset;
                      public int crTextColor;
                      public byte bCharSet;
                      public byte bPitchAndFamily;
                      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
                      public char[] szFaceName;
                      public short wWeight;
                      public short sSpacing;
                      public int crBackColor;
                      public int LCID;
                      public uint dwReserved;
                      public short sStyle;
                      public short wKerning;
                      public byte bUnderlineType;
                      public byte bAnimation;
                      public byte bRevAuthor;
                    }
                  
                    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
                    private static extern IntPtr LoadLibrary(string lpFileName);
                  
                    [DllImport("user32", CharSet = CharSet.Auto)]
                    private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref CHARFORMAT lParam);
                  
                    [DllImport("user32.dll")]
                    private static extern IntPtr SendMessage(IntPtr hWnd, Int32 wMsg, Int32 wParam, ref Point lParam);
                  
                    [DllImport("user32.dll")]
                    private static extern IntPtr SendMessage(IntPtr hWnd, Int32 wMsg, Int32 wParam, IntPtr lParam);
                  
                    private bool frozen = false;
                    private Point lastScroll = Point.Empty;
                    private IntPtr lastEvent = IntPtr.Zero;
                    private int lastIndex = 0;
                    private int lastWidth = 0;
                  
                    protected override CreateParams CreateParams {
                      get {
                        var cp = base.CreateParams;
                        if (LoadLibrary("msftedit.dll") != IntPtr.Zero) {
                          cp.ClassName = "RICHEDIT50W";
                        }
                        return cp;
                      }
                    }
                  
                    [Browsable(false)]
                    [DefaultValue(typeof(bool), "False")]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public bool FreezeDrawing {
                      get { return frozen; }
                      set {
                        if (value != frozen) {
                          frozen = value;
                          if (frozen) {
                            this.SuspendLayout();
                            SendMessage(this.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
                            SendMessage(this.Handle, EM_GETSCROLLPOS, 0, ref lastScroll);
                            lastEvent = SendMessage(this.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
                            lastIndex = this.SelectionStart;
                            lastWidth = this.SelectionLength;
                          } else {
                            this.Select(lastIndex, lastWidth);
                            SendMessage(this.Handle, EM_SETEVENTMASK, 0, lastEvent);
                            SendMessage(this.Handle, EM_SETSCROLLPOS, 0, ref lastScroll);
                            SendMessage(this.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
                            this.Invalidate();
                            this.ResumeLayout();
                          }
                        }
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public Font CurrentFont {
                      get {
                        Font result = this.Font;
                        if (this.SelectionLength == 0) {
                          result = SelectionFont;
                        } else {
                          using (RichBox rb = new RichBox()) {
                            rb.FreezeDrawing = true;
                            rb.SelectAll();
                            rb.SelectedRtf = this.SelectedRtf;
                            rb.Select(0, 1);
                            result = rb.SelectionFont;
                          }
                        }
                        return result;
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public string SelectionFontName {
                      get { return CurrentFont.FontFamily.Name; }
                      set {
                        CHARFORMAT cf = new CHARFORMAT();
                        cf.cbSize = Marshal.SizeOf(cf);
                        cf.szFaceName = new char[32];
                        cf.dwMask = CFM_FACE;
                        value.CopyTo(0, cf.szFaceName, 0, Math.Min(31, value.Length));
                        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
                        Marshal.StructureToPtr(cf, lParam, false);
                        SendMessage(this.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam);
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public float SelectionFontSize {
                      get { return CurrentFont.Size; }
                      set {
                        CHARFORMAT cf = new CHARFORMAT();
                        cf.cbSize = Marshal.SizeOf(cf);
                        cf.dwMask = CFM_SIZE;
                        cf.yHeight = Convert.ToInt32(value * 20);
                        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
                        Marshal.StructureToPtr(cf, lParam, false);
                        SendMessage(this.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam);
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public bool SelectionBold {
                      get { return CurrentFont.Bold; }
                      set {
                        CHARFORMAT cf = new CHARFORMAT();
                        cf.cbSize = Marshal.SizeOf(cf);
                        cf.dwMask = CFM_BOLD;
                        cf.dwEffects = value ? CFM_BOLD : 0;
                        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
                        Marshal.StructureToPtr(cf, lParam, false);
                        SendMessage(this.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam);
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public bool SelectionItalic {
                      get { return CurrentFont.Italic; }
                      set {
                        CHARFORMAT cf = new CHARFORMAT();
                        cf.cbSize = Marshal.SizeOf(cf);
                        cf.dwMask = CFM_ITALIC;
                        cf.dwEffects = value ? CFM_ITALIC : 0;
                        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
                        Marshal.StructureToPtr(cf, lParam, false);
                        SendMessage(this.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam);
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public bool SelectionStrikeout {
                      get { return CurrentFont.Strikeout; }
                      set {
                        CHARFORMAT cf = new CHARFORMAT();
                        cf.cbSize = Marshal.SizeOf(cf);
                        cf.dwMask = CFM_STRIKE;
                        cf.dwEffects = value ? CFM_STRIKE : 0;
                        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
                        Marshal.StructureToPtr(cf, lParam, false);
                        SendMessage(this.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam);
                      }
                    }
                  
                    [Browsable(false)]
                    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                    public bool SelectionUnderline {
                      get { return CurrentFont.Underline; }
                      set {
                        CHARFORMAT cf = new CHARFORMAT();
                        cf.cbSize = Marshal.SizeOf(cf);
                        cf.dwMask = CFM_UNDERLINE;
                        cf.dwEffects = value ? CFM_UNDERLINE : 0;
                        IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
                        Marshal.StructureToPtr(cf, lParam, false);
                        SendMessage(this.Handle, EM_SETCHARFORMAT, SCF_SELECTION, lParam);
                      }
                    }
                  }
                  

                  它添加了新的属性,例如 SelectionBold、SelectionItalic 等,您可以在其中应用属性而不会丢失文本的其他格式.

                  It adds new properties such as SelectionBold, SelectionItalic, etc. where you can apply the attribute and not lose the other formatting of the text.

                  这篇关于更改richtextbox的字体而不丢失格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C# 中使用自定义千位分隔符 下一篇:如何将布尔值一般格式化为是/否字符串?

                  相关文章

                  <i id='TYErF'><tr id='TYErF'><dt id='TYErF'><q id='TYErF'><span id='TYErF'><b id='TYErF'><form id='TYErF'><ins id='TYErF'></ins><ul id='TYErF'></ul><sub id='TYErF'></sub></form><legend id='TYErF'></legend><bdo id='TYErF'><pre id='TYErF'><center id='TYErF'></center></pre></bdo></b><th id='TYErF'></th></span></q></dt></tr></i><div id='TYErF'><tfoot id='TYErF'></tfoot><dl id='TYErF'><fieldset id='TYErF'></fieldset></dl></div>
                    • <bdo id='TYErF'></bdo><ul id='TYErF'></ul>

                  1. <tfoot id='TYErF'></tfoot>

                    <small id='TYErF'></small><noframes id='TYErF'>

                      <legend id='TYErF'><style id='TYErF'><dir id='TYErF'><q id='TYErF'></q></dir></style></legend>