中英文輸入法的自動切換
2019-09-06 23:33:30
供稿:網(wǎng)友
前言:
在開發(fā)數(shù)據(jù)庫程序時,常常需要輸入中文和英文,為此,操作員不得不在兩者之間不斷切換,能不能實現(xiàn)中英文輸入法的自動切換呢?即在需要輸入中文的地方系統(tǒng)打開中文輸入法,在需要輸入英文的地方系統(tǒng)自動關(guān)閉中文輸入法,回到英文輸入法。本人在開發(fā)課程輸入模塊時,根據(jù)操作員的實際要求利用c++ builder5實現(xiàn)了中英文輸入法的自動切換功能,每個操作員可以根據(jù)他的中文輸入法習(xí)慣自己定制他所習(xí)慣的中文輸入法,從而真正實現(xiàn)了多用戶中英文輸入法的自動切換。
程序設(shè)計思路:
每個輸入控件有兩個屬性imemode和imename,其中imemode表明當(dāng)前的輸入法,與中國有關(guān)的幾個值分別為:imdisable, imclose, imopen, imdontcare , imchinese。若將imemode屬性設(shè)置為imchinese表明此控件的輸入法為中文,而imename屬性則反映了是何種中文輸入法;將imemode設(shè)置為imclose,則可以關(guān)閉已經(jīng)打開的中文輸入法,回到英文輸入法狀態(tài)。由于每個操作員的中文輸入法習(xí)慣不一樣,不能再程序中指定imename,所以需要在運行階段動態(tài)指定輸入控件的imename屬性值。
程序?qū)崿F(xiàn):
c++ builder有一全局變量screen,其屬性ime反映的系統(tǒng)的輸入法。程序中首先獲取系統(tǒng)安裝的輸入法,用戶根據(jù)他的喜好選擇他所喜歡的中文輸入法,將用戶的選擇寫入一ini文件中。在需要切換到中文輸入的地方從此ini文件讀取數(shù)據(jù),瓶將此值賦給輸入控件的imename,從而實現(xiàn)了動態(tài)指定imename。
n 新建一form,命名為form_ime,在form_ime上放一combox控件combox1用來獲取系統(tǒng)的輸入法,再拖兩個button控件button1和button2,設(shè)置其caption屬性分別為"修改"和"關(guān)閉"。
程序源代碼如下:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "ime.h"
#include
#include "dm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "statusbarex"
#pragma resource "*.dfm"
tform_ime *form_ime;
//---------------------------------------------------------------------------
__fastcall tform_ime::tform_ime(tcomponent* owner)
: tform(owner)
{
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::formshow(tobject *sender)
{
//獲取系統(tǒng)的輸入法,并賦給combox1->items
combobox1->items->assign(screen->imes);
//打開imesetup.ini文件,若不存在,則自動創(chuàng)建此文件----
tinifile *pinifile = new
tinifile(extractfilepath(application->exename)+"imesetup.ini");
//讀取以前保存的輸入法名稱,將顯示在combox1框中
combobox1->text=pinifile->readstring("ime", "chinese", "");
delete pinifile;
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::formclose(tobject *sender, tcloseaction &action)
{
action=cafree; //退出時自動釋放form所占的內(nèi)存空間
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::button2click(tobject *sender)
{
close();
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::button1click(tobject *sender)
{
//若用戶重新指定輸入法,將選擇的輸入法重新寫回到imesetup.ini文件
tinifile *pinifile = new
tinifile(extractfilepath(application->exename)+"imesetup.ini");
pinifile->writestring("ime", "chinese", combobox1->text);
delete pinifile;
//同時將所選擇的輸入法賦給數(shù)據(jù)模塊dm1中的chimename變量
dm1->chimename=combobox1->text;
//顯示中文提示框,表明默認中文輸入法修改成功
messageboxex(handle,"默認中文輸入法修改成功",
this->caption.c_str(),mb_iconinformation+mb_ok,0x0404);
}
//---------------------------------------------------------------------------
n 在數(shù)據(jù)模塊的構(gòu)造函數(shù)中讀取imesetup.ini文件,將用戶選擇的輸入法賦給chimename變量
__fastcall tdm1::tdm1(tcomponent* owner)
: tdatamodule(owner)
{
//---- 讀取默認中文輸入法-----
tinifile *pinifile = new
tinifile(extractfilepath(application->exename)+"imesetup.ini");
chimename=pinifile->readstring("ime", "chinese", "");
delete pinifile;
}
//-----------------------------------------
n 在課程輸入模塊中,在formshow()事件中指定輸入控件的中文輸入法,其它需要輸入英文的控件在設(shè)計階段可指定其imemode=imclose;
//---指定課程名稱默認中文輸入法-----
void __fastcall tcourseform::formshow(tobject *sender)
{
dbedit2->imename=dm1->chimename;
dbgrid1->columns->items[1]->imename=dm1->chimename;
}
//-----------------------------------------------------
其它的輸入模塊中需要用到中文輸入的地方都可使用此種方法,簡單實用
編譯運行程序,感受一下中英文輸入法的自動切換感覺吧!
中英文輸入法的自動切換
--------------------------------------------------------------------------------
http://tech.sina.com.cn 2000/11/15 軟件世界 駱名群
前言:
在開發(fā)數(shù)據(jù)庫程序時,常常需要輸入中文和英文,為此,操作員不得不在兩者之間不斷切換,能不能實現(xiàn)中英文輸入法的自動切換呢?即在需要輸入中文的地方系統(tǒng)打開中文輸入法,在需要輸入英文的地方系統(tǒng)自動關(guān)閉中文輸入法,回到英文輸入法。本人在開發(fā)課程輸入模塊時,根據(jù)操作員的實際要求利用c++ builder5實現(xiàn)了中英文輸入法的自動切換功能,每個操作員可以根據(jù)他的中文輸入法習(xí)慣自己定制他所習(xí)慣的中文輸入法,從而真正實現(xiàn)了多用戶中英文輸入法的自動切換。
程序設(shè)計思路:
每個輸入控件有兩個屬性imemode和imename,其中imemode表明當(dāng)前的輸入法,與中國有關(guān)的幾個值分別為:imdisable, imclose, imopen, imdontcare , imchinese。若將imemode屬性設(shè)置為imchinese表明此控件的輸入法為中文,而imename屬性則反映了是何種中文輸入法;將imemode設(shè)置為imclose,則可以關(guān)閉已經(jīng)打開的中文輸入法,回到英文輸入法狀態(tài)。由于每個操作員的中文輸入法習(xí)慣不一樣,不能再程序中指定imename,所以需要在運行階段動態(tài)指定輸入控件的imename屬性值。
程序?qū)崿F(xiàn):
c++ builder有一全局變量screen,其屬性ime反映的系統(tǒng)的輸入法。程序中首先獲取系統(tǒng)安裝的輸入法,用戶根據(jù)他的喜好選擇他所喜歡的中文輸入法,將用戶的選擇寫入一ini文件中。在需要切換到中文輸入的地方從此ini文件讀取數(shù)據(jù),瓶將此值賦給輸入控件的imename,從而實現(xiàn)了動態(tài)指定imename。
n 新建一form,命名為form_ime,在form_ime上放一combox控件combox1用來獲取系統(tǒng)的輸入法,再拖兩個button控件button1和button2,設(shè)置其caption屬性分別為"修改"和"關(guān)閉"。
程序源代碼如下:
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "ime.h"
#include
#include "dm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "statusbarex"
#pragma resource "*.dfm"
tform_ime *form_ime;
//---------------------------------------------------------------------------
__fastcall tform_ime::tform_ime(tcomponent* owner)
: tform(owner)
{
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::formshow(tobject *sender)
{
//獲取系統(tǒng)的輸入法,并賦給combox1->items
combobox1->items->assign(screen->imes);
//打開imesetup.ini文件,若不存在,則自動創(chuàng)建此文件----
tinifile *pinifile = new
tinifile(extractfilepath(application->exename)+"imesetup.ini");
//讀取以前保存的輸入法名稱,將顯示在combox1框中
combobox1->text=pinifile->readstring("ime", "chinese", "");
delete pinifile;
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::formclose(tobject *sender, tcloseaction &action)
{
action=cafree; //退出時自動釋放form所占的內(nèi)存空間
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::button2click(tobject *sender)
{
close();
}
//---------------------------------------------------------------------------
void __fastcall tform_ime::button1click(tobject *sender)
{
//若用戶重新指定輸入法,將選擇的輸入法重新寫回到imesetup.ini文件
tinifile *pinifile = new
tinifile(extractfilepath(application->exename)+"imesetup.ini");
pinifile->writestring("ime", "chinese", combobox1->text);
delete pinifile;
//同時將所選擇的輸入法賦給數(shù)據(jù)模塊dm1中的chimename變量
dm1->chimename=combobox1->text;
//顯示中文提示框,表明默認中文輸入法修改成功
messageboxex(handle,"默認中文輸入法修改成功",
this->caption.c_str(),mb_iconinformation+mb_ok,0x0404);
}
//---------------------------------------------------------------------------
n 在數(shù)據(jù)模塊的構(gòu)造函數(shù)中讀取imesetup.ini文件,將用戶選擇的輸入法賦給chimename變量
__fastcall tdm1::tdm1(tcomponent* owner)
: tdatamodule(owner)
{
//---- 讀取默認中文輸入法-----
tinifile *pinifile = new
tinifile(extractfilepath(application->exename)+"imesetup.ini");
chimename=pinifile->readstring("ime", "chinese", "");
delete pinifile;
}
//-----------------------------------------
n 在課程輸入模塊中,在formshow()事件中指定輸入控件的中文輸入法,其它需要輸入英文的控件在設(shè)計階段可指定其imemode=imclose;
//---指定課程名稱默認中文輸入法-----
void __fastcall tcourseform::formshow(tobject *sender)
{
dbedit2->imename=dm1->chimename;
dbgrid1->columns->items[1]->imename=dm1->chimename;
}
//-----------------------------------------------------
其它的輸入模塊中需要用到中文輸入的地方都可使用此種方法,簡單實用
編譯運行程序,感受一下中英文輸入法的自動切換感覺吧!