中英文輸入法的自動切換
2019-09-06 23:33:30
供稿:網友
前言:
在開發數據庫程序時,常常需要輸入中文和英文,為此,操作員不得不在兩者之間不斷切換,能不能實現中英文輸入法的自動切換呢?即在需要輸入中文的地方系統打開中文輸入法,在需要輸入英文的地方系統自動關閉中文輸入法,回到英文輸入法。本人在開發課程輸入模塊時,根據操作員的實際要求利用c++ builder5實現了中英文輸入法的自動切換功能,每個操作員可以根據他的中文輸入法習慣自己定制他所習慣的中文輸入法,從而真正實現了多用戶中英文輸入法的自動切換。
程序設計思路:
每個輸入控件有兩個屬性imemode和imename,其中imemode表明當前的輸入法,與中國有關的幾個值分別為:imdisable, imclose, imopen, imdontcare , imchinese。若將imemode屬性設置為imchinese表明此控件的輸入法為中文,而imename屬性則反映了是何種中文輸入法;將imemode設置為imclose,則可以關閉已經打開的中文輸入法,回到英文輸入法狀態。由于每個操作員的中文輸入法習慣不一樣,不能再程序中指定imename,所以需要在運行階段動態指定輸入控件的imename屬性值。
程序實現:
c++ builder有一全局變量screen,其屬性ime反映的系統的輸入法。程序中首先獲取系統安裝的輸入法,用戶根據他的喜好選擇他所喜歡的中文輸入法,將用戶的選擇寫入一ini文件中。在需要切換到中文輸入的地方從此ini文件讀取數據,瓶將此值賦給輸入控件的imename,從而實現了動態指定imename。
n 新建一form,命名為form_ime,在form_ime上放一combox控件combox1用來獲取系統的輸入法,再拖兩個button控件button1和button2,設置其caption屬性分別為"修改"和"關閉"。
程序源代碼如下:
//---------------------------------------------------------------------------
#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)
{
//獲取系統的輸入法,并賦給combox1->items
combobox1->items->assign(screen->imes);
//打開imesetup.ini文件,若不存在,則自動創建此文件----
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所占的內存空間
}
//---------------------------------------------------------------------------
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;
//同時將所選擇的輸入法賦給數據模塊dm1中的chimename變量
dm1->chimename=combobox1->text;
//顯示中文提示框,表明默認中文輸入法修改成功
messageboxex(handle,"默認中文輸入法修改成功",
this->caption.c_str(),mb_iconinformation+mb_ok,0x0404);
}
//---------------------------------------------------------------------------
n 在數據模塊的構造函數中讀取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()事件中指定輸入控件的中文輸入法,其它需要輸入英文的控件在設計階段可指定其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 軟件世界 駱名群
前言:
在開發數據庫程序時,常常需要輸入中文和英文,為此,操作員不得不在兩者之間不斷切換,能不能實現中英文輸入法的自動切換呢?即在需要輸入中文的地方系統打開中文輸入法,在需要輸入英文的地方系統自動關閉中文輸入法,回到英文輸入法。本人在開發課程輸入模塊時,根據操作員的實際要求利用c++ builder5實現了中英文輸入法的自動切換功能,每個操作員可以根據他的中文輸入法習慣自己定制他所習慣的中文輸入法,從而真正實現了多用戶中英文輸入法的自動切換。
程序設計思路:
每個輸入控件有兩個屬性imemode和imename,其中imemode表明當前的輸入法,與中國有關的幾個值分別為:imdisable, imclose, imopen, imdontcare , imchinese。若將imemode屬性設置為imchinese表明此控件的輸入法為中文,而imename屬性則反映了是何種中文輸入法;將imemode設置為imclose,則可以關閉已經打開的中文輸入法,回到英文輸入法狀態。由于每個操作員的中文輸入法習慣不一樣,不能再程序中指定imename,所以需要在運行階段動態指定輸入控件的imename屬性值。
程序實現:
c++ builder有一全局變量screen,其屬性ime反映的系統的輸入法。程序中首先獲取系統安裝的輸入法,用戶根據他的喜好選擇他所喜歡的中文輸入法,將用戶的選擇寫入一ini文件中。在需要切換到中文輸入的地方從此ini文件讀取數據,瓶將此值賦給輸入控件的imename,從而實現了動態指定imename。
n 新建一form,命名為form_ime,在form_ime上放一combox控件combox1用來獲取系統的輸入法,再拖兩個button控件button1和button2,設置其caption屬性分別為"修改"和"關閉"。
程序源代碼如下:
//---------------------------------------------------------------------------
#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)
{
//獲取系統的輸入法,并賦給combox1->items
combobox1->items->assign(screen->imes);
//打開imesetup.ini文件,若不存在,則自動創建此文件----
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所占的內存空間
}
//---------------------------------------------------------------------------
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;
//同時將所選擇的輸入法賦給數據模塊dm1中的chimename變量
dm1->chimename=combobox1->text;
//顯示中文提示框,表明默認中文輸入法修改成功
messageboxex(handle,"默認中文輸入法修改成功",
this->caption.c_str(),mb_iconinformation+mb_ok,0x0404);
}
//---------------------------------------------------------------------------
n 在數據模塊的構造函數中讀取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()事件中指定輸入控件的中文輸入法,其它需要輸入英文的控件在設計階段可指定其imemode=imclose;
//---指定課程名稱默認中文輸入法-----
void __fastcall tcourseform::formshow(tobject *sender)
{
dbedit2->imename=dm1->chimename;
dbgrid1->columns->items[1]->imename=dm1->chimename;
}
//-----------------------------------------------------
其它的輸入模塊中需要用到中文輸入的地方都可使用此種方法,簡單實用
編譯運行程序,感受一下中英文輸入法的自動切換感覺吧!