Jonvy

导航

Show_Application_Title

https://wiki.freepascal.org/Show_Application_Title,_Version,_and_Company#Windows

 Lazarus中:

function TForm1.GetVersionInfo : string;
var
  BuildNum : String;
  ver, Version,Rev:string;
  sProgName:string;
  Info: TVersionInfo;
begin
   Info := TVersionInfo.Create;
   Info.Load(HINSTANCE);
   // grab just the Build Number
   sProgName :='Label check report';
   ver := IntToStr(Info.FixedInfo.FileVersion[0]);
   Version:=IntToStr(Info.FixedInfo.FileVersion[1]);
   Rev:=IntToStr(Info.FixedInfo.FileVersion[2]);
   BuildNum := IntToStr(Info.FixedInfo.FileVersion[3]);
   Info.Free;

   // Update the title string - include the version & ver #
  // fMain.Caption := sProgName + ', ' + Version + ' Version   v' + ver + '-'+BuildNum;
   Result:=sProgName + ' ' + ver + '.'+ Version + '.'+Rev+'.'+BuildNum;

end;                         

调用:

Form1.Caption:=GetVersionInfo;

Delphi中:

使用Windows API来获取版本信息。以下是一个示例代码,展示如何在Delphi中获取文件的版本信息:

 

uses
  Windows, SysUtils;

function GetFileVersion(const FileName: string): string;
var
  Size, Handle: DWORD;
  Buffer: Pointer;
  FileInfo: PVSFixedFileInfo;
  FileInfoSize: UINT;
  Major, Minor, Release, Build: Word;
begin
  Size := GetFileVersionInfoSize(PChar(FileName), Handle);
  if Size = 0 then
    raise Exception.Create('No version information found');

  GetMem(Buffer, Size);
  try
    if not GetFileVersionInfo(PChar(FileName), Handle, Size, Buffer) then
      raise Exception.Create('Error reading version information');

    if not VerQueryValue(Buffer, '\', Pointer(FileInfo), FileInfoSize) then
      raise Exception.Create('Error querying version information');

    Major := HiWord(FileInfo.dwFileVersionMS);
    Minor := LoWord(FileInfo.dwFileVersionMS);
    Release := HiWord(FileInfo.dwFileVersionLS);
    Build := LoWord(FileInfo.dwFileVersionLS);

    Result := Format('%d.%d.%d.%d', [Major, Minor, Release, Build]);
  finally
    FreeMem(Buffer);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetFileVersion(ParamStr(0))); // 获取当前程序的版本信息
end;

 

 

 

 

 

 

 

posted on 2023-09-06 13:46  不亮  阅读(28)  评论(0)    收藏  举报