当前位置:网站首页>C# DPI适配问题
C# DPI适配问题
2022-06-23 05:31:00 【羊毛韭菜】
C# DPI适配问题(部分电脑)
什么是DPI?
全称是dots per inch (DPI), 也就是每英寸的点数,在显示器上就是每英寸的像素个数,Window上一般默认是96 dpi 作为100% 的缩放比率, 但是要注意的是该值未必是真正的显示器物理值, 只是Windows里我们的一个参考标准。
当我们不想要我们的界面随着DPI的改变而改变的时候,可以让windows忽略我们的程序,代码如下:
// 在运行整个程序要打开的第一个winform的初始化里面,执行这个方法就可以了
public static int ignoreDPI()
{
SetProcessDPIAware(); //重要
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;
这个时候,有些控件里面的文字显示的不对,是因为字体大小设置的问题
下面我们思考为什么DPI设置高了之后, 我们看到的字体会变大? 因为系统字体是是以固定大小(宋体10号字,物理尺寸为(10/72)英寸)设计的, 当我们DPI设置高了之后 ,说明该字体要占有更多的像素, 在屏幕分辨率不变的前提下, 看起来也就大了。所以如果我们设置高DPI,通常也意味着我们的显示器是高分辨率, 里面的字体看起来太小了, 我们需要提高DPI来把内容放大。
如果我们用的是系统字体(如GetStockObject(DEFAULT_GUI_FONT)), 那么这种情况下我们不用操心, 因为系统会对该字体在高DPI时进行相应的放大; 如果我们是用CreateFont自己创建的字体, 那就要我们自己对该字体进行放大了。
总结:如果不想麻烦,可以直接使用windows系统字体,如 (微软雅黑, 12pt /微软雅黑, 10.5pt 等)
Demo下载 https://download.csdn.net/download/qq_24905875/10681521
边栏推荐
- Qt使用多线程编译项目的方法
- Index - MySQL
- Docker实战 -- 部署Redis集群与部署微服务项目
- C # database reports errors. Let's have a look
- mysql如何将日期转为数字
- Sklearn classification in sklearn_ Report & accuracy / recall /f1 value
- QT creator builds osgearth environment (osgqt msvc2017)
- 2.17 haas506 2.0开发教程-system(仅支持2.2以上版本)
- Smart port: how to realize intelligent port supervision based on the national standard gb28181 protocol easygbs?
- For non dpdk kvm1.0 machines, set init Maxrxbuffers changed from 256 to 1024 to improve packet receiving capacity
猜你喜欢

综合培养学生脑力思维的少儿编程

qt creater搭建osgearth环境(osgQT MSVC2017)

解读创客教育中的团结协作精神

js创建数组(元素都是对象)

二叉树的遍历及相关知识

业务逻辑安全思路总结

QT creator builds osgearth environment (osgqt msvc2017)

Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control

How to maintain secure encryption of email communication with FDA?

设计师需要懂的数据指标与数据分析模型
随机推荐
Functions and basic structure of CPU
30 data visualization tips that can not be ignored
haas506 2.0开发教程-高级组件库-modem.net(仅支持2.2以上版本)
C# wpf 附加属性实现界面上定义装饰器
c#数据库报错问题大家帮我看看吧
Topic35——34. Find the first and last positions of elements in a sorted array
Synchronous switching power supply reduces EMI layout dv/dt di/dt
Steam教育对国内大学生的影响力
剑指 Offer 42. 连续子数组的最大和
LeetCode笔记:Weekly Contest 298
2022-01-12: give a positive array arr, length N, subscript 0~n-1, a
Coordinate transformation
Network architecture from Wan to sd-wan edge devices
Topic35——34. 在排序数组中查找元素的第一个和最后一个位置
2.17 haas506 2.0 development tutorial system (only versions above 2.2 are supported)
Vs+qt project transferred to QT Creator
279.完全平方数
Programmers' real ideas | daily anecdotes
将TensorFlow1.x改写为pytorch
微信小程序 - 全局监听globalData的某个属性变化,例如监听网络状态切换