一个简单T9输入法的实现(有代码)

文章难度:初学者<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

关于T9输入法的核心程序(由我朋友Nix提供),共两个文件,一个是NixInputMethod.h,还有一个是T9.c; 参见附录。

具体输入法的实现方法如下:

1. 首先创建一个对话框,然后在其中放入两个控件:IDC_EDIT1 IDC_WORD STATIC控件)

2. 为了实现在Edit中一边输入,STATIC中会出现智能的功能,所以我们的实现如下:

a) 重载函数PreTranslateMessage ,使其能够捕捉到键盘的所有事件,故其实现如下:

BOOL CMy11Dlg::PreTranslateMessage(MSG* pMsg)

{

try {

CWnd* pCtrl = GetFocus();

/*

* 可以通过Enter建进行光标焦点的切换

*/

if (pMsg->message == WM_KEYDOWN && GetAsyncKeyState(VK_TAB))

{

// get the char index of the caret position

int nPos = LOWORD(m_MyEditCtl.CharFromPos(m_MyEditCtl.GetCaretPos()));

// select zero chars

m_MyEditCtl.SetSel(nPos, nPos);

// then replace that selection with a TAB

m_MyEditCtl.ReplaceSel("\t", TRUE);

// no need to do a msg translation, so quit.

// that way no further processing gets done

return TRUE;

} else

if (GetAsyncKeyState(VK_RETURN)) // Enter

{

switch(pCtrl->GetDlgCtrlID()) {

case IDOK:

if (!reset) {

AfxMessageBox("OK");

reset = 1;

} else {

reset = 0;

GetDlgItem(IDC_STATIC)->SetFocus();

}

break;

default:

pMsg->wParam = VK_TAB;

}

} else

/*

* 通过123....选择第几个字

*/

if (pMsg->message == WM_KEYDOWN && (isdigit(pMsg->wParam) || (pMsg->wParam >64 && pMsg->wParam < 91))) {

CString str;

if (isdigit(pMsg->wParam)) {

GetDlgItem(IDC_WORD)->GetWindowText(str);

char* temp = str.GetBuffer(100);

char zi[3];

zi[0] = temp[2*(pMsg->wParam - 48)-2];

zi[1] = temp[2*(pMsg->wParam - 48)-1];

zi[2] = '\0';

pCtrl->SetWindowText(zi);

str.ReleaseBuffer();

return TRUE;

}

UpdateData();

pCtrl->GetWindowText(str);

str += (pMsg->wParam + 32);

CShincoInputMethod pIME;

GetDlgItem(IDC_WORD)->SetWindowText(pIME.GetInputString((unsigned char*)str.GetBuffer(100)));

str.ReleaseBuffer();

}

return CDialog::PreTranslateMessage(pMsg);

} catch (...) {

// AfxMessageBox("Error");

}

return TRUE;

}

这样基本就可以实现一个输入法了,如果你加上词库的话,就可以实现类似智能ABC之类的东西了,当然那样的话,你还需要多努力努力!

上面程序只是测试用的,你自己可以重新试试看输入法的实现,如果需要代码,请联系我。

附录:

// NixInputMethod.h: interface for the CShincoInputMethod class.

//

//

#if !defined(AFX_SHINCOINPUTMETHOD_H__40148888_4731_<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="4" unitname="a">4A</chmetcnv>49_87CC_<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="4" unitname="F">4F</chmetcnv>9CFCAF<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="77" unitname="a">77A</chmetcnv>5__INCLUDED_)

#define AFX_SHINCOINPUTMETHOD_H__40148888_4731_<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="4" unitname="a">4A</chmetcnv>49_87CC_<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="4" unitname="F">4F</chmetcnv>9CFCAF<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="77" unitname="a">77A</chmetcnv>5__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

class CNixInputMethod

{

public:

unsigned long py_mb_begin;

CString GetInputString(BYTE input_py_val[]);

BYTE *py_ime(BYTE input_py_val[]);

CNixInputMethod();

virtual ~CNixInputMethod();

};

