Pit log: Windows Native API with DPI scaling

Source: Internet
Author: User
Tags textout

High-DPI displays are becoming increasingly popular, and software naturally adapts to this change, as well as a recent internship with a dpi scaling issue. Because the internal framework of a control has a bug, will lead to the display of the content of the problem, and then really can not be changed to use the Windows Native API to define their own fonts, but this write a problem, originally in the internal development machine 100% put down good, a run to my own WIN10, Zoom 125% on the 2K screen and the font will appear unhealthy (the font becomes too large).

Window Vista after the system can be directly to a setprocessdpiawareness to control the DPI problem, but this function is not very useful, or there is no way to precisely control the scale, and this function only in Windows More than 8.1 of the system can be used (Setprocessdpiaware , but also must be Windows Vista above the system), in case our program needs to run on XP? That would require a different approach.

In fact, this method is also very simple, is to use getdevicecaps to get the current environment handle DPI can be, and then the default DPI (96) to do the operation, get the dpi we really want

 Casewm_paint:{paintstruct PS; HDC HDC= BeginPaint (hwnd,&PS); Auto Curdpix=GetDeviceCaps (hdc, LOGPIXELSX); Auto Curdpiy=GetDeviceCaps (hdc, logpixelsy); Std::wstring Str (L"Hello World");      LOGFONT LF;      Hgdiobj Hobject; ZeroMemory (&LF,sizeof(LOGFONT)); F.lfcharset=Gb2312_charset; Lf.lfwidth= MulDiv ( -, the, Curdpix); Lf.lfheight= MulDiv ( -, the, CURDPIY); Lf.lfpitchandfamily=Variable_pitch; swprintf_s (Lf.lffacename, _countof (lf.lffacename), L"Microsoft Ya-Black"); Hobject= SelectObject (hdc, CreateFontIndirect (&LF)); TextOut (HDC, +, -, Str.data (), str.length ());       DeleteObject (SelectObject (hdc, hobject)); Lf.lfwidth= -; Lf.lfheight= -; Hobject= SelectObject (hdc, CreateFontIndirect (&LF)); TextOut (HDC, +, $, Str.data (), str.length ());       DeleteObject (SelectObject (hdc, hobject)); EndPaint (hwnd,&PS); return 0;}

The demonstration here is to output Hello wolrd on the screen, now assume that our font is magnified, but assuming that our other controls are not magnified, then the word in the control will be displayed abnormal, this time to reduce the size of the word, to be similar to 100%, you need muldiv (size, 96 , curdpi); once, the font is shrunk.

As we can see, the second line is magnified at 120% magnification with a height of 55, a width of 22 font, the font is large, we use the method above to reduce the font back to normal size

Pit log: Windows Native API with DPI scaling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.