当前位置:网站首页>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
边栏推荐
- phpStudy设置301重定向
- 综合培养学生脑力思维的少儿编程
- 把CSMA/CD、Token Bus、Token Ring说清楚
- CPU的功能和基本结构
- Illustration Google V8 18: asynchronous programming (I): how does V8 implement micro tasks?
- haas506 2.0开发教程-hota(仅支持2.2以上版本)
- Explain csma/cd, token bus and token ring clearly
- 问题:访问组件中数据object(定义的数据)中属性也为object对象中的属性时,报错现象
- 什么是客户体验自动化?
- JS to create an array (all elements are objects)
猜你喜欢

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

JS to create an array (all elements are objects)

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

The softing datafeed OPC suite stores Siemens PLC data in an Oracle Database

项目_过滤器Filter解决中文乱码

剑指 Offer 42. 连续子数组的最大和

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)

QT中的item views与Item widgets控件的用法总结

了解学习 JSX 的工作方式

Day_ 01 smart communication health project - project overview and environmental construction
随机推荐
百度URL参数之LINK?URL参数加密解密研究(代码实例)
Link of Baidu URL Parameters? Recherche sur le chiffrement et le décryptage des paramètres d'URL (exemple de Code)
解析创客教育中的造物原理
Mysql5.6 (5.7-8) is based on shardingsphere5.1.1 sharding proxy mode. Read / write separation
设计师需要懂的数据指标与数据分析模型
Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021
2.17 haas506 2.0 development tutorial system (only versions above 2.2 are supported)
Day_ 08 smart health project - mobile terminal development - physical examination appointment
疫情下的传媒产业,小程序生态驱动数字化转型探索
Gridsearchcv (grid search), a model parameter adjuster in sklearn
中台库存中的实仓与虚仓的业务逻辑设计
数值计算方法 Chapter7. 计算矩阵的特征值和特征向量
How to view native IP
Docker practice - redis cluster deployment and micro service deployment project
Day_ 10 smart health project - permission control, graphic report
What is a PDCA cycle? How to integrate PDCA cycle and OKR
2020 smart power plant industry insight white paper
如何查看本机IP
sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)
直播带货这么火,如何在小程序中实现视频通话及直播互动功能?