#endif // !defined(AFX_SHINCOINPUTMETHOD_H__40148888_4731_<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="4" unitname="a">4A</chmetcnv>49_87CC_<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="4" unitname="F">4F</chmetcnv>9CFCAF<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="77" unitname="a">77A</chmetcnv>5__INCLUDED_)

/*

* T9.c

*/

#include "stdafx.h"

#include "NixInputMethod.h"

BYTE PY_mb_a[][255]=

{

{"阿啊吖锕嗄呵腌"},

{"哎哀唉埃挨皑癌矮蔼艾爱隘碍"},

{"安氨鞍俺岸按案胺暗"},

{"肮昂盎"},

{"凹敖熬翱袄傲奥澳懊"},

{"八巴叭扒吧芭疤捌笆拔跋把靶坝爸罢霸"},

{"白百佰柏摆败拜稗"},

{"扳班般颁斑搬板版办半伴扮拌绊瓣"},

{"邦帮梆绑榜膀蚌傍棒谤磅镑"},

{"包苞胞褒雹宝饱保堡报抱豹鲍暴爆剥薄瀑"},

{"卑杯悲碑北贝狈备背钡倍被惫焙辈"},

{"奔本苯笨夯"},

{"崩绷甭泵迸蹦"},

{"逼鼻比彼笔鄙币必毕闭庇毖陛毙敝痹蓖弊碧蔽壁避臂"},

{"边编鞭贬扁卞便变遍辨辩辫"},

{"彪标膘表"},

{"憋鳖别瘪"},

{"宾彬斌滨濒摈"},

{"冰兵丙秉柄炳饼并病"},

{"拨波玻钵脖菠播伯驳帛泊勃铂舶博渤搏箔膊卜"},

{"补哺捕不布步怖部埠簿"},

{"擦嚓礤"},

{"猜才材财裁采彩睬踩菜蔡"},

{"参餐残蚕惭惨灿"},

{"仓沧苍舱藏"},

{"操糙曹槽草"},

{"册侧厕测策"},

{"层蹭曾"},

{"叉插查茬茶搽察碴岔诧差刹"},

{"拆柴豺"},

{"掺搀谗馋缠蝉产铲阐颤"},

{"昌猖肠尝偿常厂场敞畅倡唱"},

{"抄钞超巢朝嘲潮吵炒绰"},

{"车扯彻掣撤澈"},

{"郴尘臣忱沉辰陈晨衬趁"},

{"称撑成呈承诚城乘惩程澄橙逞骋秤"},

{"吃痴弛池驰迟持尺侈齿耻斥赤炽翅"},

{"充冲虫崇宠"},

{"抽仇绸畴愁稠筹酬踌丑瞅臭"},

{"出初除厨滁锄雏橱躇础储楚处搐触矗畜"},

{""},

{"川穿传船椽喘串"},

{"闯疮窗床创"},

{"吹炊垂捶锤"},

{"春椿纯唇淳醇蠢"},

{""},

{"疵词茨瓷慈辞磁雌此次刺赐"},

{"囱从匆葱聪丛"},

{""},

{"粗促醋簇"},

{"蹿窜篡"},

{"崔催摧脆淬瘁粹翠"},

{"村存寸"},

{"搓磋撮挫措错"},

{"搭达答瘩打大"},

{"呆歹傣代带待怠殆贷袋逮戴"},

{"丹单担耽郸胆掸旦但诞弹惮淡蛋氮"},

{"当挡党荡档"},

{"刀导岛倒捣祷蹈到悼盗道稻"},

{"的地得德锝"},

{"灯登蹬等邓凳瞪"},

{"低堤滴狄迪敌涤笛嫡底抵地弟帝递第缔蒂"},

{"掂滇颠典点碘电佃甸店垫惦淀奠殿靛"},

{"刁叼凋碉雕吊钓掉"},

{"爹跌迭谍叠碟蝶"},

{"丁叮盯钉顶鼎订定锭"},

{""},

{"东冬董懂动冻侗恫栋洞"},

{"都兜斗抖陡豆逗痘"},

{"督毒读犊独堵赌睹妒杜肚度渡镀"},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值