首页 > 开发 > 综合 > 正文

在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();
   }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表