中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi Delphi程序输入法自动切换最简单的方式

来源:站内 关于:bill 发布时间:2007-06-27   [收藏] [推荐]


 前言:
当我们的软件交付用户使用时,特别涉及到大量的数据输入处理时,用户需要频繁的在中英文之间切换,而且用户使用的输入法也不同,有的是 五笔,有的是拼音等等.我们能否在软件中提供一个个性化的输入法选择?当用户选择自己喜欢的输入法时,程序中该输入中文的地方,全部自动切换为中文,该输入英文的地方自动切换为英文.
   这个实现方法比较多,但多很麻烦,因此,虽然很多程序员可以实现,但都感到非常麻烦而放弃!下面是本人的一个非常简单和好用的解决方法.
解决思路:
程序员只需要将窗体中该输入中文的控件的 imemode=imchinese,然后在每个窗体里 create(或active) 事件里调用本人编写的方法ChangeYouFormAllControlIme(frm)即可.在程序中提供一个用户输入法选项供用户选择自己喜欢的输入法,调用显示FrmImeNameList 窗体即可!总之,程序员只需要调用 一个公用 unit(含有窗体的unit) 下的方法: ChangeYouFormAllControlIme(YFormName:TWinControl), 调用显示窗体 FrmImeNameList
实现方法: unit 名字 UnitImeManager 包含的窗体名字 FrmImeNameList
      
界面操作的代码:
    procedure TFrmImeNameList.FormCreate(Sender: TObject); //列表框加载系统输入法
begin
ListBox1.Items:=screen.Imes;
end;
      procedure TFrmImeNameList.BitBtn1Click(Sender: TObject);//保存用户选择的输入法存放到系统注册表里面
var
Reg:TRegistry;
CustImeName:string;
begin
reg:=TRegistry.Create;
reg.RootKey:=HKEY_LOCAL_MACHINE;
if ListBox1.ItemIndex<>-1 then
 CustImeName:=ListBox1.Items[ListBox1.ItemIndex]
 else
 CustImeName:='中文 (简体) - 智能 ABC';
try
 if Reg.OpenKey('\Software\IMeCustom', True) then Reg.WriteString('CustomIMeName',CustImeName);
finally
 reg.CloseKey;
 reg.Free;
end;
FrmImeNameList.Close;
end;
   
公用 unit的方法:
1         更改相应窗体中所有的控件的输入法
   Procedure ChangeYouFormAllControlIme(YFormName:TWinControl); // 需要更改输入法的窗体名称
var
i:integer;
ChildControl:TControl;
Reg:TRegistry;
YouFormOrOTher:Twincontrol;
begin
YouFormOrOTher:=YFormName;
reg:=TRegistry.Create;
//读取注册表保存的用户选择的输入法,用单元全局变量 StrImeName 保存
reg.RootKey:=HKEY_LOCAL_MACHINE;
try
 if Reg.OpenKey('\Software\IMeCustom',false)=true then
   StrImeName:=reg.ReadString('CustomIMeName');
finally
 reg.CloseKey;
 reg.Free;
end;
//调用方法 JugeClassType(ChildControl) 更改窗体中控件的 ImeMode
for i:=0 to YouFormOrOTher.ControlCount-1 do
 begin
   ChildControl:=YouFormOrOTher.Controls[i];
   JugeClassType(ChildControl);
          //如果控件包含控件,更改被包含的控件
         if ChildControl is TWinControl then ChangeYouFormAllControlIme(ChildControl as TWinControl);
 end;
end;
2         判断控件类别,更改其 imemode 属性为用户选择的输入法
   procedure JugeClassType(PClass:Tcontrol); //虽然麻烦,但必须具体判断,下面将 dephi 可更改的控件几乎都包含了,没有包含的是使用价值不大.
begin
if pclass is TEdit then //更改 Tedit 类控件 以下同理
 begin
   if TEdit(pclass).ImeMode=imchinese then
     TEdit(pclass).ImeName:=StrImeName
   else
     TEdit(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TMemo then
 begin
   if TMemo(pclass).ImeMode=imchinese then
     TMemo(pclass).ImeName:=StrImeName
   else
     TMemo(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TComboBox then
 begin
   if TComboBox(pclass).ImeMode=imchinese then
     TComboBox(pclass).ImeName:=StrImeName
   else
     TComboBox(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TRichEdit then
 begin
   if TRichEdit(pclass).ImeMode=imchinese then
     TRichEdit(pclass).ImeName:=StrImeName
  else
     TRichEdit(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TDBGrid then
 begin
   if TDBGrid(pclass).ImeMode=imchinese then
     TDBGrid(pclass).ImeName:=StrImeName
   else
     TDBGrid(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TDBEdit then
 begin
   if TDBEdit(pclass).ImeMode=imchinese then
     TDBEdit(pclass).ImeName:=StrImeName
   else
     TDBEdit(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TDbMemo then
 begin
   if TDbMemo(pclass).ImeMode=imchinese then
     TDbMemo(pclass).ImeName:=StrImeName
   else
     TDbMemo(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TDbcombobox then
 begin
   if TDbcombobox(pclass).ImeMode=imchinese then
     TDbcombobox(pclass).ImeName:=StrImeName
   else
     TDbcombobox(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is Tdblookupcombobox then
 begin
   if Tdblookupcombobox(pclass).ImeMode=imchinese then
     Tdblookupcombobox(pclass).ImeName:=StrImeName
   else
     Tdblookupcombobox(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is Tdbrichedit then
 begin
   if Tdbrichedit(pclass).ImeMode=imchinese then
     Tdbrichedit(pclass).ImeName:=StrImeName
   else
     Tdbrichedit(pclass).ImeMode:=imClose;
   exit;
 end;
if pclass is TMaskEdit then
 begin
   if TMaskEdit(pclass).ImeMode=imchinese then
     TMaskEdit(pclass).ImeName:=StrImeName
   else
     TMaskEdit(pclass).ImeMode:=imClose;
   exit;
 end;
end;
 

[浏览: 次]   
上一篇:delphi 汉字输入法的编程及使用   下一篇:delphi CodeGear在Borland中蛹化 能否化蝶
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi 消息是由谁来发出又由谁来完成
·delphi 程序缩小为任务右下角的小图标
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932