取DPI 缩放比例 HWND wnd = ::GetDesktopWindow(); dbg_print("desktopwnd:0x%X\n",wnd); HDC dc = GetDC(wnd); int desktopVerts = GetDeviceCaps(dc, DESKTOPVERTRES ); dbg_print("DESKTOPVERTRES:%d\n",desktopVerts); int verts = GetDeviceCaps(dc, VERTRES ); dbg_print("VERTRES:%d\n",verts); dpiScaling = desktopVerts*100 / verts; ReleaseDC(dc); 计算缩放参照 void dpiScale(RECT& r) { r.left = r.left * dpiScaling / 100; r.right = r.right * dpiScaling / 100; r.top = r.top * dpiScaling / 100; r.bottom = r.bottom * dpiScaling / 100; } 以上就是获取DPI缩放比例和计算参照,注意:由于是整数计算,比例被先放大了100倍,这里需要换算回去,以避免整数除法带来的较大误差
后加:
HWND wnd = ::GetDesktopWindow(); HDC dc = ::GetDC(wnd); double PIXX = GetDeviceCaps(dc, LOGPIXELSX); double rate = PIXX / 96;