Delphi中一些比较常用的方法和函数


//数据库操作
procedure TMainForm.AdoQureyState(IsSelect:Boolean;SqlStr:string);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(SqlStr);
if(IsSelect=True)then
begin
try
    ADOQuery1.Open;
except
    on e:Exception do
    begin
       //do something
      raise;
    end;
end;
end
else
begin
try
    ADOQuery1.ExecSQL;
except
    on e:Exception do
    begin
       //do something
       raise;
    end;
end;
end;
end;

//替换数据
Function ReplaceData(Str:string;Restr:string;NewStr:string):string;
begin
Str:= stringreplace(Str,Restr,NewStr, [rfReplaceAll]);
result:=Str;
end;

类型说明:

rfReplaceAll:全部替换
rfIgnoreCase:忽略大小写

[]:替换第一个

注:可连用,如:[rfReplaceAll, rfIgnoreCase]忽略大小写并替换所有

//获取指定位置的字符串
Function GetData(StrBegin:string;StrEnd:string;Str:string):String;
var
in_star,in_end:integer;
begin
in_star:=AnsiPos(strbegin,Str)+length(strbegin);
in_end:=AnsiPos(strEnd,Str);
if((in_star=0)or(in_end=0))then
begin
result:='';
end
else
begin
result:=copy(Str,in_star,in_end-in_star);
end;
end;

//保存INI信息
procedure TMainForm.SaveInI();
var
Filename:string;
myinifile:Tinifile;
begin
Filename:=ExtractFilePath(Paramstr(0))+'Operation.ini';
myinifile:=Tinifile.Create(filename);

myinifile.WriteInteger('Test','Pv',Pv);
myinifile.Free;
end;

//读取INI配置文件信息
procedure TMainForm.LoadInI();
var
Filename:string;
myinifile:Tinifile;
begin
Filename:=ExtractFilePath(Paramstr(0))+'Operation.ini';//获取续点
myinifile:=Tinifile.Create(filename);
Pv:=strtoint(myinifile.Readstring('Test','Pv','0'));

myinifile.Free;

end;

//点击指定位置或名称的链接或者给指定位置或名称的输入框赋值
procedure GetItem(Document:IHTMLDocument2;Index:String;IsClick:Boolean;State:integer;Value:string);
var IDoc1: IHTMLDocument2;
    ielc: IHTMLElementCollection;
    ihtml:IHTMLElement;
    input:IHTMLInputElement;
    TextArea:IHTMLTextAreaElement;
    i:integer;
begin
Document.QueryInterface(IHTMLDocument2,iDoc1);
ielc:=idoc1.Get_all;
if(trystrtoint(Index,i))then
begin
if(IsClick)then
begin
    ihtml:=ielc.item(strtoint(Index),0) as IHTMLElement;
    ihtml.click;
end
else
begin
    case State of
      0 :
      begin
        input:=ielc.item(strtoint(Index),0) as IHTMLInputElement;
        input.value:=Value;
      end;
      1 :
      begin
        TextArea:=ielc.item(strtoint(Index),0) as IHTMLTextAreaElement;
        TextArea.value:=Value;
      end;
    end;
end;
end
else
begin
if(IsClick)then
begin
    ihtml:=ielc.item(Index,0) as IHTMLElement;
    ihtml.click;
end
else
begin
    case State of
      0 :
      begin
        input:=ielc.item(Index,0) as IHTMLInputElement;
        input.value:=Value;
      end;
      1 :
      begin
        TextArea:=ielc.item(Index,0) as IHTMLTextAreaElement;
        TextArea.value:=Value;
      end;
    end;
end;
end;
end;

//获取指定网址的网页源码
function GetWebPage(const Url: string):string;
var
Session,
HttpFile:HINTERNET;
szSizeBuffer:Pointer;
dwLengthSizeBuffer:DWord;
dwReserved:DWord;
dwFileSize:DWord;
dwBytesRead:DWord;
Contents:PChar;
begin
Session:=InternetOpen('',0,niL,niL,0);
HttpFile:=InternetOpenUrl(Session,PChar(Url),niL,0,0,0);
dwLengthSizeBuffer:=1024;
HttpQueryInfo(HttpFile,5,szSizeBuffer,dwLengthSizeBuffer,dwReserved);
GetMem(Contents,dwFileSize);
InternetReadFile(HttpFile,Contents,dwFileSize,dwBytesRead);
InternetCloseHandle(HttpFile);
InternetCloseHandle(Session);
Result:=StrPas(Contents);
FreeMem(Contents);
end;

//获取网页源码
function GetHtml(Document:IHTMLDocument2): string;
const
BufSize = $10000;
var
Size: Int64;
Stream: IStream;
hHTMLText: HGLOBAL;
psi: IPersistStreamInit;
begin
if not Assigned(Document) then Exit;
OleCheck(Document.QueryInterface(IPersistStreamInit, psi));
try
//OleCheck(psi.GetSizeMax(Size));
hHTMLText := GlobalAlloc(GPTR, BufSize);
//if 0 = hHTMLText then RaiseLastWin32Error;
OleCheck(CreateStreamOnHGlobal(hHTMLText,True, Stream));
try
OleCheck(psi.Save(Stream, False));
Size := StrLen(PChar(hHTMLText));
SetLength(Result, Size);
CopyMemory(PChar(Result), Pointer(hHTMLText),Size);
finally
Stream := nil;
end;
finally
psi := nil;
end;
end;


发表评论

您的电子邮箱地址不会被公开。

5 + 4 =