richtextbox有没有什么属性让中文和英文都是同一种字体。
否则英文总是Arial字体,即使我在代码里把他切换过来,但再打入的英文又变成了Arial字体
我试过在richTextBox1_TextChanged里写入代码,让每次修改都改变一次字体,但感觉这样打字会卡。
所以想问richtextbox有没有什么属性实现统一

解决方案 »

  1.   

    RichTextBox就是这样子,可以把字母转成宋体 
      

  2.   

    不是有个Font属性吗?我修改它之后,字体大小,粗体啥的都有效。
      

  3.   

    我在richTextBox1_TextChanged中写过
    this.richTextBox1.Font = new Font(fontname, fontsize, fs);
    这样成功了,但是输入文字的时候很卡,特别文本长一点的话感觉很明显
      

  4.   

    回7楼,你自己试下啊,你messagebox你选中的文本啊,它英文的就是会自动变成Arial字体
      

  5.   

    我试了下,貌似是因为字体找不到,所以它找了个默认字体。
    因为如果你选择了宋体,但是宋体不包含英文字体,因此英文字体就去默认的字体(Arial)里去找了。你要是选择了Arial,但是Arial里面不包含中文,因此中文就到默认的字体(宋体)里去找了。所以你会发现,输入的字体里面是含有两种字体的。
      

  6.   

    RichTextBox内部提供了一个方法用来修改单个字符的字体,但是并未公开,如果要使用这个方法,只能利用消息机制,利用API的SendMessage发送消息,这比起你修改全文速度要快。
    反编译后研究得到的相关函数如下:[StructLayout(LayoutKind.Sequential), ComVisible(true)]
    public struct HandleRef
    {
        internal object m_wrapper;
        internal IntPtr m_handle;
        public HandleRef(object wrapper, IntPtr handle)
        {
            this.m_wrapper = wrapper;
            this.m_handle = handle;
        }    public object Wrapper
        {
            get
            {
                return this.m_wrapper;
            }
        }
        public IntPtr Handle
        {
            get
            {
                return this.m_handle;
            }
        }
        public static explicit operator IntPtr(HandleRef value)
        {
            return value.m_handle;
        }    public static IntPtr ToIntPtr(HandleRef value)
        {
            return value.m_handle;
        }
    }[StructLayout(LayoutKind.Sequential, Pack=4)]
    public class CHARFORMATA
    {
        public int cbSize = Marshal.SizeOf(typeof(CHARFORMATA));
        public int dwMask;
        public int dwEffects;
        public int yHeight;
        public int yOffset;
        public int crTextColor;
        public byte bCharSet;
        public byte bPitchAndFamily;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=0x20)]
        public byte[] szFaceName = new byte[0x20];
    }[DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] CHARFORMATA lParam); 不行了,再看下去头晕了,有关CHARFORMATA类的赋值操作,你要是有兴趣的话,自己反编译了研究吧。
      

  7.   

    到Form1.Designer.cs里面修改
    private void InitializeComponent()
            {
                this.richTextBox1 = new System.Windows.Forms.RichTextBox();
                this.richTextBox2 = new System.Windows.Forms.RichTextBox();
                this.SuspendLayout();
                // 
                // richTextBox1
                this.richTextBox1.Font = new Font("隶书", 13, System.Drawing.FontStyle.Regular)
    }
      

  8.   

    找到方法了,原来那个内部方法提供了隐式调用,之前一直没发现,通过对SelectionFont的设置,即调用了那个内部方法了,这样修改字体的效率比全文修改高得多。
    操作方法如下(有加分吗?):        int start = 0;
            int end = 0;
            public testForm()
            {
                InitializeComponent();
                richTextBox1.TextChanged +=new EventHandler(richTextBox1_TextChanged);
                richTextBox1.SelectionChanged += new EventHandler(richTextBox1_SelectionChanged);
            }        void richTextBox1_SelectionChanged(object sender, EventArgs e)
            {
                start = end;
                end = richTextBox1.SelectionStart;
            }        void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                if (end > start)
                {
                    richTextBox1.Select(start, end - start);
                }
                richTextBox1.SelectionFont = new Font("宋体", 12);
                richTextBox1.Select(start, 0);
            }
      

  9.   

      private void BaseText_KeyUp(object sender, KeyEventArgs e)
            {
                if((e.KeyValue>=65 &&e.KeyValue<=90)  || (e.KeyValue>=97 &&e.KeyValue<=122))
                {
                    try
                    {
                        try
                        {
                            BaseText.Font = new Font("宋体", 12, GraphicsUnit.Pixel);
                        }
                        catch
                        {
                            BaseText.Font = new Font("Arial", 12, GraphicsUnit.Pixel);
                        }
                    }
                    catch { }
                }
            }用这个吧,,我研究好久想出来的,,效率很不错,,,这个算是微软BUG了