如何在c#console应用程序中获取系统dpi设置 [英] how to get the system dpi settings in c# console application

查看:190
本文介绍了如何在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.

推荐答案

你可以试试这个:



You can try this:

using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
    float dpiX = graphics.DpiX;
    float dpiY = graphics.DpiY;
}





注意:您需要对System.Drawing程序集的引用。


试试这个:



Try this:

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...


该值存储在注册表中



对于XP,它是在

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \FontDPI LogPixels



你可以从控制台窗口句柄创建一个Graphics对象:

The value is stored in the registry

For XP it''s at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI LogPixels

You could create a Graphics object from the console window handle:
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设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