在MCU项目中,准备自己写个简单的UI,在中文文字显示,打算选择 UCDOS 中的 HZK16,为了查看字体文件,用C#写了个简单的查看器,并导出数据为C语言数组(因为所用的STM32F103VET片内flash有512K,可以容纳字库,不用外接SPI Flash了)。

源程序下载:http://download.csdn.net/detail/mostone/6024943

OS:windows 2008 R2 standard (zh-cn)

IDE:Microsoft Visual Studio Express 2012 for Windows Desktop - Microsoft Visual C# 2012

两个点阵字体下载链接:

常用的几个字体库---ASC16、HZK16、HZK12,附HZK16的使用资料

16*16点阵中文字库BIN文件

先上几张图:

查看器有两个 Form,一个是主画面,一个是选择字体对话框。布局如上图,下面是两个源代码:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace HZK
{public partial class Form1 : Form{System.IO.Stream pFontReadStream;string pFontFileName;int pPageNo = 0;Size pFontSize;Bitmap pBitmap;Graphics pGraphics;FormOpenFontDialog pDialog = null;static string[] FONT_SIZE_LIST = new string[] { "12x6", "12x8", "12x12", "16x8", "16x16", "24x24", "40x20", "40x40", "48x24", "48x48" };public Form1(){InitializeComponent();this.pBitmap = new Bitmap(930, 910);this.pGraphics = Graphics.FromImage(pBitmap);this.toolStripStatusLabel1.Visible = false;this.toolStripProgressBar1.Visible = false;this.comboBox1.Items.AddRange(FONT_SIZE_LIST.ToArray());}public static string[] getFontSizeList(){return FONT_SIZE_LIST;}private Size getSize(string value){return new Size(Int16.Parse(value.Substring(3)), Int16.Parse(value.Substring(0, 2)));}private void btnPickFont_Click(object sender, EventArgs e){if (pDialog == null) pDialog = new FormOpenFontDialog();if (pDialog.ShowDialog() != DialogResult.OK) return;pFontReadStream = pDialog.getFontFileStream();pFontSize = this.getSize(pDialog.getFontSize());pFontFileName = pDialog.getFontFileName();redraw();}private void redraw(){pPageNo = 0;drawPage(pPageNo);this.toolStripStatusLabel1.Text = String.Format("{0}   Font size:{1}x{2}", pFontFileName, pFontSize.Height, pFontSize.Width);}private void drawPage(int pageNo){if (pFontReadStream == null) return;const int space = 5;const int ox = 50;const int oy = 20;Point pt = new Point(ox, oy);pFontReadStream.Position = pageNo * 16 * 16 * (pFontSize.Width == 12 ? 16 : pFontSize.Width) * pFontSize.Height / 8;pGraphics.Clear(this.pictureBox1.BackColor);this.toolStripStatusLabel1.Visible = false;this.toolStripProgressBar1.Visible = true;List<byte> list = new List<byte>();bool eof = false;int tmp;for (int row = 0; row < 16; row++){pGraphics.DrawString((pageNo * 16 * 16 + row * 16).ToString("X4"), DefaultFont, Brushes.Black, new Point(10, oy + row * (space + pFontSize.Height)));for (int col = 0; col < 16; col++){for (int i = 0; i < (pFontSize.Width == 12 ? 16 : pFontSize.Width) * pFontSize.Height / 8; i++){tmp = pFontReadStream.ReadByte();if (tmp == -1){eof = true; break;}list.Add((byte)tmp);}if (eof) break;if (this.radioButton1.Checked){drawFontAsRowScan(pt, pGraphics, list, pFontSize);}else{drawFontAsColumnScan(pt, pGraphics, list, pFontSize);}pt.X += space + pFontSize.Width;list.Clear();this.toolStripProgressBar1.Value = Math.Min((row * 16 + col) / 16 * 16, 100);}if (eof) break;pt.Y += space + pFontSize.Height;pt.X = ox;}pictureBox1.Image = pBitmap;this.toolStripProgressBar1.Visible = false;this.toolStripStatusLabel1.Visible = true;}// draw one charprivate void drawFontAsRowScan(Point pt, Graphics graph, List<byte> list, Size fontSize){Pen pen;int mask;int bytesPerLine = (int)Math.Ceiling((decimal)fontSize.Width / 8);List<byte>.Enumerator enm = list.GetEnumerator();enm.MoveNext();byte byteData = enm.Current;for (int row = 0; row < fontSize.Height; row++){for (int bytes = 0; bytes < bytesPerLine; bytes++){mask = 0x80;for (int bits = 0; bits < 8; bits++){pen = ((byteData & mask) == mask ? Pens.Black : Pens.White);graph.DrawRectangle(pen, pt.X, pt.Y, 1, 1);mask = mask >> 1;pt.X += 1;if (bytes * 8 + bits == fontSize.Width - 1) break;}enm.MoveNext();byteData = enm.Current;}pt.Y += 1;pt.X -= fontSize.Width;}}// draw one charprivate void drawFontAsColumnScan(Point pt, Graphics graph, List<byte> list, Size fontSize){Pen pen;int mask;int bytesPerLine = (int)Math.Ceiling((decimal)fontSize.Width / 8);List<byte>.Enumerator enm = list.GetEnumerator();enm.MoveNext();byte byteData = enm.Current;for (int col = 0; col < fontSize.Width; col++){for (int bytes = 0; bytes < bytesPerLine; bytes++){mask = 0x80;for (int bits = 0; bits < 8; bits++){pen = ((byteData & mask) == mask ? Pens.Black : Pens.White);graph.DrawRectangle(pen, pt.X, pt.Y, 1, 1);mask = mask >> 1;pt.Y += 1;if (bytes * 8 + bits == fontSize.Height - 1) break;}enm.MoveNext();byteData = enm.Current;}pt.X += 1;pt.Y -= fontSize.Height;}}private void Form1_FormClosed(object sender, FormClosedEventArgs e){if (pFontReadStream != null){pFontReadStream.Close();}}private void button1_Click(object sender, EventArgs e){drawPage(++pPageNo);}private void button2_Click(object sender, EventArgs e){if (pPageNo == 0) return;drawPage(--pPageNo);}private void button3_Click(object sender, EventArgs e){if (pFontReadStream == null) return;if (this.saveFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)return;System.IO.Stream ss = this.saveFileDialog1.OpenFile();System.IO.StreamWriter sw = new System.IO.StreamWriter(ss);pFontReadStream.Position = 0;int byt;int pos = 0;while (true){byt = pFontReadStream.ReadByte();if (byt == -1) break;pos++;if (pos == 32){sw.WriteLine("0x" + byt.ToString("X2") + ",");pos = 0;}else{sw.Write("0x" + byt.ToString("X2") + ",");}}sw.Close();MessageBox.Show("Font array export done.");}private void radioButton1_CheckedChanged(object sender, EventArgs e){drawPage(pPageNo);}private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){this.pFontSize = getSize(this.comboBox1.SelectedItem.ToString());redraw();}}
}

FormOpenFontDialog.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace HZK
{public partial class FormOpenFontDialog : Form{string pFontSize = null;public FormOpenFontDialog(){InitializeComponent();string[] fontSizeList = Form1.getFontSizeList();string prevFontSize = null;Size radioSize = new Size(120, 30);int radioOriX = 25;Point radioLocation = new Point(radioOriX, 30);foreach (string fontSize in fontSizeList){if (prevFontSize == null) {prevFontSize = fontSize.Substring(0, 2);}else if (fontSize.Substring(0, 2) != prevFontSize){radioLocation.Y += radioSize.Height;radioLocation.X = radioOriX;prevFontSize = fontSize.Substring(0, 2);}RadioButton radio = new RadioButton();radio.Text = fontSize;radio.Location = radioLocation;radio.CheckedChanged += new System.EventHandler(this.radio_CheckedChanged);this.groupBox1.Controls.Add(radio);radioLocation.X += radioSize.Width;}}private void btnPickFont_Click(object sender, EventArgs e){if (this.openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)return;this.txtFontFile.Text = this.openFileDialog1.FileName;}private void button3_Click(object sender, EventArgs e){this.DialogResult = DialogResult.Cancel;}public string getFontFileName(){return this.txtFontFile.Text;}public System.IO.Stream getFontFileStream(){if (this.txtFontFile.Text.Length != 0){return this.openFileDialog1.OpenFile();}return null;}public string getFontSize(){return pFontSize;}private void button1_Click(object sender, EventArgs e){if (this.txtFontFile.Text.Length == 0){MessageBox.Show("Please select font file.");return;}if (pFontSize == null){MessageBox.Show("Please select font type.");return;}this.DialogResult = DialogResult.OK;}private void radio_CheckedChanged(object sender, EventArgs e){pFontSize = ((RadioButton)sender).Text;}}
}

点阵字体文件查看器 c#(HZK16)相关推荐

  1. 【玩转.Net MF – 03】远程文件查看器

    虽说目前.Net Micro Framework已经支持文件系统(FAT16/FAT32),但在远程还无法直接访问,从某种意义上讲,无法和PC交互的存储介质显得有些鸡肋.我做SideShow相关开发的 ...

  2. 文件查看器示例支持 文件预览,编辑和视频播放

    以下为文件查看器示例,支持 文件预览,和视频播放: import QtQuick 2.2 import QtQuick.Window 2.1 import QtQuick.Controls 1.2 i ...

  3. Linux 下高级日志文件查看器Log File Navigator

    Log File Navigator,简称lnav,是一款面向小规模的适用于 Linux 的高级日志文件查看器.它是一个终端应用程序,可以理解您的日志文件,让您轻松找到问题,几乎不需要什么设置. ln ...

  4. 使用广泛的开源PCB文件查看器 Gerbv 含多个严重漏洞

     聚焦源代码安全,网罗国内外最新资讯! 编译:代码卫士 专栏·供应链安全 数字化时代,软件无处不在.软件如同社会中的"虚拟人",已经成为支撑社会正常运转的最基本元素之一,软件的安全 ...

  5. bin文件查看器app_腾讯文件:腾讯官方出品的微信 / QQ 文件管理和清理利器

    前言 微信和 QQ 基本是大家手机上的必备应用了,但是使用时间久了,难免会积攒很多文件,甚至引起应用卡顿. 包括垃圾/缓存文件,或者我们接收保存的文件等等,占据了大量空间,又不好管理. 今天就分享一款 ...

  6. 【实用软件】Json文件查看器(支持查看超大JSON文件)

    内容信息 软件类型:绿色 软件平台:电脑 软件版本:v1.0 软件大小:3.4MB 软件特点 Json文件查看器是一个用来查看Json文件的的绿色软件

  7. 好用的Bin文件查看器,J-flash

    工作中,很多地方用到Bin文件,如编译完成后的固件和从MCU的Flash读出来的文件,这时候一个好的Bin文件查看器至关重要.经常我们用STM32自带的STM32 ST-LINK Utility可以直 ...

  8. IFC模型文件查看器(基于IFC++开源库实现)

    关于IFC IFC是由buildingSMART以工业的产品资料交换标准STEP编号ISO-10303-11的产品模型信息描述用EXPERSS语言为基础,基于BIM中AEC/FM相关领域信息交流所指定 ...

  9. html文件阅读器电脑版,Excel文件查看器

    Excel文件查看器电脑版是一款文档查看软件,Excel文件查看器电脑版对于需要临时查看Excel文档但是又不需要修改它的用户来说,Excel文件查看器电脑版无疑是最佳选择,速度快并且小巧,是个不错的 ...

最新文章

  1. BootStrap 模态框禁用空白处点击关闭
  2. 评价对象检测模型的数字度量:F1分数以及它们如何帮助评估模型的表现
  3. DNS隧道工具使用 不过其网络传输速度限制较大
  4. React官方文档学习笔记(二)
  5. java stream中Collectors的用法
  6. 垃圾回收算法简单介绍——JVM读书笔记lt;二gt;
  7. WinForm中使用Excel控件
  8. VS2017创建ASP.NET Core Web程序
  9. Go语言channel与select原理
  10. less-postcss
  11. 40Linux组41Linux所有者42Linux所在组43Linux修改所有组
  12. 产品经理学习笔记(二)-------------------商业模式和商业模式画布
  13. csdn积分怎么获取?
  14. JEECG代码生成器(GUI)的使用
  15. 《汉字简体、繁体相互转换》 查看源代码
  16. python弧度角度转换程序_python 弧度与角度互转实例
  17. 就业指导期末试题(含正确答案)
  18. 有关于win10系统不能更改自己ip得问题解决办法
  19. C++ 将二叉树以前序遍历的顺序转换成链表
  20. Java获取当前年月日、时间

热门文章

  1. javaFX学习之DatePicker日期控件
  2. 从青铜到王者,助力企业轻松上云的四大绝招!
  3. JavaScript--JQuery事件 当网页元素加载完成后再去执行事件
  4. 【SUMO学习】初级 Autobahn
  5. 【项目】bxg基于SaaS的餐掌柜项目实战(2023)
  6. 20120912,微软9月12日发布2个安全补丁
  7. ASP.NET Web程序设计习题与练习答案-祁长兴主编版
  8. javascript变量声明提升(hoisting)
  9. v-if和v-for不能一起使用的原因以及解决办法
  10. java读写ini文件