打印本文 打印本文 关闭窗口 关闭窗口
DELPHI常用函数集及简要范例
作者:武汉SEO闵涛  文章来源:敏韬网  点击数19885  更新时间:2009/4/23 18:30:33  文章录入:mintao  责任编辑:mintao
nbsp; else begin
   ix := ColumnToSort - 1;
   Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
  end;
end;
##  OnColumnClick, AlphaSort, OnCompare, CompareText example
-----------------------------------------------------------------------------
Concat    将字串相加.
-----------------------------------------------------------------------------
Unit  System
函数原型 function Concat(s1 [, s2,..., sn]: string): string;
说明  与 S := S1 + S2 + S3 ...; 相同.
范例  var
     S: string;
   begin
     S := Concat(''''ABC'''', ''''DEF''''); { ''''ABCDE'''' }
   end;

var
   S: string;
begin
   S:= ''''? ''''+ ''''???? ''''+ ''''???????? ??????'''';
   S:= Concat(''''? '''', ''''???? '''', ''''???????? ??????'''');
      // ? ????? ??????? S := ''''? ???? ???????? ??????''''
end;
-----------------------------------------------------------------------------
Copy    从母字串拷贝至另一个字串.
-----------------------------------------------------------------------------
Unit  System
函数原型 function Copy(S: string; Index, Count: Integer): string;
说明  S  : 字串.
   Indexd : 从第几位开始拷贝.
   Count : 总共要拷贝几位.
范例  var S: string;
   begin
     S := ''''ABCDEF'''';
     S := Copy(S, 2, 3); { ''''BCD'''' }
   end;
----------------
var
   S: string;
begin
   S:= ''''??????'''';
   S:= Copy( S, 3, 4);     // S := ''''????''''
end;
---------------
Example
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var
  Found: boolean;
  i,SelSt: Integer;
  TmpStr: string;
begin
  { first, process the keystroke to obtain the current string }
  { This code requires all items in list to be uppercase}
  if Key in [''''a''''..''''z''''] then Dec(Key,32); {Force Uppercase only!}
  with (Sender as TComboBox) do
  begin
    SelSt := SelStart;
    if (Key = Chr(vk_Back)) and (SelLength <> 0) then
     TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255)

    else if Key = Chr(vk_Back) then {SelLength = 0}
     TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255)
    else {Key in [''''A''''..''''Z'''', etc]}
     TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255);
    if TmpStr = '''' then Exit;
    { update SelSt to the current insertion point }

    if (Key = Chr(vk_Back)) and (SelSt > 0) then Dec(SelSt)

    else if Key <> Chr(vk_Back) then Inc(SelSt);
    Key := #0; { indicate that key was handled }
    if SelSt = 0 then 
    begin
      Text:= '''';
      Exit;
    end;

   {Now that TmpStr is the currently typed string, see if we can locate a match }

    Found := False;
    for i := 1 to Items.Count do
      if Copy(Items[i-1],1,Length(TmpStr)) = TmpStr then
      begin
        Text := Items[i-1]; { update to the match that was found }
        ItemIndex := i-1;
        Found := True;
        Break;
      end;
    if Found then { select the untyped end of the string }
    begin
      SelStart := SelSt;
      SelLength := Length(Text)-SelSt;

    end
    else Beep;
  end;
end;
-----------------------
procedure TComponentEditor.Copy;
var
  AFormat : Word;
  AData,APalette : THandle;
begin
  with Component as TImage do
  begin
    Picture.SaveToClipBoardFormat(AFormat,AData,APalette);
    ClipBoard.SetAsHandle(AFormat,AData);
  end;
end;


## Copy, Chr, SelStart, SelLength example

-----------------------------------------------------------------------------
Delete    删除字串中的数个字元.
-----------------------------------------------------------------------------
Unit  System
函数原型 procedure Delete(var S: string; Index, Count:Integer);
说明  S  : 字串.
   Indexd : 从第几位开始删.
   Count : 总共要删几位.
范例  var
     s: string;
   begin
     s := ''''Honest Abe Lincoln'''';
     Delete(s,8,4);
     Canvas.TextOut(10, 10, s); { ''''Honest Lincoln'''' }
   end;
var
   S: string;
begin
   S:= ''''???????, ??????, ??????????!'''';
   Delete(S, 8, 1);     // S := ''''??????? ??????, ??????????!''''
   MessageDlg(S, mtWarning, [mbOK],0); 
end;
-----------------------------------------------------------------------------
NewStr   在 heap 中配置一个新的字串空间给PString 指标.
-----------------------------------------------------------------------------
DisposeStr  在 heap 中释放一个字串空间 PString指标.
-----------------------------------------------------------------------------
Unit  SysUtils
函数原型 function NewStr(const S: string): PString;
函数原型 procedure DisposeStr(P: PString);
说明  S  : 字串.
   Pstring : 新的字串指标.
范例  var
     P: PString;
     S: string;
   begin
     S := ''''Ask me about Blaise'''';
     P := NewStr(S);
     DisposeStr(P):
   end;
-----------------------------------------------------------------------------
Insert    将一个子字串插入另一个字串中.
-----------------------------------------------------------------------------
Unit  System
函数原型 procedure Insert(Source: string; var S: string; Index: Integer);
说明  Source : 子字串.
   S  : 被插入的母字串.
   Indexd  : 从第几位开始插入.
范例  var
     S: string;
   begin
     S := ''''Honest Lincoln'''';
     Insert(''''Abe '''', S, 8); { ''''Honest Abe Lincoln'''' }
   end;
var
   S: string;
begin
   S:= ''''??????? ?????? ??????????.'''';
   Insert( ''''!'''', S, 8);       { S := ''''???????! ?????? ??????????.''''}
   MessageDlg( S, mtWarning, [mbOK],0); 
end;
-----------------------------------------------------------------------------
IntToHex   将 Int 转为&n

 << 上一页  [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]  ...  下一页 >> 

打印本文 打印本文 关闭窗口 关闭窗口