本文介绍了如何在c#console应用程序中获取系统dpi设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在c#console应用程序中获取系统dpi设置。无论是dpi是90还是120或其他一些自定义设置。
How to get the system dpi settings in c# console application. Whether the dpi is 90 or 120 or some other custom setting.
推荐答案
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
float dpiX = graphics.DpiX;
float dpiY = graphics.DpiY;
}
注意:您需要对System.Drawing程序集的引用。
using System;
using System.Runtime.InteropServices;
namespace DPI
{
class Program
{
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
/// <summary>
/// Logical pixels inch in X
/// </summary>
const int LOGPIXELSX = 88;
/// <summary>
/// Logical pixels inch in Y
/// </summary>
const int LOGPIXELSY = 90;
static void Main(string[] args)
{
IntPtr hdc = GetDC(IntPtr.Zero);
Console.WriteLine(GetDeviceCaps(hdc, LOGPIXELSX));
// or
Console.WriteLine(GetDeviceCaps(hdc, LOGPIXELSY));
Console.ReadKey();
}
}
}
无需额外参考。但是如果导入gdi32和user32 dll更好吗?我不知道 - 马尔科夫和艾伦N'的解决方案看起来更优雅......
No additional references needed. But if it''s better to import gdi32 and user32 dll? I dont know - markovl''s and Alan N''s solutions look more elegant...
using (Graphics g = Graphics.FromHwnd(Process.GetCurrentProcess().MainWindowHandle)) {
Console.WriteLine("X {0:F0}dpi, Y {1:F0}dpi", g.DpiX, g.DpiY);
}
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion\FontDPI")) {
Console.WriteLine(key.GetValue("LogPixels"));
}
Alan。
Alan.
这篇关于如何在c#console应用程序中获取系统dpi设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!