Delphi7:获取附加的监视器属性

时间:2010-10-11 08:29:44

标签: delphi delphi-7 monitor

如何获取显示器的属性?我最感兴趣的是制造商名称和型号。我也不想从注册表中获取它。 (有些PC就像我的工作PC一样限制了对属性键的访问,所以我宁愿想要扫描系统总线或者除了reg之外的东西。)

有什么想法吗? 谢谢SoulBlade

1 个答案:

答案 0 :(得分:4)

尝试使用Win32_DesktopMonitor WMI类。这堂课有你所看到的所有信息。

检查此示例代码。

program GetWMI_MonitorInfo;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

function VarStrNull(VarStr:OleVariant):string;//dummy function to handle null variants
begin
  Result:='';
  if not VarIsNull(VarStr) then
  Result:=VarToStr(VarStr);
end;


procedure  GetMonitorInfo;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;
    Moniker: IMoniker;
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
  end;

begin
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\CIMV2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_DesktopMonitor','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  if oEnum.Next(1, colItem, iValue) = 0 then
  begin
    Writeln('Caption      '+VarStrNull(colItem.Caption));
    Writeln('Description  '+VarStrNull(colItem.Description));
    Writeln('Device ID    '+VarStrNull(colItem.DeviceID));
    Writeln('Manufacturer '+VarStrNull(colItem.MonitorManufacturer));//Manufacter
    Writeln('Type         '+VarStrNull(colItem.MonitorType));//Model
  end;

end;


begin
 try
    CoInitialize(nil);
    try
      GetMonitorInfo;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.