Use OCR in Windows 10 quickly and easily with Text Grab. With optional background process and popups.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

  1. //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. //// PARTICULAR PURPOSE.
  5. ////
  6. //// Copyright (c) Microsoft Corporation. All rights reserved
  7. //Main helper class that provides helper functions used by the PerMonitorDPIWindow class
  8. #include "pch.h"
  9. #include "PerMonitorDPIHelpers.h"
  10. // Returns the DPI awareness of the current process
  11. PROCESS_DPI_AWARENESS NativeHelpers::PerMonitorDPIHelper::GetPerMonitorDPIAware()
  12. {
  13. PROCESS_DPI_AWARENESS awareness;
  14. HANDLE hProcess;
  15. hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, GetCurrentProcessId());
  16. auto result = GetProcessDpiAwareness(hProcess, &awareness);
  17. if (S_OK != result)
  18. {
  19. throw gcnew System::Exception(L"Unable to read process DPI level");
  20. }
  21. return awareness;
  22. }
  23. //Sets the current process as Per_Monitor_DPI_Aware. Returns True if the process was marked as Per_Monitor_DPI_Aware
  24. BOOL NativeHelpers::PerMonitorDPIHelper::SetPerMonitorDPIAware()
  25. {
  26. auto result = SetProcessDpiAwareness(PROCESS_DPI_AWARENESS::PROCESS_PER_MONITOR_DPI_AWARE);
  27. if (S_OK != result)
  28. {
  29. return FALSE;
  30. }
  31. return TRUE;
  32. }
  33. //Returns the DPI of the window handle passed in the parameter
  34. double NativeHelpers::PerMonitorDPIHelper::GetDpiForWindow(IntPtr hwnd)
  35. {
  36. return GetDpiForHwnd(static_cast<HWND>(hwnd.ToPointer()));
  37. }
  38. double NativeHelpers::PerMonitorDPIHelper::GetDpiForHwnd(HWND hWnd)
  39. {
  40. auto monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
  41. UINT newDpiX;
  42. UINT newDpiY;
  43. if (FAILED(GetDpiForMonitor(monitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI, &newDpiX, &newDpiY)))
  44. {
  45. newDpiX = 96;
  46. newDpiY = 96;
  47. }
  48. return ((double) newDpiX);
  49. }
  50. //Returns the system DPI
  51. double NativeHelpers::PerMonitorDPIHelper::GetSystemDPI()
  52. {
  53. int newDpiX(0);
  54. auto hDC = GetDC(NULL);
  55. newDpiX = GetDeviceCaps(hDC, LOGPIXELSX);
  56. ReleaseDC(NULL, hDC);
  57. return (double)newDpiX;
  58. }