信息发布软件,b2b软件,广告发布软件

标题: Delphi程序实现直接用中文输入的超级好方法 [打印本页]

作者: 群发软件    时间: 2017-8-11 22:52
标题: Delphi程序实现直接用中文输入的超级好方法
本帖最后由 群发软件 于 2017-8-11 23:05 编辑

Delphi程序实现直接用中文输入的超级好方法

 BorlandDelphi以其强大的功能及和高效的可 视化开发环境为广大程序设计员所青睐。尤其是它封装了WINDOWSAPI函数,能方便地利 用WINDOWS资源,大大加快了程序开发速度。

  在平时的计算机操作中,中文输入是不可避免的。 使用者可能喜欢不同的中文输入法(inputmethodeditor,简称IME),这就不得不经常点击 任务栏中的中文图标或用CTRL+Space,CTRL+Shift热键切换,初学者用起来很不方便。针对 这一问题,可以在开发软件时,在程序中设置用户喜欢的中文输入法,方便用户的使用。Delphi 中只有少数控件如TEdit支持IME,而且该功能不强,不能在运行时更改输入法。

  笔者通过实践和摸索,查找了相关的IME资料,利 用了WINDOWSAPI函数,实现了IME的功能。

  常用函数有:

API函数:BOOLImmSimulateHotKey

(HWNDhWnd,DWORDdwHotKeyID);//模拟热键

其中Hwnd为程序窗口的句柄,dwHotHKeyID

为模拟的热键,若成功则返回True

HKLGetKeyboardLayout(DWORDdwLayout);

//获得当前键盘状态


BOOLImmIsIME(HKLhKL);

//判断当前是否处于中文输入状态,若是则返回True

自定义函数:

打开相应输入法:OpenIme(imename:string),

例OpenIme(全拼输入法);

关闭中文输入法:CloseIme;

以下是一个简单的例子,仅起参考作用。

使用时uses中加上imm

具体的实现方法及源代码如下:

unitUnit1;

interface

uses

Windows,Messages,SysUtils,Classes,

  Graphics,Controls,Forms,Dialogs,

StdCtrls,Buttons,imm;

type

TForm1=class(TForm)

ComboBox1:TComboBox;

BitBtn1:TBitBtn;

BitBtn2:TBitBtn;

BitBtn3:TBitBtn;

procedureFormShow(Sender:TObject);

procedureOpenIme(imename:string);

procedurecloseIme;

procedureComboBox1Change(Sender:TObject);

procedureBitBtn1Click(Sender:TObject);

procedureBitBtn2Click(Sender:TObject);

procedureBitBtn3Click(Sender:TObject);

private

{Privatedeclarations}

public

{Publicdeclarations}

end;

var

Form1:TForm1;

implementation

{$R*.DFM}

procedureTForm1.FormShow(Sender:TObject);

var

j:integer;

begin

forj:=0toscreen.imes.count-1do

begin

ComBoBox1.Items.Add(screen.Imes.strings[j]);

  //获取系统中已安装的中文输入法

end;

end;

procedureTform1.OpenIme(imename:string);

var

I:integer;

myhkl:hkl;

begin

ifImeName<>then

begin

ifScreen.Imes.Count<>0then

begin

I:=scr   .Imes.indexof(imename);

ifI>=0then

myhkl:=hkl(screen.Imes.objects);

activatekeyboardlayout(myhkl,

  KLF_ACTIVATE);//设置相应的输入法

end;

end;

end;

procedureTForm1.closeime;

var

myhkl:hkl;

begin

myhkl:=GetKeyBoardLayOut(0);

ifImmIsIME(myhkl)then

//判断是否在中文状态,若是则关闭它

immsimulateHotkey(handle,

IME_CHotKey_IME_NonIME_Toggle);

end;

procedureTForm1.ComboBox1Change(Sender:TObject);

begin

OpenIme(ComboBox1.Text);

end;

procedureTForm1.BitBtn1Click(Sender:TObject);

begin

immsimulateHotkey(handle,

IME_CHotKey_shape_Toggle);//切换半角和全角模式

end;

procedureTForm1.BitBtn2Click(Sender:TObject);

begin

immsimulateHotkey(handle,

IME_CHotKey_symbol_Toggle);

  //切换中文标点模式和英文标点模式

end;

procedureTForm1.BitBtn3Click(Sender:TObject);

begin

closeime;

end;

end.

在Delphi中一个汉字占两个字节,各种字符串处理函数,直接处理字符串中汉字时经常会发生乱码或不显示。我在处理时也是弄了半天,到处找资料,一下是找到的几种处理方法。在Delphi7下测试通过。

一.中文所用的字符全是双字节字符,英文所用的字节全是单字节字符,也就是mbSingleByte。本实例是用ByteType()函数返回字符串指定位置上的字符,如果不是mbSingleByte,则表示为双字节字符。主要代码如下:

procedure TForm1.Button1Click(Sender: TObject);
    var
     s,s1: String;
     i : Integer;
    begin
     s := Edit1.Text;
     i := 0;
     while i<Length(s) do
     begin
      if (ByteType(S,i) <> mbSingleByte) then
      begin
       s1 := s1+ Copy(s,i,2)+' ';
       i := i+2;
      end
      else
       i:= i+1;
     end;
     Label2.Caption := Trim(s1);
    end;

