在Delphi上如何获得Windows版本是Vista而不是XP?

时间:2011-11-11 03:33:28

标签: windows delphi winapi delphi-7

有没有办法知道我们正在开发哪种Windows版本?

我需要在Windows XP中将图像设置为TBitButton,而在Windows7中没有图像。它应该自动完成。

2 个答案:

答案 0 :(得分:10)

检查SysUtils.Win32MajorVersion(在Delphi 7中,如果您的SysUtils条款不存在,则需要添加uses - 以后的版本添加它自动)。最简单的方法是在IDE中像往常一样分配Glyph,如果您在Vista或更高版本上运行,则清除它:

if SysUtils.Win32MajorVersion >= 6 then // Windows Vista or higher
  BitBtn1.Glyph := nil;

有关检测特定Windows版本和版本的详细信息,请参阅this post。它尚未针对最新的Windows版本和版本进行更新,但它可以帮助您入门。您也可以搜索[delphi] GetVersionEx的SO以查看其他示例。

答案 1 :(得分:0)

这实际上是我的一个小项目 - 一个提供操作系统信息的插件组件 - 甚至可以在设计时预览它......

unit JDOSInfo;

interface

uses
  Classes, Windows, SysUtils, StrUtils, Forms, Registry;

type
  TJDOSInfo = class(TComponent)
  private
    fReg: TRegistry;
    fKey: String;
    fMinor: Integer;
    fMajor: Integer;
    fBuild: Integer;
    fPlatform: Integer;
    fIsServer: Bool;
    fIs64bit: Bool;
    fProductName: String;
    function GetProductName: String;
    procedure SetProductName(Value: String);
    procedure SetMajor(Value: Integer);
    procedure SetMinor(Value: Integer);
    procedure SetBuild(Value: Integer);
    procedure SetPlatform(Value: Integer);
    procedure SetIs64Bit(const Value: Bool);
    procedure SetIsServer(const Value: Bool);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Major: Integer read fMajor write SetMajor;
    property Minor: Integer read fMinor write SetMinor;
    property Build: Integer read fBuild write SetBuild;
    property Platf: Integer read fPlatform write SetPlatform;
    property ProductName: String read GetProductName write SetProductName;
    property IsServer: Bool read fIsServer write SetIsServer;
    property Is64Bit: Bool read fIs64bit write SetIs64Bit;
  end;

function IsWOW64: Boolean; 
function GetOSInfo: TOSVersionInfo;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('JD Custom', [TJDOSInfo]);
end;

function GetOSInfo: TOSVersionInfo;
begin
  FillChar(Result, SizeOf(Result), 0);
  Result.dwOSVersionInfoSize := SizeOf(Result);
  if not GetVersionEx(Result) then
    raise Exception.Create('Error calling GetVersionEx');
end;

function IsWOW64: Boolean;
type
  TIsWow64Process = function( // Type of IsWow64Process API fn
    Handle: THandle;
    var Res: BOOL): BOOL; stdcall;
var
  IsWow64Result: BOOL;              // result from IsWow64Process
  IsWow64Process: TIsWow64Process;  // IsWow64Process fn reference
begin
  // Try to load required function from kernel32
  IsWow64Process:= GetProcAddress(GetModuleHandle('kernel32'),'IsWow64Process');
  if Assigned(IsWow64Process) then
  begin
    // Function is implemented: call it
    if not IsWow64Process(GetCurrentProcess, IsWow64Result) then
      raise Exception.Create('Bad process handle');
    // Return result of function
    Result := IsWow64Result;
  end else
    // Function not implemented: can't be running on Wow64
    Result:= False;
end;

constructor TJDOSInfo.Create(AOwner: TComponent);
var
  Info: TOSVersionInfo;
  Str: String;
begin
  inherited Create(AOwner);
  fReg:= TRegistry.Create(KEY_READ);
  fReg.RootKey:= HKEY_LOCAL_MACHINE;
  fKey:= 'Software\Microsoft\Windows NT\CurrentVersion';  
  fReg.OpenKey(fKey, False);
  Info:= GetOSInfo;
  fMajor:= Info.dwMajorVersion;
  fMinor:= Info.dwMinorVersion;
  fBuild:= Info.dwBuildNumber;
  fIsServer:= False;
  fIs64bit:= False;
  fPlatform:= Info.dwPlatformId;
  if fMajor >= 5 then begin
    //After 2000
    if fReg.ValueExists('ProductName') then
      Str:= fReg.ReadString('ProductName')
    else begin
      Str:= 'Unknown OS: '+IntToStr(fMajor)+'.'+IntToStr(fMinor)+'.'+
        IntToStr(fBuild)+'.'+IntToStr(fPlatform);
    end;      
    if fReg.ValueExists('InstallationType') then begin
      if UpperCase(fReg.ReadString('InstallationType')) = 'SERVER' then
        fIsServer:= True;
    end;
    fIs64bit:= IsWOW64;
    if fIs64bit then
      Str:= Str + ' 64 Bit';
  end else begin
    //Before 2000
    case fMajor of
      4: begin
        case fMinor of
          0: Str:= 'Windows 95';
          10: Str:= 'Windows 98';
          90: Str:= 'Windows ME';
        end;
      end;
      else begin
        Str:= 'Older than 95';
      end;
    end;
  end;
  Self.fProductName:= Str;
end;

destructor TJDOSInfo.Destroy;
begin
  if assigned(fReg) then begin
    if fReg.Active then
      fReg.CloseKey;
    fReg.Free;
  end;
  inherited Destroy;
end;

function TJDOSInfo.GetProductName: String;
begin
  Result:= Self.fProductName;
end;

procedure TJDOSInfo.SetProductName(Value: String);
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetMinor(Value: Integer); 
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetMajor(Value: Integer); 
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetBuild(Value: Integer);  
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetPlatform(Value: Integer); 
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetIs64Bit(const Value: Bool);
begin
  //Do Nothing Here!
end;

procedure TJDOSInfo.SetIsServer(const Value: Bool);
begin
  //Do Nothing Here!
end;

end.