通过网盘分享的文件:DelphiAutoComplete.rar

链接: https://pan.baidu.com/s/1l4oZIeS3SJgn5P8aLAjyBA 提取码: rrwz

uses System.StrUtils; //字符串函数

 声明两个全局变量

var
aStringList: TStringList; //读取 关键字
aMemoInput:string;       //当前 已输入 项
procedure TSearchReplaceDemoForm.FormCreate(Sender: TObject);
begin
 aStringList:=TStringList.Create;
 aStringList.LoadFromFile('keyWord.txt');  //从文件加载 关键字
end;
function TSearchReplaceDemoForm.tulaterIsSeparator(Car: Char): Boolean;
begin
  case Car of
    '.', ';', ',', ':', '¡', '!', '·', '"', '''', '^', '+', '-', '*', '/', '\', '¨', ' ',
    '`', '[', ']', '(', ')', 'º', 'ª', '{', '}', '?', '¿', '%', '=':
      Result := True;
    else
      Result := False;
  end;
end;

 拖一个ListBox,命名为ListBox_keyWords

procedure TSearchReplaceDemoForm.ListBox_keyWordsKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
 if ( key=VK_LEFT ) or ( key=VK_RIGHT )   then begin //   左右  键 不能响应,否则会 返回到SynMemo
       key:=0;
     exit;
  end;
end;
procedure TSearchReplaceDemoForm.ListBox_keyWordsKeyPress(Sender: TObject;
  var Key: Char);
var  tempStr:string;
begin
 if key=#27  then   //esc
     begin
     ListBox_keyWords.Visible:=false;
     SynEditor.SetFocus;
     aMemoInput:='';
     end;

  if key =#13  then  begin    //回车键

      tempStr:=  ListBox_keyWords.Items[ListBox_keyWords.ItemIndex] ;

      SynEditor.SelText := rightstr(  tempStr ,length(tempStr)-length(aMemoInput));
      ListBox_keyWords.Visible:=false;
      SynEditor.SetFocus;
      aMemoInput:='';
  end;
end
procedure TForm9.ListBox_keyWords_Refresh(aInput: string);
var i:integer;
pt: TPOint;
begin
if aInput='' then  begin  ListBox_keyWords.Visible:=false; exit; end;

 ListBox_keyWords.Items.Clear;
      for i := 0 to aStringList.Count -1 do begin

        if    sameText( leftStr( aStringList[i],length(aMemoInput))  ,aInput) then
        ListBox_keyWords.Items.Add(aStringList[i])
      end;


      if ListBox_keyWords.Items.Count>0 then      begin
      //================ListBox_keyWords 不能获得 焦点================-----------
        GetCaretPos(pt);
        ListBox_keyWords.Top  := SynEditor.Top + pt.y  + SynEditor.Font.Size*2;
        ListBox_keyWords.left := SynEditor.Left+ pt.x  +11;
        ListBox_keyWords.Visible:=True;
      //==================================================------
      end
      else  begin
       ListBox_keyWords.Visible:=false;
     //  aMemoInput:='';
      end;
end;

鼠标双击,选择

procedure TSearchReplaceDemoForm.ListBox_keyWordsDblClick(Sender: TObject);
begin
var    tempStr: string;
begin
  if ListBox_keyWords.ItemIndex >= 0 then
  begin
      tempStr:=  ListBox_keyWords.Items[ListBox_keyWords.ItemIndex] ;
      SynEditor.SelText := rightstr(  tempStr ,length(tempStr)-length(aMemoInput));
      ListBox_keyWords.Visible:=false;
      SynEditor.SetFocus;

       aMemoInput:='';
  end;
end;
end

 假设SynEditor为Memo类型

procedure TSearchReplaceDemoForm.SynEditorKeyDown(Sender: TObject; var  Key: Word; Shift: TShiftState);
begin
 if ( key=VK_DOWN)  and (ListBox_keyWords.Visible)     then begin //按 向下键,选择
  ListBox_keyWords.SetFocus;
   ListBox_keyWords.ItemIndex:=0;
       key:=0;//如果有问题,注释掉这行
    exit;
  end;

 if ( key=VK_BACK)     then begin // 向后删除键
    aMemoInput:=leftstr(aMemoInput,length(aMemoInput)-1) ;
   ListBox_keyWords_Refresh(aMemoInput);
        key:=0;//如果有问题,注释掉这行
     exit;
  end;

 if ( key=VK_ESCAPE ) or(key= VK_RETURN)    then begin // ESC  和 回车
   ListBox_keyWords.Visible:= false;
   aMemoInput:='';
        key:=0;//如果有问题,注释掉这行
   exit;
  end;
end;

procedure TSearchReplaceDemoForm.SynEditorKeyPress(Sender: TObject; var  Key: Char);
begin
 if CharInSet( key , ['a'..'z','A'..'Z','_']){ or (Key > #127)}  then   begin
  aMemoInput:=aMemoInput+key;
  ListBox_keyWords_Refresh(aMemoInput);
 end;

 if tulaterIsSeparator(key ) then     begin   //输入 间隔符, ListBox 消失
     aMemoInput:='' ;
    ListBox_keyWords.Visible:=false;
  end;
end;

 

procedure TForm9.ListBox_keyWordsDrawItem(Control: TWinControl; Index: Integer;    Rect: TRect; State: TOwnerDrawState);
var aX:integer;
   midStr,rightStr,yyyStr:string;
begin
  ax := Rect.Left ;
  midStr:=lowercase( aMemoInput);
//=====================================================================================
 ListBox_keyWords.Canvas.Font.Color := clred;
       //  中 文字
    yyyStr:=copy(
                    ListBox_keyWords.Items[index]
                    , pos(midStr, lowercase(ListBox_keyWords.Items[index]))
                    , length(midStr)
                 );
  ListBox_keyWords.Canvas.TextOut(ax , Rect.Top ,yyyStr);
  Inc(ax, ListBox_keyWords.Canvas.TextWidth(yyyStr));
  ax:=ax+1;
//=====================================================================================
 ListBox_keyWords.Canvas.Font.Color := clblack;
    rightStr:=copy(
                     ListBox_keyWords.Items[index]
                                 ,pos(midStr, lowercase(ListBox_keyWords.Items[index]))+length(midStr)
                                 ,100 );      //  右 文字
  ListBox_keyWords.Canvas.TextOut(ax , Rect.Top ,rightStr);
end