绘制自定义的字体:
private void PaintMessageBox() { Graphics g = this.panel1.CreateGraphics();//为panel1创建Graphics对象 Font ft = new Font("黑体", 17F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));//定义字体
//判断要绘制的字符串字节数是否大于16,中文占两个字节,英文占一个字节
if (Encoding.Default.GetByteCount(message) > 16) { string str1 = message.Substring(0, 8);//从下标0开始,截取8个字节的字符串,一行绘制4个中文 string str2 = message.Substring(8);
//(调用绘制字符串函数DrawString(),参数为字符串,笔刷颜色,绘制的起始位置
Encoding.Default.GetByteCount(str1) * 6表示每个字节占6个像素宽 (this.panel1.Height / 2) - 26)表示字体底部距离panel1顶部的距离)
g.DrawString(str1, ft, Brushes.White, new Point((this.panel1.Width / 2) - Encoding.Default.GetByteCount(str1) * 6, (this.panel1.Height / 2) - 26));
//同上,绘制第二行字符串 g.DrawString(str2, ft, Brushes.White, new Point((this.panel1.Width / 2) - Encoding.Default.GetByteCount(str2) * 6, (this.panel1.Height / 2) + 4)); }
//一行可以搞定的话,将字符串绘制在panel的中间 else g.DrawString(message, ft, Brushes.White, new Point((this.panel1.Width / 2) - Encoding.Default.GetByteCount(message) * 6, (this.panel1.Height / 2) - 12)); }