Advertisement
TLama

Untitled

Dec 9th, 2014
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.48 KB | None | 0 0
  1. [Code]
  2. const
  3.   S_OK = $00000000;
  4.   LOGPIXELSY = 90;
  5.   MONITOR_DEFAULTTONULL = $00000000;
  6.   MONITOR_DEFAULTTOPRIMARY = $00000001;
  7.   MONITOR_DEFAULTTONEAREST = $00000002;
  8.  
  9. type
  10.   HDC = THandle;
  11.   HMONITOR = THandle;
  12.   MONITOR_DPI_TYPE = (
  13.     MDT_EFFECTIVE_DPI,
  14.     MDT_ANGULAR_DPI,
  15.     MDT_RAW_DPI
  16.   );
  17. const
  18.   MDT_DEFAULT = MDT_EFFECTIVE_DPI;
  19.  
  20. function GetDC(hWnd: HWND): HDC;
  21.   external 'GetDC@user32.dll stdcall';
  22. function ReleaseDC(hWnd: HWND; hDC: HDC): Integer;
  23.   external 'ReleaseDC@user32.dll stdcall';
  24. function GetDeviceCaps(hdc: HDC; index: Integer): Integer;
  25.   external 'GetDeviceCaps@gdi32.dll stdcall';
  26. function MonitorFromWindow(hwnd: HWND; dwFlags: DWORD): HMONITOR;
  27.   external 'MonitorFromWindow@user32.dll stdcall';
  28. function GetDpiForMonitor(hmonitor: HMONITOR; dpiType: MONITOR_DPI_TYPE;
  29.   out dpiX: UINT; out dpiY: UINT): HRESULT;
  30.   external 'GetDpiForMonitor@shcore.dll stdcall delayload';
  31.  
  32. function IsWindows81OrLater: Boolean;
  33. begin
  34.   Result := GetWindowsVersion >= $06030000;
  35. end;
  36.  
  37. function GetPrimaryMonitorDPI: Integer;
  38. var
  39.   DC: HDC;
  40. begin
  41.   // no need for try..finally block here; just get the desktop DC handle,
  42.   // get the DPI and release the obtained handle
  43.   DC := GetDC(0);
  44.   Result := GetDeviceCaps(DC, LOGPIXELSY);
  45.   ReleaseDC(0, DC);
  46. end;
  47.  
  48. function GetWindowMonitorDPI(Handle: HWND): Integer;
  49. var
  50.   HorzDPI: UINT;
  51.   VertDPI: UINT;
  52.   Monitor: HMONITOR;
  53. begin
  54.   // if we're not on at least Windows 8.1, then return the primary monitor
  55.   // DPI since earlier systems did not allow per monitor DPI settings
  56.   if not IsWindows81OrLater then
  57.     Result := GetPrimaryMonitorDPI
  58.   else
  59.   // otherwise determine which monitor the given window intersects and try
  60.   // to get DPI settings of the found monitor (if any; check MSDN docs for
  61.   // the explanation and other options how to find the nearest monitor)
  62.   begin
  63.     // try to get the monitor which the window intersects
  64.     Monitor := MonitorFromWindow(Handle, MONITOR_DEFAULTTONULL);
  65.     // if there's any, then...
  66.     if Monitor <> 0 then
  67.     begin
  68.       // try to get DPI for that monitor; if it succeeds, return the value,
  69.       // raise an exception otherwise (for details check the MSDN docs)
  70.       if GetDpiForMonitor(Monitor, MDT_DEFAULT, HorzDPI, VertDPI) = S_OK then
  71.         Result := VertDPI
  72.       else
  73.         RaiseException('GetDpiForMonitor function call failed!');
  74.     end
  75.     else
  76.       RaiseException('The given window does not intersect any monitor!');
  77.   end;
  78. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement