1. 程式人生 > >一些DELPHI的函式

一些DELPHI的函式

1、用HTTP從網站下載檔案:
方法一,用IdHttp:
procedure TForm1.Button1Click(Sender: Tobject);
var
 fs: TFileStream;
begin
 fs := TFileStream.Create('c:\aaa.htm', fmCreate);
 IdHTTP1.Get('http://www.abc.com/aaa.shtml', fs);
 fs.Free;
end;
方法二:
uses
 UrlMon
function DownloadFile(Source, Dest: string): Boolean;
begin
 try
 Result := UrlDownloadToFile(nil, Pchar(source), Pchar(Dest), 0, nil) = 0;
 except
 Result := False;
 end;
end;
procedure TForm1.Button1Click(Sender: Tobject);
begin
 if DownloadFile(edit1.Text, edit2.Text) then
 ShowMessage('下載成功')
 else ShowMessage('下載失敗');
end;

2、網路鄰居複製檔案:
uses shellapi;
copyfile(pchar('newfile.txt'),pchar('//computer/direct/targer.txt'),false);

3、取得WINDOWS目錄:
方法一:
uses shellapi;
var windir:array[0..255] of char;
getwindowsdirectory(windir,sizeof(windir));
方法二:
從登錄檔中讀取,位置:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion
SystemRoot鍵,取得如:C:\WINDOWS

4、對檔案、目錄進行操作:
Chdir('c:\abcdir');轉到目錄
Mkdir('dirname');建立目錄
Rmdir('dirname');刪除目錄
GetCurrentDir;//取當前目錄名,無'\'
Getdir(0,s);//取工作目錄名s:='c:\abcdir';
Deletfile('abc.txt');//刪除檔案
Renamefile('old.txt','new.txt');//檔案更名
ExtractFilename(filelistbox1.filename);//取檔名
ExtractFileExt(filelistbox1.filename);//取檔案字尾

5、取得系統執行的程序名:
var hCurrentWindow:HWnd;szText:array[0..254] of char;
begin
hCurrentWindow:=Getwindow(handle,GW_HWndFrist);
while hCurrentWindow <> 0 do
begin
if Getwindowtext(hcurrnetwindow,@sztext,255)>0 then listbox1.items.add(strpas(@sztext));
hCurrentWindow:=Getwindow(hCurrentwindow,GW_HWndNext);
end;
end;

6、操作Cookie:
response.cookies("name").domain:='http://www.aaa.com';
with response.cookies.add do
begin
name:='username';
value:='username';
end;

7、按鍵接受訊息:
OnCreate事件中處理:Application.OnMessage:=MyOnMessage;
procedure Tform1.MyOnMessage(var MSG:TMSG;var Handle:Boolean);
begin
if msg.message=256 then … //ANY鍵
if msg.message=112 then … //F1
if msg.message=113 then … //F2
end;

8、判斷撥號網路是開還是關:
if GetSystemMetrics(SM_NETWORK) AND $01 = $01 then
showmessage('線上!')
else showmessage('不線上!');

9、IP到域名的轉換:
function GetDomainName(Ip:string):string;
var
pH:Phostent;
data:twsadata;
ii:dword;
begin
WSAStartup($101, Data);
ii:=inet_addr(pchar(ip));
pH:=gethostbyaddr(@ii,sizeof(ii),PF_INET);
if (ph<>nil) then
result:=pH.h_name
else
result:='';
WSACleanup;
end;

10、解除interbase預設只能連5個客戶端的限制:
必須硬碟安裝,如果是用的光碟,需要把光碟上IB6\SERVER目錄COPY到硬碟,然後從硬碟安裝。在SERVER目錄下,記錄ib_license.dat檔案中的ID及KEY,再把下面這個ib_license.dat檔案覆蓋掉SERVER目錄中的同名檔案,然後開始安裝,填寫ID和KEY時用記錄下的ID及KEY。
Ib_license.dat:
PRODUCT INTERBASE, OPTIONS QR, VERSION WI-V6.0, CERTIFICATE 100, ID VAR-10472, KEY a8-2-22-0, COMMENT InterBase Remote Client
PRODUCT INTERBASE, OPTIONS DEIQR, VERSION WI-V6.0, CERTIFICATE 50, USERS 2, ID VAR-13505, KEY 68-10-22-0PRODUCT INTERBASE, OPTIONS QR, VERSION WI-V6.0, CERTIFICATE 100, ID VAR-10472, KEY a8-2-22-1, COMMENT InterBase Remote Client
PRODUCT INTERBASE, OPTIONS DEIQRSW, VERSION WI-V6.0, CERTIFICATE 56, ID VAR-12345, KEY f7-13-a6-4
PRODUCT INTERBASE, OPTIONS R, VERSION WI-V6.0, CERTIFICATE 52, ID VAR-12347, KEY 1f-1-22-0
PRODUCT INTERBASE, OPTIONS DEIQR, VERSION WI-V6.0, CERTIFICATE 50, USERS 2, ID VAR-12348, KEY 3e-10-22-0
PRODUCT INTERBASE, OPTIONS S, VERSION WI-V6.0, CERTIFICATE 53, ID VAR-12349, KEY 2e-1-44-0
PRODUCT INTERBASE, OPTIONS D, VERSION WI-V6.0, CERTIFICATE 54, ID VAR-12350, KEY eb-1-0-0
PRODUCT INTERBASE, OPTIONS W, VERSION WI-V6.0, CERTIFICATE 60, ID VAR-12351, KEY 1a-1-40-4
PRODUCT INTERBASE, OPTIONS C3, VERSION WI-V6.0, CERTIFICATE 70, ID VAR-12353, KEY be-1-11-10
PRODUCT INTERBASE, OPTIONS A, VERSION WI-V6.0, CERTIFICATE 71, ID VAR-12354, KEY 87-1-0-0
PRODUCT INTERBASE, OPTIONS F, VERSION WI-V6.0, CERTIFICATE 72, ID VAR-12344, KEY 7c-f-0-0
PRODUCT INTERBASE, OPTIONS F, VERSION WI-V6.0, CERTIFICATE 72, ID VAR-99999, KEY ff-2-0-0
PRODUCT INTERBASE, OPTIONS 2, VERSION WI-V6.0, CERTIFICATE 73, ID VAR-11111, KEY 20-1-0-88
PRODUCT INTERBASE, OPTIONS P, VERSION WI-V6.0, CERTIFICATE 74, ID VAR-22222, KEY 10-82-8-0
PRODUCT INTERBASE, OPTIONS Y, VERSION WI-V6.0, CERTIFICATE 81, ID VAR-44444, KEY d3-3-0-11
PRODUCT INTERBASE, OPTIONS 5, VERSION WI-V6.0, CERTIFICATE 82, ID VAR-55555, KEY a5-3-0-40
PRODUCT INTERBASE, OPTIONS T, VERSION WI-V6.0, CERTIFICATE 83, ID VAR-66666, KEY f7-3-88-0