Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Code]
- const
- S_OK = $00000000;
- LOGPIXELSY = 90;
- MONITOR_DEFAULTTONULL = $00000000;
- MONITOR_DEFAULTTOPRIMARY = $00000001;
- MONITOR_DEFAULTTONEAREST = $00000002;
- type
- HDC = THandle;
- HMONITOR = THandle;
- MONITOR_DPI_TYPE = (
- MDT_EFFECTIVE_DPI,
- MDT_ANGULAR_DPI,
- MDT_RAW_DPI
- );
- const
- MDT_DEFAULT = MDT_EFFECTIVE_DPI;
- function GetDC(hWnd: HWND): HDC;
- external 'GetDC@user32.dll stdcall';
- function ReleaseDC(hWnd: HWND; hDC: HDC): Integer;
- external 'ReleaseDC@user32.dll stdcall';
- function GetDeviceCaps(hdc: HDC; index: Integer): Integer;
- external 'GetDeviceCaps@gdi32.dll stdcall';
- function MonitorFromWindow(hwnd: HWND; dwFlags: DWORD): HMONITOR;
- external 'MonitorFromWindow@user32.dll stdcall';
- function GetDpiForMonitor(hmonitor: HMONITOR; dpiType: MONITOR_DPI_TYPE;
- out dpiX: UINT; out dpiY: UINT): HRESULT;
- external 'GetDpiForMonitor@shcore.dll stdcall delayload';
- function IsWindows81OrLater: Boolean;
- begin
- Result := GetWindowsVersion >= $06030000;
- end;
- function GetPrimaryMonitorDPI: Integer;
- var
- DC: HDC;
- begin
- // no need for try..finally block here; just get the desktop DC handle,
- // get the DPI and release the obtained handle
- DC := GetDC(0);
- Result := GetDeviceCaps(DC, LOGPIXELSY);
- ReleaseDC(0, DC);
- end;
- function GetWindowMonitorDPI(Handle: HWND): Integer;
- var
- HorzDPI: UINT;
- VertDPI: UINT;
- Monitor: HMONITOR;
- begin
- // if we're not on at least Windows 8.1, then return the primary monitor
- // DPI since earlier systems did not allow per monitor DPI settings
- if not IsWindows81OrLater then
- Result := GetPrimaryMonitorDPI
- else
- // otherwise determine which monitor the given window intersects and try
- // to get DPI settings of the found monitor (if any; check MSDN docs for
- // the explanation and other options how to find the nearest monitor)
- begin
- // try to get the monitor which the window intersects
- Monitor := MonitorFromWindow(Handle, MONITOR_DEFAULTTONULL);
- // if there's any, then...
- if Monitor <> 0 then
- begin
- // try to get DPI for that monitor; if it succeeds, return the value,
- // raise an exception otherwise (for details check the MSDN docs)
- if GetDpiForMonitor(Monitor, MDT_DEFAULT, HorzDPI, VertDPI) = S_OK then
- Result := VertDPI
- else
- RaiseException('GetDpiForMonitor function call failed!');
- end
- else
- RaiseException('The given window does not intersect any monitor!');
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement