Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
モニターの dpi を取得する
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
// 解像度を取得
MONITORINFOEX info;
info.cbSize = sizeof(info);
GetMonitorInfo(hMonitor, &info);
// モニターのデバイスコンテキストを取得する
HDC hdc = CreateDC(TEXT("DISPLAY"), info.szDevice, NULL, NULL);
// 画面解像度を取得
#if 1
int const width_pix = GetDeviceCaps(hdc, HORZRES);
int const height_pix = GetDeviceCaps(hdc, VERTRES);
#else
LONG const width_pix = info.rcMonitor.right - info.rcMonitor.left;
LONG const height_pix = info.rcMonitor.bottom - info.rcMonitor.top;
#endif
double const size_pix = std::sqrt(width_pix * width_pix + height_pix * height_pix);
// 物理サイズを取得 (単位は mm)
int const width_mm = GetDeviceCaps(hdc, HORZSIZE);
int const height_mm = GetDeviceCaps(hdc, VERTSIZE);
DeleteDC(hdc);
// インチサイズに換算
double const width_inch = width_mm / 25.4;
double const height_inch = height_mm / 25.4;
double const size_inch = std::sqrt(width_inch * width_inch + height_inch * height_inch);
// DPI を算出
double const width_dpi = width_pix / width_inch;
double const height_dpi = height_pix / height_inch;
double const size_dpi = size_pix / size_inch;
// 表示
TCHAR buffer[1024];
_stprintf_s(buffer, TEXT("%lf inch: %lf(%lf, %lf)"), size_inch, size_dpi, width_dpi, height_dpi);
::MessageBox(hWnd, buffer, TEXT("dpi"), MB_OK);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment