Search code examples
winapidelphi-7

GetDpiForMonitor with Delphi 7 for per-monitor DPI awareness


The WinAPI function GetDpiForMonitor doesn't have a header translation for Delphi 7 and searching the internet I could not find one.

If anyone has sample code showing how to use this WinAPI function from Delphi 7, that would help greatly.


Solution

  • Here's how to dynamically load the DLL and call the function in Delphi 7:

    Type
      TMONITOR_DPI_TYPE = (
        MDT_EFFECTIVE_DPI {= 0},
        MDT_ANGULAR_DPI   {= 1},
        MDT_RAW_DPI       {= 2},
        MDT_DEFAULT       {= MDT_EFFECTIVE_DPI });
    
    var
      dpiX              : UINT;
      dpiY              : UINT;
      ErrCode           : HResult;
      hShcore           : THandle;
      GetDpiForMonitor  : function(monitor: HMONITOR; dpiType: TMONITOR_DPI_TYPE; var dpi, dpiY: UINT): HRESULT; stdcall;
    
    begin
      hShcore := GetModuleHandle('Shcore');
      If hShcore <> 0 then GetDpiForMonitor := GetProcAddress(hShcore,'GetDpiForMonitor');
      If @GetDpiForMonitor <> nil then 
        ErrCode := GetDpiForMonitor(Monitor.Handle,MDT_EFFECTIVE_DPI,dpiX,dpiY);
    end;