Search code examples
delphidirectxscreen-resolutionmultiple-monitors

Get current/native screen resolution of all monitors in Delphi (DirectX)


our delphi application can have multiple DirectX windows, often on multiple screens. up to now the user had to specify the fullscreen resolution using a dropdown with the supported resolutions. it would be very nice, if he could use a setting like 'current' which would be the resolution of the screen on which the window is.

we are using delphi with clootie directX headers. can someone give me a hint, how i would write a method using directX, winAPI or delphi methods to get the resolution of the current screen on which the window is?

kind regards, thalm

Final Solution:

ok, delphi 2007 MultiMon.pas returns crap for GetMonitorInfo, so i found this method, which works for me, using the winAPI directly:

function GetRectOfMonitorContainingRect(const R: TRect): TRect;
{ Returns bounding rectangle of monitor containing or nearest to R }
type
  HMONITOR = type THandle;
  TMonitorInfo = record
    cbSize: DWORD;
    rcMonitor: TRect;
    rcWork: TRect;
    dwFlags: DWORD;
  end;
const
  MONITOR_DEFAULTTONEAREST = $00000002;
var
  Module: HMODULE;
  MonitorFromRect: function(const lprc: TRect; dwFlags: DWORD): HMONITOR; stdcall;
  GetMonitorInfo: function(hMonitor: HMONITOR; var lpmi: TMonitorInfo): BOOL; stdcall;
  M: HMONITOR;
  Info: TMonitorInfo;
begin
  Module := GetModuleHandle(user32);
  MonitorFromRect := GetProcAddress(Module, 'MonitorFromRect');
  GetMonitorInfo := GetProcAddress(Module, 'GetMonitorInfoA');
  if Assigned(MonitorFromRect) and Assigned(GetMonitorInfo) then begin
    M := MonitorFromRect(R, MONITOR_DEFAULTTONEAREST);
    Info.cbSize := SizeOf(Info);
    if GetMonitorInfo(M, Info) then begin
      Result := Info.rcMonitor;
      Exit;
    end;
  end;
  Result := GetRectOfPrimaryMonitor(True);
end;

Solution

  • var
      MonInfo: TMonitorInfo;
    begin
      MonInfo.cbSize := SizeOf(MonInfo);
      GetMonitorInfo(MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST), @MonInfo);
      ShowMessage(Format('Current resolution: %dx%d',
                  [MonInfo.rcMonitor.Right - MonInfo.rcMonitor.Left,
                   MonInfo.rcMonitor.Bottom - MonInfo.rcMonitor.Top]));