我有一个带文本框的应用程序,屏幕上文本框的宽度在用户屏幕上必须始终为17.5厘米.
这是我到目前为止所尝试的:
const double centimeter = 17.5; // the width I need
const double inches = centimeter * 0.393700787; // convert centimeter to inches
float dpi = GetDpiX(); // get the dpi. 96 in my case.
var pixels = dpi*inches; // this should give me the amount of pixels
textbox1.Width = Convert.ToInt32(pixels); // set it. Done.
private float GetDpiX()
{
floar returnValue;
Graphics graphics = CreateGraphics();
returnValue = graphics.DpiX;
graphics.Dispose(); // don’t forget to release the unnecessary resources
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
但这给了我不同尺寸和不同分辨率.
它给了我13厘米与1680 X 1050和21.5厘米与1024×768.
我究竟做错了什么?
方法graphics.DpiX不会给出显示器每英寸的真实点数.它返回Windows显示属性中设置的DPI,主要是96或120 DPI.
无法读取显示器的实际DPI.微软确实对Windows Vista/7进行了研究,但只要显示器制造商不提供从显示器硬件读取值的标准方法,就不可能实现.
| 归档时间: |
|
| 查看次数: |
2002 次 |
| 最近记录: |