二.本实例是用Length()函数来获取字符串的长度,用Ord()函数来获取每个字符的ASCII码,当ASCII码的值大于$7F时,表示该字符是汉字的前一个字节,并用Copy()函数来获取当前的汉字。主要代码如下:

procedure TForm1.Button2Click(Sender: TObject);
var
     str,s,sj: String;
     i: integer;
    begin
     str := trim(Edit1.Text);
     i := 0;
     while i< Length(str) do
     begin
      if ord(str) > $7F then
      begin
       s := Copy(str,i,2);
       sj :=sj+ '"'+s+'"';
       i := i+2;
      end
      else  i:=i+1;
     end;
     Label2.Caption := '在字符串中含有汉字'+sj;
    end;
三用widestring类型。widestring处理汉字时汉字按一个字符算。代码如下:

procedure TForm1.Button3Click(Sender: TObject);
var
  str:string;
  badword,filtstr:widestring;
  filtcount,i:integer;
  msg:widestring;
begin
  msg:=edit1.Text;
  for i:=1 to length(msg) do
   begin
     if Pos((msg),filtstr)<>0 then
        begin
          badword:=badword+msg+',';
          msg:=StringReplace(msg,msg,'*', []);
          filtcount:=filtcount+1;
        end;
   end;
   showmessage(msg);
end;


/1,函数代码

{

  判断字符串是否包含汉字
  // judgeStr:要判断的字符串
  //posInt:第一个汉字位置
}
function TForm2.IsHaveChinese(judgeStr: string; var posInt: integer): boolean;
var
  p: PWideChar; // 要判断的字符
  count: integer; // 包含汉字位置
  isHave: boolean; // 是否包含汉字返回值
begin

  isHave := false; // 是否包含汉字返回值默认为false
  count := 1; // 包含汉字位置默认为1

  p := PWideChar(judgeStr); // 把要判断字符串转换

  // 循环判断每个字符
  while p^ <> #0 do
  begin
    case p^ of
      #$4E00 .. #$9FA5:
        begin
          isHave := true; // 设置是否包含汉字返回值为true
          posInt := count; // 设置包含汉字位置
          break; // 退出循环
        end;

    end;

    Inc(p);
    Inc(count); // 包含汉字位置递增
  end;

  result := isHave;

end;


//2,例子:

procedure TForm2.Button3Click(Sender: TObject);
var
  testStr1, testStr2: string;
  posInt: integer;
begin
  testStr1 := '12345';
  testStr2 := '123汉字45';

  if self.IsHaveChinese(testStr1, posInt) = true then
  begin
    ShowMessage(testStr1 + ' 包含汉字 :' + inttostr(posInt));
  end
  else
  begin
    ShowMessage(testStr1 + ' 不包含汉字');
  end;

  if self.IsHaveChinese(testStr2, posInt) = true then
  begin
    ShowMessage(testStr2 + ' 包含汉字 :' + inttostr(posInt));
  end
  else
  begin
    ShowMessage(testStr2 + ' 不包含汉字');
  end;
end;



作者: daoke    时间: 2017-8-15 00:23
很专业。。。
作者: a001hao    时间: 2017-8-15 02:08
很好,定制的效果也很好。
作者: dongwuhua    时间: 2017-8-16 00:54
,大爱,喜欢,是我想要的,不错,值得购买,
作者: 小痞子    时间: 2017-8-18 03:05
心不懂就问他就告诉也不嫌弃烦不给好评等啥呢必须好评太给力了好多客人都说我家店铺好看。从来没有碰到过
作者: ziyang701    时间: 2017-8-18 16:22
感谢店家的细心指导!!谢谢!
作者: cd0010    时间: 2017-8-19 00:23
很好,东西很好用!
作者: yerface    时间: 2017-8-22 19:11
好,网站做得也非常的漂亮,另外强调一下:17号技术员很不错,非常的负责任。为17号技术员赞一个。
作者: z2340868    时间: 2017-8-22 22:11
,服务亲切,客服指导很有耐心。设计很漂亮,很贴切,值得推荐啦!
作者: mmgg    时间: 2017-8-22 22:51
晚,价格合适,功能齐全,尤其是技术客服服务周到,花很少的钱做很大的事,合作愉快,希望以后更好合作
作者: huanyili998    时间: 2017-8-23 20:27
赞一个,卖家服务部错
作者: jiandao1    时间: 2017-8-26 06:06
力,好评!!!!
作者: 阿拉丁    时间: 2017-8-28 22:56
,而且服务很好。值得信赖。
作者: yiyi2014    时间: 2017-8-30 08:58
用心设计。。
作者: 万能群发    时间: 2017-9-3 04:37
效,沟通愉快!不错的店家!!!
作者: c19900420    时间: 2017-9-3 08:02
!发货速度快,很热情!很不错
作者: chenxue2015    时间: 2017-9-7 13:57
特意用了几天才来追加评论的,真心好用,大爱!!!
作者: loverun    时间: 2017-9-9 17:10
值!卖家服务相当棒!




欢迎光临 信息发布软件,b2b软件,广告发布软件 (http://www.postbbs.com/) Powered by Discuz! X3.2