模拟Nokia手机输入的编辑框[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 mindel 的 blog

{
功能:模拟nokia手机的输入法控件,继承自tcustomedit
作者:mindel
支持:mindel # 163.com 
最后更新:2004/12/11
}
unit nokiaedit;

interface

uses
  sysutils, classes, controls, stdctrls, extctrls, dialogs;

type
  keysetint = -1..9; //-1表示发生错误

  tnokiaedit = class(tcustomedit)
  private
    finterval: integer;
    fkeyset: tstrings;
    procedure setkeyset(const value: tstrings);
    function gettimerenabled: boolean;
    procedure settimerenabled(const value: boolean);
    function gettextlength: integer;
    { private declarations }
  protected
    { protected declarations }
    ftimer: ttimer;
    procedure ontimer(sender: tobject);virtual;
    property timerenabled: boolean read gettimerenabled write settimerenabled;
    procedure keypress(var key: char); override;
    function indexbychar(const key: char): keysetint;
    function searchnextchar(const index: keysetint; key: char): char;
    function searchfirstchar(const index: keysetint): char;
    function endofchar(const value: string): char;
  public
    { public declarations }
    constructor create(aowner: tcomponent);override;
    destructor destroy;override;

    property textlength: integer read gettextlength;
  published
    { published declarations }
    property maxlength default 1;
    property interval: integer read finterval write finterval;
    property keyset: tstrings read fkeyset write setkeyset;
    property font;
  end;

procedure register;

implementation

procedure register;
begin
  registercomponents('coolslob', [tnokiaedit]);
end;

{ tnokiaedit }

constructor tnokiaedit.create(aowner: tcomponent);
begin
  inherited;
  fkeyset := tstringlist.create;
  ftimer := ttimer.create(self);
  ftimer.ontimer := ontimer;
end;

destructor tnokiaedit.destroy;
begin
  fkeyset.free;
  fkeyset := nil;
  inherited;
end;

function tnokiaedit.endofchar(const value: string): char;
begin
  if value = '' then result := #0
  else result := value[length(value)];
end;

function tnokiaedit.gettextlength: integer;
begin
  result := length(trim(text));
end;

function tnokiaedit.gettimerenabled: boolean;
begin
  result := ftimer.enabled;
end;

function tnokiaedit.indexbychar(const key: char): keysetint;
//
var
  i: integer;
begin
  result := -1;
  if not assigned(fkeyset) then exit;
  for i := 0 to fkeyset.count - 1 do
  begin
    if pos(key, fkeyset.strings[i]) > 0 then
    begin
      result := i;
      break;
    end;
  end;
end;

procedure tnokiaedit.keypress(var key: char);
var
  curindex, newindex: keysetint;
  s: string;
  curchar: char;
  selected: boolean;
begin
  inherited;
  if key in ['0'..'9'] then
  begin
    ftimer.enabled := false; 
    if textlength = 0 then curindex := -1
    else curindex := indexbychar(text[length(text)]);
    newindex := ord(key) - 48;
    if curindex = newindex then
    begin
      curchar := endofchar(text);
      s := text;
      selected := wordbool(sellength);
      delete(s, selstart + 1, sellength);
      text := s;
      if (not selected)or(curchar='') then text := text + searchfirstchar(curindex)
      else text := text + searchnextchar(curindex, curchar);
    end
    else
      text := text + searchfirstchar(newindex);

    selstart := length(text) - 1;
    sellength := 1;

本文关键:模拟Nokia手机输入的编辑框
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top