曾经在2011-11-18 写了《获得汉字字符串拼音首字母》,但那只能获得汉字的首字符。实际工作中常常需要汉字的全部拼音,或者更多的是要获得汉字的声母。因为在管理中涉及到数据库,在数据库编程中涉及到字段。人们常将汉字说明转换为字段名,规则是开头和中间部分的汉字取声母(不完全等同首字符),最后汉字则取全拼来共同组成字段名。给予这样的考虑就列出下面的程序,当然是改造自网上的程序。
调用方法是:tmpStr = hz_pinyin("汉字变成对应的拼音ok", "", true);
第1个参数是要转换的汉字字符数字串,第2个是转换后拼音之间的连接字串,第3个是全部拼音时是否仅首字符大写。完成转换后,直接将结果放入剪切板内,以方便其它软件使用。其中的static
int why_pinyin = 2;来控制取得拼音的方式,0代表全拼,1 仅首字母, 2
开头和中间的汉字为声母,最后一个汉字为全拼。
有时候经过市场调查并写成了文本,此时可通过调用金山词霸的屏幕取词功能,将鼠标放到需要的字符串首汉字上,它便自动获得汉字串,同时自动生成按需要的方式获得对应的拼音字串。因此,需要下载早期金山词霸的库XdictGrb.dll,通过命令行注册
regsvr32 XdictGrb.dll ,在解决方案中添加引用即可。
完整代码如下:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using XDICTGRB;//金山词霸组件
using System.Text;
using System.Globalization;
using System.Threading;
using System.Text.RegularExpressions;
namespace WordToIE
{
/// <summary>
/// Description of MainForm.
///
</summary>
public partial class MainForm :
Form,IXDictGrabSink
{
#region 变量定义
// GB2312-80 标准规范中第一个汉字的机内码.即"啊"的机内码
private const int firstChCode = -20319;
// GB2312-80 标准规范中最后一个汉字的机内码.即"齄"的机内码
private const int lastChCode = -2050;
// GB2312-80 标准规范中最后一个一级汉字的机内码.即"座"的机内码
private const int lastOfOneLevelChCode = -10247;
#endregion 变量定义
// 配置中文字符
static Regex regex = new Regex("^[\u4e00-\u9fa5]$");
static int why_pinyin = 2;//控制 取得拼音的方式 //0 全拼, 1
首字母, 2 前面声母,最后一个汉字全拼。
static int last_hz_no = 0;//得到字符串中(中间可能有字母和 数字
),最后的汉字序号,它要变成全拼,开始的都为声母
public MainForm()
{
//
// The
InitializeComponent() call is required for Windows Forms designer
support.
//
InitializeComponent();
//
// TODO: Add
constructor code after the InitializeComponent() call.
//
}
void MainFormLoad(object
sender, EventArgs e)
{
XDICTGRB.GrabProxy
gp = new GrabProxy();
gp.GrabInterval = 1;//指抓取时间间隔
gp.GrabMode = XDictGrabModeEnum.XDictGrabMouse;//设定取词的属性
gp.GrabEnabled = true;//是否取词的属性
gp.AdviseGrab(this);
}
//接口的实现
int IXDictGrabSink.QueryWord(string WordString, int lCursorX, int
lCursorY, string SentenceString, ref int lLoc, ref int
lStart)
{
//1 得到屏幕上鼠标位置的 字符串
string
tmpStr=SentenceString;//鼠标所在语句
string tmpStr2 = tmpStr;
////2 取得其中最后的汉字序号
//last_hz_no = last_hanzi(tmpStr2);//得到字符串中(中间可能有字母和 数字
),最后的汉字序号,它要变成全拼,开始的都为声母
//3 将字符串转换为 ,开始的都为声母,最后的 为全拼,字母数字保留 不变
string tmpStr3 = hz_pinyin(tmpStr, "", true);//汉字变成对应的拼音;
this.textBox1.Text = tmpStr + " " + tmpStr3;//汉字 +
对应的拼音
// 放到剪切板内共外部使用
Clipboard.Clear();
Clipboard.SetText(tmpStr + " " + tmpStr3);
//this.textBox1.Text = SentenceString.Substring(lLoc +
1,1);//鼠标所在字符
return 1;
}
private void button1_Click(object sender, EventArgs e)
{
string tmpStr = "",chs="酐2阿h?*";
string tt;
for (int i = (pyValue.Length - 1); i >= 0;
i--)
{//十进制 --> 十六进制
tt = "0x" + pyValue[i].ToString("X").Substring(4) + "," + pyName[i]
+ "=";
tmpStr += tt;
// this.listBox1.Items.Add(tt);
}
this.textBox1.Text = tmpStr;
}
# region 一级汉字 , 及其对应的拼音,多对应多
/// <summary>
/// 汉字的机内码数组
/// </summary>
private static int[] pyValue = new int[]
{
-20319,-20317,-20304,-20295,-20292,-20283,-20265,-20257,-20242,-20230,-20051,-20036,
-20032,-20026,-20002,-19990,-19986,-19982,-19976,-19805,-19784,-19775,-19774,-19763,
-19756,-19751,-19746,-19741,-19739,-19728,-19725,-19715,-19540,-19531,-19525,-19515,
-19500,-19484,-19479,-19467,-19289,-19288,-19281,-19275,-19270,-19263,-19261,-19249,
-19243,-19242,-19238,-19235,-19227,-19224,-19218,-19212,-19038,-19023,-19018,-19006,
-19003,-18996,-18977,-18961,-18952,-18783,-18774,-18773,-18763,-18756,-18741,-18735,
-18731,-18722,-18710,-18697,-18696,-18526,-18518,-18501,-18490,-18478,-18463,-18448,
-18447,-18446,-18239,-18237,-18231,-18220,-18211,-18201,-18184,-18183,
-18181,-18012,
-17997,-17988,-17970,-17964,-17961,-17950,-17947,-17931,-17928,-17922,-17759,-17752,
-17733,-17730,-17721,-17703,-17701,-17697,-17692,-17683,-17676,-17496,-17487,-17482,
-17468,-17454,-17433,-17427,-17417,-17202,-17185,-16983,-16970,-16942,-16915,-16733,
-16708,-16706,-16689,-16664,-16657,-16647,-16474,-16470,-16465,-16459,-16452,-16448,
-16433,-16429,-16427,-16423,-16419,-16412,-16407,-16403,-16401,-16393,-16220,-16216,
-16212,-16205,-16202,-16187,-16180,-16171,-16169,-16158,-16155,-15959,-15958,-15944,
-15933,-15920,-15915,-15903,-15889,-15878,-15707,-15701,-15681,-15667,-15661,-15659,
-15652,-15640,-15631,-15625,-15454,-15448,-15436,-15435,-15419,-15416,-15408,-15394,
-15385,-15377,-15375,-15369,-15363,-15362,-15183,-15180,-15165,-15158,-15153,-15150,