wpf 查找当前显示器的大小(高度和宽度)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13010357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:53:13  来源:igfitidea点击:

Find Current monitor's Size (Height and Width)

c#.netwpf

提问by Mohanavel

I am using dual monitor and I want to find the Current(where my application is active) monitor's height and width (note : not resolution). How can i achieve this. My WPF application display screen size options based on current screen size.

我正在使用双显示器,我想找到当前(我的应用程序处于活动状态)显示器的高度和宽度(注意:不是分辨率)。我怎样才能做到这一点。我的 WPF 应用程序根据当前屏幕尺寸显示屏幕尺寸选项。

回答by Grass the Dinosaur

This answer has two parts - please don't judge. The new one I'm quite uncertain but will give you better results. The older one I'm sure works somehow.

这个答案有两部分 - 请不要判断。新的我很不确定,但会给你更好的结果。我确信旧的以某种方式起作用。

New answer

新答案

I think you've missed an incredibly important answer in one of the questions you viewed. There was an answer using the Graphics.DpiXand Graphics.DpiYby dividing them to the pixel resolution's x and y values respectively. Read it right here! Don't forget to read through the comments as well.

我认为您在查看的问题之一中错过了一个非常重要的答案。有一个使用Graphics.DpiX和的答案,方法是Graphics.DpiY将它们分别除以像素分辨率的 x 和 y 值。在这里阅读!不要忘记阅读评论。

Because the amount of pixels in an inch differs in different monitors, this dude found out that Graphics.DpiXis the the amount of pixels in the X axis required to make an inch. The same goes for Graphics.DpiYwhich obviously points the pixels per inch in the y axis. In order to find the inches from the resolution, you divide these two. You can read more on the usage hereand here. The second link is in Visual Basic but, imagining that you have previous C# knowledge you'd be able to figure out the syntax.

因为一英寸的像素数量在不同的显示器上是不同的,这个家伙发现这Graphics.DpiX是制作一英寸所需的 X 轴上的像素数量。这同样适用于Graphics.DpiY这显然指向y轴每英寸的像素。为了从分辨率中找到英寸,您将这两个分开。您可以在此处此处阅读有关用法的更多信息。第二个链接在 Visual Basic 中,但是,假设您具有以前的 C# 知识,您将能够弄清楚语法。

Be warned - I haven't tried any of the following yet!

请注意 - 我还没有尝试过以下任何一项!

Start out by referencing System.Drawing.

从参考开始System.Drawing

Next, theoretically (I haven't tried this out before so please do correct me if you can) you'd need to make a new Graphics class.

接下来,理论上(我之前没有尝试过,所以如果可以的话请纠正我)你需要创建一个新的 Graphics 类。

Graphics DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));

Graphics DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero));

And now, using our new Graphics class that references the monitor in question, we grab the DPI and the screen resolution and divide the screen resolution by the DPI to get how many inches the screen is.

现在,使用引用相关显示器的新 Graphics 类,我们获取 DPI 和屏幕分辨率,然后将屏幕分辨率除以 DPI 以获得屏幕的英寸数。

SystemInformation.PrimaryMonitorSize.Width / Graphics.DpiXfor width and SystemInformation.PrimaryMonitorSize.Height / Graphics.DpiYfor height. You need to assign those two to their respective variables and voila! Width and height of the screen - in inches.

SystemInformation.PrimaryMonitorSize.Width / Graphics.DpiX对于宽度和SystemInformation.PrimaryMonitorSize.Height / Graphics.DpiY高度。您需要将这两个分配给它们各自的变量,瞧!屏幕的宽度和高度 - 以英寸为单位。

Again if I do do something wrong, please tell me. I'm still a novice programmer so if I do something wrong, don't bash me or flame me - can you please suggest the appropriate code if I do so.

再次,如果我做错了什么,请告诉我。我仍然是一个新手程序员,所以如果我做错了什么,不要抨击我或抨击我 - 如果我这样做,你能建议适当的代码吗?


Old answer


旧答案

The person who wrote; try System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Heightit's working in my case.was, in fact, telling you the height, in pixels, however. What you need to do is to convert this integer to inches.

写信的人;尝试System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height在我的情况下工作。然而,实际上是告诉你高度,以像素为单位。您需要做的是将此整数转换为英寸。

One inch is equal to 96 pixels (or 96.0000000000011 to be more exact). So, write the following code;

一英寸等于 96 像素(或更准确地说是 96.0000000000011)。因此,编写以下代码;

    //Height
    Int MonitorHeightinInches = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height / 96;
    //Width
    Int MonitorWidthinInches = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width / 96;

Now you may reuse the code as much as you want, in your application. This works for me.

现在,您可以在您的应用程序中尽可能多地重用代码。这对我有用。

回答by ZombieSheep

You will find it very difficult to get a monitor's physical dimensions. You might be able to get the resolution and DPI and work it out from there, but that won't necessarily give you the correct answer. Imagine a projector - you may know it's resolution and DPI but the physical size of the displayed image will depend on the distance to the screen. It's not just projectors, though. The manufacturing processes per monitor may result in slightly off figures, for example.

您会发现很难获得显示器的物理尺寸。您可能能够获得分辨率和 DPI 并从那里解决,但这不一定会给您正确的答案。想象一下投影仪 - 您可能知道它的分辨率和 DPI,但显示图像的物理尺寸将取决于到屏幕的距离。不过,这不仅仅是投影仪。例如,每台显示器的制造过程可能会导致数字略有偏差。

I would be very tempted to remove any reference to physical sizes and make a flowing layout that resizes adequately according to the resolution and DPI capabilities of your display.

我很想删除对物理尺寸的任何引用,并制作一个流畅的布局,根据您的显示器的分辨率和 DPI 功能充分调整大小。

回答by TylerHaigh

If you're feeling adventurous, have a look in the registry keys. Specifically HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY and loop through the displays (or better still just the active display) and use the EDIDinformation to get the screens width and height (and by Pythagoras theorem, the diagonal length) of the monitor

如果您喜欢冒险,请查看注册表项。特别是 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY 并循环显示(或者更好的只是活动显示)并使用EDID信息来获取屏幕的宽度和高度(以及毕达哥拉斯定理,对角线长度)显示器

回答by Manish Parakhiya

try

尝试

       System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height

it's working in my case.

它在我的情况下工作。