Delphi7: Get attached monitor properties

旧巷老猫 提交于 2019-12-10 16:37:51

问题


How do I get my monitor's properties? I'm mostly interested in the manufacturer name and model type. I also don't want to get it from the registry. (Some PC's like my work PC has restricted access to the property key so I'd rather want to scan a system bus or something other than the reg.)

Any ideas? Thanks SoulBlade


回答1:


try using the Win32_DesktopMonitor WMI Class. this class have all the information wich you are looking.

check this sample code.

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.


来源:https://stackoverflow.com/questions/3904619/delphi7-get-attached-monitor-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!