richtextbox有没有什么属性让中文和英文都是同一种字体。
否则英文总是Arial字体,即使我在代码里把他切换过来,但再打入的英文又变成了Arial字体
我试过在richTextBox1_TextChanged里写入代码,让每次修改都改变一次字体,但感觉这样打字会卡。
所以想问richtextbox有没有什么属性实现统一
否则英文总是Arial字体,即使我在代码里把他切换过来,但再打入的英文又变成了Arial字体
我试过在richTextBox1_TextChanged里写入代码,让每次修改都改变一次字体,但感觉这样打字会卡。
所以想问richtextbox有没有什么属性实现统一
this.richTextBox1.Font = new Font(fontname, fontsize, fs);
这样成功了,但是输入文字的时候很卡,特别文本长一点的话感觉很明显
因为如果你选择了宋体,但是宋体不包含英文字体,因此英文字体就去默认的字体(Arial)里去找了。你要是选择了Arial,但是Arial里面不包含中文,因此中文就到默认的字体(宋体)里去找了。所以你会发现,输入的字体里面是含有两种字体的。
反编译后研究得到的相关函数如下:[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类的赋值操作,你要是有兴趣的话,自己反编译了研究吧。
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)
}
操作方法如下(有加分吗?): 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);
}
{
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了