delphi 常用方法

1、文件查找的方法

function Searchfile(path: string): TStringList;
var
  SearchRec: TSearchRec;
  found: integer;
begin
  Result := TStringList.Create;
  found := FindFirst(path + '\' + '*.*', faAnyFile, SearchRec);
  while found = 0 do
  begin
    if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and
      (SearchRec.Attr <> faDirectory) then
      Result.Add(SearchRec.Name);
    found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

2、通过文件名字获取Base64方法

function getBASE64_ByFile(const fileName: WideString): WideString;
var
  str:TStringstream;
  fstream:TFilestream;
begin
  //将bmp,jpeg 或其他 文件,返回BASE64编码
  result:='';
  if fileExists(fileName) then
  begin
    fstream := TFilestream.Create(fileName, fmOpenRead);
    str := TstringStream.create('');
    EncodeStream(fstream,str);
    result := str.DataString;
    fstream.free;
    str.free;
  end;
end;

3、通过进程名杀进程

procedure kill_all_proc(filename: string);
var
  i: integer;
  lppe: TProcessEntry32;
  found: boolean;
  Hand: THandle;
  dwProc: Cardinal;
  h: HWND;
begin
  Hand := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  lppe.dwSize := SizeOf(TProcessEntry32);
  found := Process32First(Hand, lppe);
  while found do
  begin
    if Trim(uppercase(StrPas(lppe.szExeFile))) = uppercase(filename) then
    begin
      dwProc := OpenProcess(PROCESS_ALL_ACCESS or PROCESS_TERMINATE, FALSE,
        dword(lppe.th32ProcessID));
      TerminateProcess(dwProc, 0);
    end;
    found := Process32Next(Hand, lppe);
  end;
end;

4、十六进制字符串转字符串

function HexToStr(const hMsg: String): String;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(hMsg) div 2 do
    Result := Result + Chr(StrToIntDef('0x'+ Copy(hMsg,(i-1)*2+1,2),0));

end;

5、字符串转DWORD,注意这里的字符串指接口等接收到的字符串。

function StrToWord(s: string): DWORD;
var
  I, c:integer;
begin
  Result := 0;

  c := Length(s);
  if c > 4 then
  begin
    s := Copy(s,1,4);
    c := 4;
  end;

  for I := 1 to c do
    Result:= Result + Round(IntPower(256,c-i)) * ord(s[i]);
end;

6、jpg图片压缩

procedure Compressjpg(filepath: string);
var
  JPEGImage: TJPEGImage;
  bmp: TBitmap;
begin
  try
    JPEGImage := TJPEGImage.Create;
    bmp := TBitmap.Create;
    JPEGImage.LoadFromFile(filepath);
    bmp.Width := JPEGImage.Width;
    bmp.Height := JPEGImage.Height;
    bmp.Canvas.StretchDraw(bmp.Canvas.ClipRect, JPEGImage);
    JPEGImage.Assign(bmp);
    JPEGImage.CompressionQuality:=StrToInt(ComboBox1.Text);
    JPEGImage.Compress;
    JPEGImage.SaveToFile(filepath);
    JPEGImage.free;
    bmp.free;
  except
  end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值