如何给一个固定宽度为17.5厘米的文本框?

Nat*_*ium 9 c# winforms

我有一个带文本框的应用程序,屏幕上文本框的宽度在用户屏幕上必须始终为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 105021.5厘米1024×768.

我究竟做错了什么?

Xen*_*nan 7

方法graphics.DpiX不会给出显示器每英寸的真实点数.它返回Windows显示属性中设置的DPI,主要是96或120 DPI.

无法读取显示器的实际DPI.微软确实对Windows Vista/7进行了研究,但只要显示器制造商不提供从显示器硬件读取值的标准方法,就不可能实现.