想做个单片机的16*16的汉字取模软件,在网上找了好久还是没解决!这是我的代码,望高手帮我看下。想把一个字符的16*16点阵数据取出来。
Code
/// <summary>
/// 定点数
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct FIXED
{
/// <summary>
/// 小数部分
/// </summary>
public ushort fract;
/// <summary>
/// 整数部分
/// </summary>
public short value;
}
/// <summary>
/// 转换矩阵
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct MAT2
{
[MarshalAs(UnmanagedType.Struct)]
public FIXED eM11;
[MarshalAs(UnmanagedType.Struct)]
public FIXED eM12;
[MarshalAs(UnmanagedType.Struct)]
public FIXED eM21;
[MarshalAs(UnmanagedType.Struct)]
public FIXED eM22;
}
/// <summary>
/// 点结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
/// <summary>
/// X值
/// </summary>
public int x;
/// <summary>
/// Y值
/// </summary>
public int y;
}
/// <summary>
/// 图形信息结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct GLYPHMETRICS
{
public uint gmBlackBoxX;
public uint gmBlackBoxY;
[MarshalAs(UnmanagedType.Struct)]
public POINT gmptGlyphOrigin;
public short gmCellIncX;
public short gmCellIncY;
}
/// <summary>
/// 扩展点结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINTFX
{
/// <summary>
/// X值
/// </summary>
[MarshalAs(UnmanagedType.Struct)]
public FIXED x;
/// <summary>
/// Y值
/// </summary>
[MarshalAs(UnmanagedType.Struct)]
public FIXED y;
}
/// <summary>
/// 字形轮廓头结构
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct TTPOLYGONHEADER
{
/// <summary>
/// 轮廓字节数
/// </summary>
public int cb;
/// <summary>
/// 轮廓类型
/// </summary>
public int dwType;
/// <summary>
/// 多边形的起始点
/// </summary>
[MarshalAs(UnmanagedType.Struct)]
public POINTFX pfxStart;
}
/// <summary>
/// 线段头结构,不包含点序列
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct TTPOLYCURVEHEAD
{
/// <summary>
/// 线类型,折线(TT_PRIM_LINE)=1;B样条曲线(TT_PRIM_OSPLINE)=2
/// </summary>
public short wType;
/// <summary>
/// 点序列中包含点的个数
/// </summary>
public short cpfx;
}
Code
public class GdiNativeMethods
{
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
[DllImport("gdi32.dll")]
public static extern int GetGlyphOutline(IntPtr hdc, uint uChar, uint fuFormat, out GLYPHMETRICS lpgm, uint cbBuffer, IntPtr lpBuffer, ref MAT2 lpmat2);
[DllImport("Kernel32.dll")]
public static extern IntPtr CloseHandle(IntPtr hObject);
}
Code
private uint GetGB2312Coding(char ch)
{//获得字符的GB码
byte[] bts = Encoding.GetEncoding("GB2312").GetBytes(new char[] { ch });
uint val = bts[0];
if (bts.Length > 1)
{
val = val * 256 + bts[1];
}
return val;
}
Code
private void button1_Click(object sender, EventArgs e)
{//将取出的信息显示在richTextBox1里面
richTextBox1.Text = "";
foreach (char t in textBox1.Text)
{
uint ch = GetGB2312Coding(t);
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr hdc = g.GetHdc();
//将字体选入场景
IntPtr fontPtr = textBox1.Font.ToHfont();
GdiNativeMethods.SelectObject(hdc, fontPtr);
MAT2 mat2 = new MAT2();
mat2.eM11.value = 1;
mat2.eM22.value = 1;
GLYPHMETRICS lpGlyph = new GLYPHMETRICS();
//获取缓存区大小
int bufferSize = GdiNativeMethods.GetGlyphOutline(hdc, ch, 1, out lpGlyph, 0, IntPtr.Zero, ref mat2);
if (bufferSize > 0)
{
//获取成功后,分配托管内存
IntPtr ipvbuffer = Marshal.AllocHGlobal(bufferSize);
int ret = GdiNativeMethods.GetGlyphOutline(hdc, ch, 1, out lpGlyph, (uint)bufferSize, ipvbuffer, ref mat2);
if (ret > 0)
{
byte[] b = new byte[bufferSize];
for (int i = 0; i < bufferSize; i++)
{
b[i] = Marshal.ReadByte((IntPtr)(ipvbuffer.ToInt32() + i * 8));
richTextBox1.Text += b[i].ToString() + "\r\n";
}
}
label1.Text =ret.ToString();
}
不知道这样做对不对。取的数据哪些是需要的数据呢!谢谢啦!
问题补充:
代码打不开。。。
private uint GetGB2312Coding(char ch)
{//获得字符的GB码
byte[] bts = Encoding.GetEncoding("GB2312").GetBytes(new char[] { ch });
uint val = bts[0];
if (bts.Length > 1)
{
val = val * 256 + bts[1];
}
return val;}
private void button1_Click(object sender, EventArgs e)
{//将取出的信息显示在richTextBox1里面
richTextBox1.Text = "";
foreach (char t in textBox1.Text)
{
uint ch = GetGB2312Coding(t);
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr hdc = g.GetHdc();
//将字体选入场景
IntPtr fontPtr = textBox1.Font.ToHfont();
GdiNativeMethods.SelectObject(hdc, fontPtr);
MAT2 mat2 = new MAT2();
mat2.eM11.value = 1;
mat2.eM22.value = 1;
GLYPHMETRICS lpGlyph = new GLYPHMETRICS();
//获取缓存区大小
int bufferSize = GdiNativeMethods.GetGlyphOutline(hdc, ch, a, out lpGlyph, 0, IntPtr.Zero, ref mat2);
if (bufferSize > 0)
{
//获取成功后,分配托管内存
IntPtr ipvbuffer = Marshal.AllocHGlobal(bufferSize);
int ret = GdiNativeMethods.GetGlyphOutline(hdc, ch, a, out lpGlyph, (uint)bufferSize, ipvbuffer, ref mat2);
if (ret > 0)
{
byte[] b = new byte[bufferSize];
for (int i = 0; i < bufferSize; i++)
{
b[i] = Marshal.ReadByte((IntPtr)(ipvbuffer.ToInt32() + i * 8));
richTextBox1.Text += b[i].ToString() + "\r\n";
}}
label1.Text =ret.ToString()
}
}
作业本本
|
初学一级
|
园豆:
140
提问于:2009-11-06 10:48