当前位置:网站首页>C DPI adaptation problem

C DPI adaptation problem

2022-06-23 07:03:00 Wool leek

C# DPI Adaptation problem ( Some computers )

What is? DPI?

The full name is dots per inch (DPI), Points per inch , On the monitor, it is the number of pixels per inch ,Window In general, the default is 96 dpi As 100% Zoom ratio of , However, it should be noted that this value may not be the real physical value of the display , It's just Windows One of our reference standards .

When we don't want our interface to follow DPI When you change , It can make windows Ignore our program , The code is as follows :

//  Before running the whole program, open the first winform In the initialization of , Just execute this method 
 public static int ignoreDPI()
        {
            SetProcessDPIAware();  // important 
            IntPtr screenDC = GetDC(IntPtr.Zero);
            int dpi_x = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSX);
            int dpi_y = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSY);
            ReleaseDC(IntPtr.Zero, screenDC);

            return dpi_x;
        }

        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr ptr);

        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);

        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex // index of capability
        );

        [DllImport("user32.dll")]
        internal static extern bool SetProcessDPIAware();

        const int LOGPIXELSX = 88;
        const int LOGPIXELSY = 90;

This is the time , The text in some controls is not displayed correctly , It's because of the font size setting

Now let's think about why DPI Set it high , The font we see will get bigger ? Because the system font is in a fixed size ( Song style 10 No , Physical dimensions are (10/72) Inch ) The design of the , When we DPI Set it high , It means that the font should occupy more pixels , On the premise that the screen resolution remains unchanged , It looks big . So if we set it high DPI, Usually it also means that our monitor is of high resolution , The font inside looks too small , We need to improve DPI To enlarge the content .

If we use system Fonts ( Such as GetStockObject(DEFAULT_GUI_FONT)), In this case, we don't have to worry about , Because the system will set the font at high DPI The corresponding amplification is carried out ; If we were to use CreateFont Self created fonts , Then we have to enlarge the font by ourselves .

summary : If you don't want to be in trouble , You can use it directly windows System font , Such as ( Microsoft YaHei , 12pt / Microsoft YaHei , 10.5pt etc. )

Demo download https://download.csdn.net/download/qq_24905875/10681521

原网站

版权声明
本文为[Wool leek]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230531031705.html