当前位置:网站首页>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
边栏推荐
- Basic RF theory (DB)
- Shutter style
- vs+qt项目转qt creator
- Qt使用多线程编译项目的方法
- 解读创客教育中的团结协作精神
- The central network and Information Technology Commission issued the National Informatization Plan for the 14th five year plan, and the network security market entered a period of rapid growth
- 如何查看本机IP
- 射频内容学习
- Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)
- Leetcode notes: Weekly contest 298
猜你喜欢

Synchronous switching power supply reduces EMI layout dv/dt di/dt

Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean

图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?

C# wpf 附加属性实现界面上定义装饰器

Day_ 02 smart communication health project - appointment management - inspection item management

C # database reports errors. Let's have a look

中台库存中的实仓与虚仓的业务逻辑设计

射频内容学习

记一次GLIB2.14升级GLIB2.18的记录以及其中的步骤原理

CPU的功能和基本结构
随机推荐
索引——MySQL
【踩坑记录】数据库连接未关闭连接,释放资源的坑
Day_ 01 smart communication health project - project overview and environmental construction
2.17 haas506 2.0 development tutorial system (only versions above 2.2 are supported)
Fastdata pole | insight report on e-commerce consumption of young Chinese users 2021
微信小程序 - 全局监听globalData的某个属性变化,例如监听网络状态切换
Linked Storage
什么是客户体验自动化?
CPU的功能和基本结构
C# wpf 通过绑定实现控件动态加载
SAP execution transaction code mrrl error -no message was found for partner 100065-
数值计算方法 Chapter7. 计算矩阵的特征值和特征向量
Mysql5.6 (5.7-8) is based on shardingsphere5.1.1 sharding proxy mode. Read / write separation
LeetCode笔记:Weekly Contest 298
20220621 Three Conjugates of Dual Quaternions
云盒子联合深信服,为南京一中打造智慧双模教学资源分享平台
haas506 2.0开发教程-高级组件库-modem.sim(仅支持2.2以上版本)
Haas506 2.0 development tutorial - Advanced Component Library -modem Sim (only supports versions above 2.2)
坐标 转化
Qt使用多线程编译项目的方法