【C#】获取电脑DPI
public static class DPIGeter
{
/// <summary>
/// 获取DPI
/// </summary>
/// <param name="dpix"></param>
/// <param name="dpiy"></param>
public static void GetDPI(ref float dpix, ref float dpiy)
{
SetProcessDPIAware();//此处会忽视系统DWM虚拟化,不建议使用
IntPtr screenDC = GetDC(IntPtr.Zero);
dpix = GetDeviceCaps(screenDC, LOGPIXELSX);
dpiy = GetDeviceCaps(screenDC, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, screenDC);
}
/// <summary>
/// 获取DPI缩放比例
/// </summary>
/// <param name="dpiscalex"></param>
/// <param name="dpiscaley"></param>
public static void GetDPIScale(ref float dpiscalex, ref float dpiscaley)
{
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
IntPtr hdc = GetDC(IntPtr.Zero);
int w = GetDeviceCaps(hdc, DESKTOPHORZRES);
int h = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
dpiscalex = (float)w / x;
dpiscaley = (float)h / y;
}
[DllImport("user32.dll")]
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 IntPtr CreateDC(
string lpszDriver, // driver name
string lpszDevice, // device name
string lpszOutput, // not used; should be NULL
Int64 lpInitData // optional printer data
); [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(); [DllImport("user32")]
public static extern int GetSystemMetrics(
int nIndex
); [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect); public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
} const int DRIVERVERSION = ;
const int TECHNOLOGY = ;
const int HORZSIZE = ;
const int VERTSIZE = ;
const int HORZRES = ;
const int VERTRES = ;
const int BITSPIXEL = ;
const int PLANES = ;
const int NUMBRUSHES = ;
const int NUMPENS = ;
const int NUMMARKERS = ;
const int NUMFONTS = ;
const int NUMCOLORS = ;
const int PDEVICESIZE = ;
const int CURVECAPS = ;
const int LINECAPS = ;
const int POLYGONALCAPS = ;
const int TEXTCAPS = ;
const int CLIPCAPS = ;
const int RASTERCAPS = ;
const int ASPECTX = ;
const int ASPECTY = ;
const int ASPECTXY = ;
const int SHADEBLENDCAPS = ;
const int LOGPIXELSX = ;
const int LOGPIXELSY = ;
const int SIZEPALETTE = ;
const int NUMRESERVED = ;
const int COLORRES = ;
const int PHYSICALWIDTH = ;
const int PHYSICALHEIGHT = ;
const int PHYSICALOFFSETX = ;
const int PHYSICALOFFSETY = ;
const int SCALINGFACTORX = ;
const int SCALINGFACTORY = ;
const int VREFRESH = ;
const int DESKTOPVERTRES = ;
const int DESKTOPHORZRES = ;
const int BLTALIGNMENT = ;
const int SM_CXSCREEN = ;//'屏幕宽度
const int SM_CYSCREEN = ;//'屏幕高度
const int SM_CXVSCROLL = ;//'垂直滚动条的宽度
const int SM_CYHSCROLL = ;//'水平滚动条的宽度
const int SM_CYCAPTION = ;//'Height of windows caption 实际标题高度加上SM_CYBORDER
const int SM_CXBORDER = ;//'Width of no-sizable borders 无法测量的窗口框架宽度
const int SM_CYBORDER = ;// 'Height of non-sizable borders 无法测量的窗口框架高度
const int SM_CXDLGFRAME = ;// 'Width of dialog box borders
const int SM_CYDLGFRAME = ;//'Height of dialog box borders
const int SM_CYHTHUMB = ;//'Height of scroll box on horizontal scroll bar 水平滚动条上滑块的高度
const int SM_CXHTHUMB = ;//' Width of scroll box on horizontal scroll bar 水平滚动条上滑块的宽度
const int SM_CXICON = ;// 'Width of standard icon 图标宽度
const int SM_CYICON = ;//'Height of standard icon 图标高度
const int SM_CXCURSOR = ;//'Width of standard cursor 光标宽度
const int SM_CYCURSOR = ;//'Height of standard cursor 光标高度
const int SM_CYMENU = ;// 'Height of menu 以像素计算的单个菜单条的高度
const int SM_CXFULLSCREEN = ;// 'Width of client area of maximized window
const int SM_CYFULLSCREEN = ;// 'Height of client area of maximized window
const int SM_CYKANJIWINDOW = ;//'Height of Kanji window
const int SM_MOUSEPRESENT = ;//'True is a mouse is present 如果为TRUE或不为0的值则安装了鼠标,否则没有安装。
const int SM_CYVSCROLL = ;//'Height of arrow in vertical scroll bar
const int SM_CXHSCROLL = ;//'Width of arrow in vertical scroll bar
const int SM_DEBUG = ;//'True if deugging version of windows is running
const int SM_SWAPBUTTON = ;//'True if left and right buttons are swapped.
const int SM_CXMIN = ;// 'Minimum width of window
const int SM_CYMIN = ;//'Minimum height of window
const int SM_CXSIZE = ;//'Width of title bar bitmaps
const int SM_CYSIZE = ;//'height of title bar bitmaps
const int SM_CXMINTRACK = ;// 'Minimum tracking width of window
const int SM_CYMINTRACK = ;//'Minimum tracking height of window
const int SM_CXDOUBLECLK = ;// 'double click width
const int SM_CYDOUBLECLK = ;// 'double click height
const int SM_CXICONSPACING = ;//'width between desktop icons
const int SM_CYICONSPACING = ;//'height between desktop icons
const int SM_MENUDROPALIGNMENT = ;//'Zero if popup menus are aligned to the left of the memu bar item. True if it is aligned to the right.
const int SM_PENWINDOWS = ;// 'The handle of the pen windows DLL if loaded.
const int SM_DBCSENABLED = ;// 'True if double byte characteds are enabled
const int SM_CMOUSEBUTTONS = ;//'Number of mouse buttons.
const int SM_CMETRICS = ;// 'Number of system metrics
const int SM_CLEANBOOT = ;//'Windows 95 boot mode. 0 = normal, 1 = safe, 2 = safe with network
const int SM_CXMAXIMIZED = ;//'default width of win95 maximised window
const int SM_CXMAXTRACK = ;// 'maximum width when resizing win95 windows
const int SM_CXMENUCHECK = ;// 'width of menu checkmark bitmap
const int SM_CXMENUSIZE = ;//'width of button on menu bar
const int SM_CXMINIMIZED = ;// 'width of rectangle into which minimised windows must fit.
const int SM_CYMAXIMIZED = ;//'default height of win95 maximised window
const int SM_CYMAXTRACK = ;//'maximum width when resizing win95 windows
const int SM_CYMENUCHECK = ;//'height of menu checkmark bitmap
const int SM_CYMENUSIZE = ;//'height of button on menu bar
const int SM_CYMINIMIZED = ;// 'height of rectangle into which minimised windows must fit.
const int SM_CYSMCAPTION = ;// 'height of windows 95 small caption
const int SM_MIDEASTENABLED = ;// 'Hebrw and Arabic enabled for windows 95
const int SM_NETWORK = ;//'bit o is set if a network is present.
const int SM_SECURE = ;//'True if security is present on windows 95 system
const int SM_SLOWMACHINE = ;// 'true if machine is too slow to run win95.
}
typedef enum _Process_DPI_Awareness {
Process_DPI_Unaware = 0,
Process_System_DPI_Aware = 1,
Process_Per_Monitor_DPI_Aware = 2
} Process_DPI_Awareness;
【C#】获取电脑DPI的更多相关文章
- C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)
在开发过程中,经常需要获取电脑的一些属性,如获取硬盘ID/CPU序列号/MAC地址作为来加密字符串. 1.硬盘 在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~ 什么叫硬盘序列号?指的是作为一个 ...
- 使用RXTX获取电脑串口
RXTX是javacomm串口通信的一个扩展 RXTX开发所需文件的下载地址:http://rxtx.qbang.org/wiki/index.php/Download 解压之后可以看到支持各个平台的 ...
- C# -- 使用System.Environment获取电脑的相关属性
使用System.Environment获取电脑的相关属性 1.使用System.Environment获取电脑的相关属性(入门案例) static void Main(string[] args) ...
- vue 项目使用 webpack 构建自动获取电脑ip地址
1.开发 H5 时移动端,经常会使用真机进行调试本地环境.webpack 配置服务器好多脚手架写的都是固定的,而在团队开发中需要每人配置自己的本机 ip 进行开发,每次开启开发环境的都需要修改,并且还 ...
- java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名
package com.cloudssaas.util; import java.io.BufferedReader; import java.io.IOException; import java. ...
- QT 获取电脑时间
使用环境: VS2010 & QT Designer5 #include <QDateTime> //包含头文件 QDateTime local(QDateTime::curre ...
- java获取电脑部分信息
获取mac地址与cpu序列号 参考博客:https://www.jb51.net/article/94793.htm 另一篇参考地址没记录下来 package util; import java.io ...
- C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息
摘要 有时需要获取电脑的相关信息.这时可以通过调用windows api的方式,进行获取. 方法 可以通过在powershell中 通过下面的命令进行查询,然后可以通过c#调用获取需要的信息. gwm ...
- Java获取电脑IP、MAC、各种版本
Java代码获取电脑IP.MAC.各种版本 package com.rapoo.middle.action; import java.io.BufferedReader; import java.io ...
随机推荐
- 代码解说Android Scroller、VelocityTracker
在编写自己定义滑动控件时经常会用到Android触摸机制和Scroller及VelocityTracker.Android Touch系统简单介绍(二):实例具体解释onInterceptTouchE ...
- Python rindex() 方法
描述 Python rindex() 方法返回子字符串最后一次出现在字符串中的索引位置,该方法与rfind() 方法一样,只不过如果子字符串不在字符串中会报一个异常. 语法 rindex() 方法语法 ...
- oracle各种常用管理sql及其他 ---待续
启动客户端工具:sqlplus /nolog 使用sysdba链接:conn / as sysdba; select * from dba_users; --查看数据库里面所有用户,前提是你是有dba ...
- Struts如何获取客户端ip地址
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- mysql 5.6 grant授权的时候出现问题
mysql> grant select on huamu_licai.* to 'read'@'%' identified by password 'Abcd1234';ERROR 1827 ( ...
- C++继承 派生类中的内存布局(单继承、多继承、虚拟继承)
今天在网上看到了一篇写得非常好的文章,是有关c++类继承内存布局的.看了之后获益良多,现在转在我自己的博客里面,作为以后复习之用. ——谈VC++对象模型(美)简.格雷程化 译 译者前言 一个C ...
- 【Android】3.9 覆盖物功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 百度地图SDK所提供的地图等级为3-19级(3.7.1版本中有些部分已经提供到了21级),所包含的信 ...
- web 安全问题(一):CSRF 攻击
什么是CSRF CSRF是怎么产生的 CSRF的攻击对象 CSRG的攻击手段 CSRF的防御措施 什么是CSRF 全称是(Cross Site Request Forgery)跨站请求伪造.也就是恶意 ...
- nonatomic对引用计数的影响(非ARC)
@interfaceAppDelegate() { NSObject * obj_; } @property(retain) NSObject * obj;// 默认是atomic //@proper ...
- Solr 数字字符不能搜索的一个问题
问题一: 测试人员告诉我数字不能被搜索.于是开始找原因: <fields> ***<field name="productName" type="tex ...