在C#應用程序中控制輸入法
2024-07-21 02:29:42
供稿:網友
在windows系統一般都安裝了至少三種輸入法,在輸入數據時常常會切換輸入法,雖然windows系統提供了切換快捷健,但對輸入工作還是帶來了不少麻煩。如果在應用程序中為用戶提供智能輸入法自動切換,那么這樣的應用程序就顯得更加專業、更加具有競爭力。不知你可用過access,在表數據輸入時access自動切換輸入法,很酷吧,現在你也可以實現這一切。如果也想你的程式也酷一下的話,請繼續...
為了控制輸入法,.net類庫在system.windows.forms.inputlanguage類中提供了支持。我計劃先花一點時間講述inputlanguage類的功能,隨后舉一個實例inputlanguagerichedit。
1、inputlanguage類是一個密封類,它提供了許多方法和屬性實現輸入法管理功能,這其中有幾個屬性尤其重要,我將在下面逐一講解,如果你想全面了解類的全部方法和屬性,請瀏覽msdn。
public static inputlanguage currentinputlanguage {get; set;}
//獲得或設置當前線程的輸入法。
public static inputlanguage defaultinputlanguage {get;}
//獲得缺省輸入法。
public static inputlanguagecollection installedinputlanguages{get;}
//獲得系統輸入法集。可以通過這個容器對象列舉系統當前安裝的輸入法列表。
public string layoutname {get;}
//獲得輸入法在系統托盤中的注冊名稱。
......
2、我們已經研究了inputlanguage類提供的幾個重要屬性了,現在可以開始動手在應用開發中應用inputlanguage類。我想創建一個.net window form的系統程序,用一個列表框列舉當前系統安裝的所有輸入法,通過改變列表框的選項自動改變當前線程的輸入法。同時還實現了根據桌面托盤中輸入法的變化來改變列表框的選項。
(1)、新建項目 --> 選擇"visual c#項目" --> 輸入項目名:inputlanguagerichedit。
(2)、在"工具箱"中拖一個richtextbox控件,命名為:richtextbox1;一個combobox控件,命名為:combobox1;一個button控件,命名為:but_exit。
(3)、用下面的代碼代替private void initializecomponent()。
{
this.combobox1 = new system.windows.forms.combobox();
this.richtextbox1 = new system.windows.forms.richtextbox();
this.but_eixt = new system.windows.forms.button();
this.suspendlayout();
//
// combobox1
//
this.combobox1.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;
this.combobox1.dropdownwidth = 160;
this.combobox1.location = new system.drawing.point(8, 232);
this.combobox1.name = "combobox1";
this.combobox1.size = new system.drawing.size(168, 20);
this.combobox1.tabindex = 1;
this.combobox1.selectedindexchanged += new system.eventhandler (this.combobox1_selectedindexchanged);
//
// richtextbox1
//
this.richtextbox1.dock = system.windows.forms.dockstyle.top;
this.richtextbox1.name = "richtextbox1";
this.richtextbox1.size = new system.drawing.size(292, 208);
this.richtextbox1.tabindex = 0;
this.richtextbox1.text = "";
//
// but_eixt
//
this.but_eixt.location = new system.drawing.point(200, 232);
this.but_eixt.name = "but_eixt";
this.but_eixt.tabindex = 2;
this.but_eixt.text = "eixt";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(292, 273);
this.controls.addrange(new system.windows.forms.control[] {
this.but_eixt,this.combobox1,this.richtextbox1});
this.name = "form1";
this.text = "form1";
this.load += new system.eventhandler(this.form1_load);
this.inputlanguagechanged += new system.windows.forms.inputlanguagechangedeventhandler (this.changeinput);
this.resumelayout(false);
}
(4)、插入下面代碼:
private void form1_load(object sender, system.eventargs e)
{
inputlanguagecollection ilc = inputlanguage.installedinputlanguages;
foreach ( inputlanguage il in ilc )
{
combobox1.items.add( il.layoutname );
}
combobox1.selectedindex = inputlanguage.installedinputlanguages.indexof ( inputlanguage.currentinputlanguage ) ;
}
private void combobox1_selectedindexchanged(object sender, system.eventargs e)
{
inputlanguage il = inputlanguage.installedinputlanguages[ combobox1.selectedindex ];
inputlanguage.currentinputlanguage = il;
}
private void changeinput(object sender, system.windows.forms.inputlanguagechangedeventargs e)
{
inputlanguage il = e.inputlanguage ;
int i = inputlanguage.installedinputlanguages.indexof( il );
if( i >= 0 && i < inputlanguage.installedinputlanguages.count )
{
combobox1.selectedindex = i ;
}
}
private void but_eixt_click(object sender, system.eventargs e)
{
application.exit();
}