前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#GDI绘制自定义字体

C#GDI绘制自定义字体

作者头像
恋喵大鲤鱼
发布2018-08-03 17:26:48
1.2K0
发布2018-08-03 17:26:48
举报
文章被收录于专栏:C/C++基础C/C++基础

 绘制自定义的字体:

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));         }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年07月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档