Как получить версию Windows Vista и выше, чем XP на Delphi? - PullRequest
3 голосов
/ 11 ноября 2011

Есть ли способ узнать, над какой версией Windows мы работаем?

Мне нужно установить изображение в TBitButton в Windows XP, а в Windows7 - нет изображения.Это должно быть сделано автоматически.

Ответы [ 2 ]

10 голосов
/ 11 ноября 2011

Отметьте SysUtils.Win32MajorVersion (в Delphi 7 вам нужно добавить SysUtils к вашему предложению uses, если его там еще нет - более поздние версии добавляют его автоматически).Самый простой способ - назначить Glyph как обычно в IDE и очистить его, если вы работаете в Vista или выше:

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

Для получения дополнительной информации об обнаружении определенных выпусков и версий Windows см. этот пост .Он не был обновлен для последних версий и выпусков Windows, но он поможет вам начать работу.Вы также можете найти SO 101 для [delphi] GetVersionEx, чтобы увидеть другие примеры.

0 голосов
/ 12 ноября 2011

Это на самом деле мой маленький проект - вставной компонент, который предоставляет информацию об операционной системе - даже предварительный просмотр во время разработки ...

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.
...