| 導購 | 订阅 | 在线投稿
分享
 
 
 

DELPHI的一些開發技巧和方法(二)

2007-06-23 04:20:04  編輯來源:互聯網  简体版  手機版  評論  字體: ||
 
  作者 : toofree
  
  
   標題 : DELPHI的一些開發技巧和方法(二)
  
  
   關鍵字: DELPHI 開發技巧
  
  
   分類 : 開發技巧
  
  
   密級 : 參賽
  
  
  
  
  
  
   (評分:★ , 回複: 3, 閱讀: 1185)
  11、向其他應用程序發送模擬鍵:
  var
  h: Thandle;
  begin
  h := FindWindow(nil, '應用程序標題');
  PostMessage(h, WM_KEYDOWN, VK_F9, 0);//發送F9鍵
  end
  12、判斷當前網絡連接方式:
  (判斷結果爲MODEM、局域網或是代理服務器方式)
  uses wininet;
  Function ConnectionKind :boolean;
  var flags: dword;
  begin
  Result := InternetGetConnectedState(@flags, 0);
  if Result then
  begin
  if (flags and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then
  begin
  showmessage('Modem');
  end;
  if (flags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then
  begin
  showmessage('LAN');
  end;
  if (flags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then
  begin
  showmessage('Proxy');
  end;
  if (flags and INTERNET_CONNECTION_MODEM_BUSY)=INTERNET_CONNECTION_MODEM_BUSY then
  begin
  showmessage('Modem Busy');
  end;
  end;
  end;
  13、取機器BIOS信息:
  with Memo1.Lines do
  begin
  Add('MainBoardBiosName:'+^I+string(Pchar(Ptr($FE061))));
  Add('MainBoardBiosCopyRight:'+^I+string(Pchar(Ptr($FE091))));
  Add('MainBoardBiosDate:'+^I+string(Pchar(Ptr($FFFF5))));
  Add('MainBoardBiosSerialNo:'+^I+string(Pchar(Ptr($FEC71))));
  end;
  14、解析服務器IP地址:
  uses winsock
  function IPAddrToName(IPAddr : String): String;
  var
  SockAddrIn: TSockAddrIn;
  HostEnt: PHostEnt;
  WSAData: TWSAData;
  begin
  WSAStartup($101, WSAData);
  SockAddrIn.sin_addr.s_addr:= inet_addr(Pchar(IPAddr));
  HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
  if HostEnt<>nil then result:=StrPas(Hostent^.h_name) else result:='';
  end;
  15、清空一個目錄:
  function EmptyDirectory(TheDirectory :String ; Recursive : Boolean) :
  Boolean;
  var
  SearchRec : TSearchRec;
  Res : Integer;
  begin
  Result := False;
  TheDirectory := NormalDir(TheDirectory);
  Res := FindFirst(TheDirectory + '*.*', faAnyFile, SearchRec);
  try
  while Res = 0 do
  begin
  if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  begin
  if ((SearchRec.Attr and faDirectory) > 0) and Recursive
  then begin
  EmptyDirectory(TheDirectory + SearchRec.Name, True);
  RemoveDirectory(Pchar(TheDirectory + SearchRec.Name));
  end
  else begin
  DeleteFile(Pchar(TheDirectory + SearchRec.Name))
  end;
  end;
  Res := FindNext(SearchRec);
  end;
  Result := True;
  finally
  FindClose(SearchRec.FindHandle);
  end;
  end;
  16、計算目錄的大小:
  function GetDirectorySize(const Adirectory: string): Integer;
  var
  Dir: TSearchRec;
  Ret: integer;
  Path: string;
  begin
  Result := 0;
  Path := ExtractFilePath(Adirectory);
  Ret := Sysutils.FindFirst(Adirectory, faAnyFile, Dir);
  if Ret <> NO_ERROR then exit;
  try
  while ret=NO_ERROR do
  begin
  inc(Result, Dir.Size);
  if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then
  Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));
  Ret := Sysutils.FindNext(Dir);
  end;
  finally
  Sysutils.FindClose(Dir);
  end;
  end;
  17、獲得硬盤序列號:
  var SerialNum : pdword; a, b : dword; Buffer : array [0..255] of char;
  begin
  if GetVolumeInformation('c:\', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0) then Label1.Caption := IntToStr(SerialNum^);
  end;
  18、目錄完全刪除:
  procedure Tform1.DeleteDirectory(strDir:String);
  var
  sr: TSearchRec;
  FileAttrs: Integer;
  strfilename:string;
  strPth:string;
  begin
  strpth:=Getcurrentdir();
  FileAttrs := faAnyFile;
  if FindFirst(strpth+'\'+strdir+'\*.*', FileAttrs, sr) = 0 then
  begin
  if (sr.Attr and FileAttrs) = sr.Attr then
  begin
  strfilename:=sr.Name;
  if fileexists(strpth+'\'+strdir+'\'+strfilename) then
  deletefile(strpth+'\'+strdir+'\'+strfilename);
  end;
  while FindNext(sr) = 0 do
  begin
  if (sr.Attr and FileAttrs) = sr.Attr then
  begin
  strfilename:=sr.name;
  if fileexists(strpth+'\'+strdir+'\'+strfilename) then
  deletefile(strpth+'\'+strdir+'\'+strfilename);
  end;
  end;
  FindClose(sr);
  removedir(strpth+'\'+strdir);
  end;
  end;
  19、TFileStream的操作:
  //從文件流當前位置讀count字節到緩沖區BUFFER
  function read(var buffer;count:longint):longint;override;
  //將緩沖區BUFFER讀到文件流中
  function write(const buffer;count:longint):longint;override;
  //設置文件流當前讀寫指針爲OFFSET
  function seek(offset:longint;origin:word):longint;override;
  origin={soFromBeginning,soFromCurrent,soFromEnd}
  //從另一文件流中當前位置複制COUNT到當前文件流當前位置
  function copyfrom(source:Tstream;count:longint):longint;
  //讀指定文件到文件流
  var myFStream:TFileStream;
  begin
  myFStream:=TFileStream.create(OpenDialog1.filename,fmOpenRead);
  end;
  20、獲得CPU序列號:
  function GetCpuId:longint;assembler;register;
  var
  temp:longint;
  begin
  asm
   PUSH EBX
   PUSH EDI
   MOV EDI,EAX
   MOV EAX,1
   DW $A20F
   MOV TEMP,EDX
   POP EDI
   POP EBX
  end;
  result:=temp;
  end;
  procedure TForm1.Button1Click(Sender: Tobject);
  begin
   label1.Caption:=IntToHex(GetCpuId,8);
  end;
  2003-5-30 10:47:00
 
作者 : toofree 標題 : DELPHI的一些開發技巧和方法(二) 關鍵字: DELPHI 開發技巧 分類 : 開發技巧 密級 : 參賽 (評分:★ , 回複: 3, 閱讀: 1185) 11、向其他應用程序發送模擬鍵: var h: Thandle; begin h := FindWindow(nil, '應用程序標題'); PostMessage(h, WM_KEYDOWN, VK_F9, 0);//發送F9鍵 end 12、判斷當前網絡連接方式: (判斷結果爲MODEM、局域網或是代理服務器方式) uses wininet; Function ConnectionKind :boolean; var flags: dword; begin Result := InternetGetConnectedState(@flags, 0); if Result then begin if (flags and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then begin showmessage('Modem'); end; if (flags and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then begin showmessage('LAN'); end; if (flags and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then begin showmessage('Proxy'); end; if (flags and INTERNET_CONNECTION_MODEM_BUSY)=INTERNET_CONNECTION_MODEM_BUSY then begin showmessage('Modem Busy'); end; end; end; 13、取機器BIOS信息: with Memo1.Lines do begin Add('MainBoardBiosName:'+^I+string(Pchar(Ptr($FE061)))); Add('MainBoardBiosCopyRight:'+^I+string(Pchar(Ptr($FE091)))); Add('MainBoardBiosDate:'+^I+string(Pchar(Ptr($FFFF5)))); Add('MainBoardBiosSerialNo:'+^I+string(Pchar(Ptr($FEC71)))); end; 14、解析服務器IP地址: uses winsock function IPAddrToName(IPAddr : String): String; var SockAddrIn: TSockAddrIn; HostEnt: PHostEnt; WSAData: TWSAData; begin WSAStartup($101, WSAData); SockAddrIn.sin_addr.s_addr:= inet_addr(Pchar(IPAddr)); HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET); if HostEnt<>nil then result:=StrPas(Hostent^.h_name) else result:=''; end; 15、清空一個目錄: function EmptyDirectory(TheDirectory :String ; Recursive : Boolean) : Boolean; var SearchRec : TSearchRec; Res : Integer; begin Result := False; TheDirectory := NormalDir(TheDirectory); Res := FindFirst(TheDirectory + '*.*', faAnyFile, SearchRec); try while Res = 0 do begin if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then begin if ((SearchRec.Attr and faDirectory) > 0) and Recursive then begin EmptyDirectory(TheDirectory + SearchRec.Name, True); RemoveDirectory(Pchar(TheDirectory + SearchRec.Name)); end else begin DeleteFile(Pchar(TheDirectory + SearchRec.Name)) end; end; Res := FindNext(SearchRec); end; Result := True; finally FindClose(SearchRec.FindHandle); end; end; 16、計算目錄的大小: function GetDirectorySize(const Adirectory: string): Integer; var Dir: TSearchRec; Ret: integer; Path: string; begin Result := 0; Path := ExtractFilePath(Adirectory); Ret := Sysutils.FindFirst(Adirectory, faAnyFile, Dir); if Ret <> NO_ERROR then exit; try while ret=NO_ERROR do begin inc(Result, Dir.Size); if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*')); Ret := Sysutils.FindNext(Dir); end; finally Sysutils.FindClose(Dir); end; end; 17、獲得硬盤序列號: var SerialNum : pdword; a, b : dword; Buffer : array [0..255] of char; begin if GetVolumeInformation('c:\', Buffer, SizeOf(Buffer), SerialNum, a, b, nil, 0) then Label1.Caption := IntToStr(SerialNum^); end; 18、目錄完全刪除: procedure Tform1.DeleteDirectory(strDir:String); var sr: TSearchRec; FileAttrs: Integer; strfilename:string; strPth:string; begin strpth:=Getcurrentdir(); FileAttrs := faAnyFile; if FindFirst(strpth+'\'+strdir+'\*.*', FileAttrs, sr) = 0 then begin if (sr.Attr and FileAttrs) = sr.Attr then begin strfilename:=sr.Name; if fileexists(strpth+'\'+strdir+'\'+strfilename) then deletefile(strpth+'\'+strdir+'\'+strfilename); end; while FindNext(sr) = 0 do begin if (sr.Attr and FileAttrs) = sr.Attr then begin strfilename:=sr.name; if fileexists(strpth+'\'+strdir+'\'+strfilename) then deletefile(strpth+'\'+strdir+'\'+strfilename); end; end; FindClose(sr); removedir(strpth+'\'+strdir); end; end; 19、TFileStream的操作: //從文件流當前位置讀count字節到緩沖區BUFFER function read(var buffer;count:longint):longint;override; //將緩沖區BUFFER讀到文件流中 function write(const buffer;count:longint):longint;override; //設置文件流當前讀寫指針爲OFFSET function seek(offset:longint;origin:word):longint;override; origin={soFromBeginning,soFromCurrent,soFromEnd} //從另一文件流中當前位置複制COUNT到當前文件流當前位置 function copyfrom(source:Tstream;count:longint):longint; //讀指定文件到文件流 var myFStream:TFileStream; begin myFStream:=TFileStream.create(OpenDialog1.filename,fmOpenRead); end; 20、獲得CPU序列號: function GetCpuId:longint;assembler;register; var temp:longint; begin asm PUSH EBX PUSH EDI MOV EDI,EAX MOV EAX,1 DW $A20F MOV TEMP,EDX POP EDI POP EBX end; result:=temp; end; procedure TForm1.Button1Click(Sender: Tobject); begin label1.Caption:=IntToHex(GetCpuId,8); end; 2003-5-30 10:47:00
󰈣󰈤
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
  免責聲明:本文僅代表作者個人觀點,與王朝網絡無關。王朝網絡登載此文出於傳遞更多信息之目的,並不意味著贊同其觀點或證實其描述,其原創性以及文中陳述文字和內容未經本站證實,對本文以及其中全部或者部分內容、文字的真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,並請自行核實相關內容。
 
© 2005- 王朝網路 版權所有