EDIT - see update at end
This is for Delphi 7.0 Build 4.453
Summary
I need to have the Handle property from the TMonitor object (an element in the Monitors array in the TScreen component), which is HMONITOR, and turn it into a string that you will use when calling EnumDisplaySettings as the parameter lpszDeviceName.
(my final goal is to get a list of device settings from the given HMONITOR value by passing the resolved name lpszDeviceName to EnumDisplaySettings calls).
detailed information
As mentioned above, the Screen.Monitors [x] .Handle property is of type HMONITOR and is usually used to go to the GetMonitorInfo function , which returns geometry, but not lpszDeviceName. (note: there is a TMonitorInfoEx structure that has a szDevice field, but it does not seem to populate on my system, although I set the cbSize field to the appropriate size).
Alternatively , if I can use szDeviceName to get the equivalent HMONITOR value, I could connect it to the next function that would use it when comparing (I inserted a dummy function call called hMonitorFromDeviceName in the code below) to indicate how it will be used.
function GetMonitorDeviceName(hmon : HMONITOR) : string;
var
DispDev : TDisplayDevice;
deviceName : string;
nDeviceIndex : integer;
begin
Result := '';
FillChar(DispDev, sizeof(DispDev),0);
DispDev.cb := sizeof(DispDev);
nDeviceIndex := 0;
while (EnumDisplayDevices(nil, nDeviceIndex, DispDev, 0)) do
begin
if ( hMonitorFromDeviceName(DispDev.DeviceString) = hmon ) then
begin
Result := StrPas(DispDev.DeviceString);
exit;
end;
inc(nDeviceIndex);
end;
end;
Update
, , , :
function GetMonitorName(hmon : HMONITOR) : string;
type
TMonitorInfoEx = record
cbSize: DWORD;
rcMonitor: TRect;
rcWork: TRect;
dwFlags: DWORD;
szDevice: array[0..CCHDEVICENAME - 1] of AnsiChar;
end;
var
DispDev : TDisplayDevice;
deviceName : string;
monInfo : TMonitorInfoEx;
begin
Result := '';
monInfo.cbSize := sizeof(monInfo);
if GetMonitorInfo(hmon,@monInfo) then
begin
DispDev.cb := sizeof(DispDev);
EnumDisplayDevices(@monInfo.szDevice, 0, DispDev, 0);
Result := StrPas(DispDev.DeviceString);
end;
end;