C#:获取完整的桌面大小?
如何知道整个桌面的大小? 不是“工作区域”,也不是“屏幕分辨率”,两者都仅指一个屏幕。我想找出每个显示器仅显示一部分的虚拟桌面的总宽度和高度。
How do I find out the size of the entire desktop? Not the "working area" and not the "screen resolution", both of which refer to only one screen. I want to find out the total width and height of the virtual desktop of which each monitor is showing only a part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您有两个选择:
PresentationFramework.dll
System.Windows.Forms.dll
如果您开发 WPF 应用程序,请使用第一个选项。
You have two options:
PresentationFramework.dll
System.Windows.Forms.dll
Use the first option if you developing a WPF application.
我认为是时候用一点 LINQ 来更新这个答案,这使得使用单个表达式可以轻松获取整个桌面大小。
我原来的答案如下:
我猜你想要的是这样的:
请记住,这并不能说明整个故事。多个监视器可以交错排列,或者排列成非矩形形状。因此,可能并非 (minx, miny) 和 (maxx, maxy) 之间的所有空间都是可见的。
编辑:
我刚刚意识到使用
Rectangle.Union
代码可以更简单一些:I think it's time to bring this answer up to date with a little LINQ, which makes it easy to get the entire desktop size with a single expression.
My original answer follows:
I guess what you want is something like this:
Keep in mind that this doesn't tell the whole story. It is possible for multiple monitors to be staggered, or arranged in a nonrectangular shape. Therefore, it may be that not all of the space between (minx, miny) and (maxx, maxy) is visible.
EDIT:
I just realized that the code could be a bit simpler using
Rectangle.Union
:查看:
Check:
要获取显示器的物理像素大小,您可以使用它。
To get the physical pixel size of the monitor you can use this.
这并没有回答问题,而只是增加了对窗口在所有屏幕中的点(位置)的额外了解。
使用下面的代码来查明某个点(例如窗口的最后已知位置)是否在整个桌面的范围内。如果没有,请将窗口的位置重置为默认的pBaseLoc;
代码不考虑任务栏或其他工具栏,你自己在那里。
使用示例:将站 A 的窗口位置保存到数据库中。用户登录到具有 2 个显示器的 B 站,并将窗口移动到第二个显示器,注销并保存新位置。返回A站,除非使用上述代码,否则不会显示该窗口。
我的进一步解决方案是,将用户 ID 和电台的 IP(& winLoc)保存到给定应用程序的数据库或本地用户首选项文件中,然后加载该电台的用户首选项 & 。应用程序。
This doesn't answer the question, but merely adds additional insight on a window's Point (location) within all the screens).
Use the code below to find out if a Point (e.g. last known Location of window) is within the bounds of the overall Desktop. If not, reset the window's Location to the default pBaseLoc;
Code does not account for the TaskBar or other Toolbars, yer on yer own there.
Example Use: Save the Window location to a database from station A. User logs into station B with 2 monitors and moves the window to the 2nd monitor, logs out saving new location. Back to station A and the window wouldn't be shown unless the above code is used.
My further resolve implemented saving the userID and station's IP (& winLoc) to database or local user prefs file for a given app, then load in user pref for that station & app.
我认为获得“真实”屏幕尺寸的最佳方法是直接从视频控制器获取值。
这应该可以完成工作;)
问候 ...
I think the best way to get the "real" screen-size, is to get the values directly from the video-controller.
This should do the job ;)
Greetings ...
您可以使用
System.Drawing
的边界。您可以创建一个这样的函数
,然后您可以在这样的变量中获取屏幕一、二等:
然后您可以获得屏幕的边界:
使用此代码,您将获得所有属性,例如
Width
、高度
等。You can use the Bounds of
System.Drawing
.You can create a function like this
and than you can get the screen one, two, etc. in a variable like this:
then you can get the bounds of the screen:
With this code you will get all the properties like
Width
,Height
, etc.此方法通过使用“左”和“上”的最低值以及“右”和“下”的最高值来返回包含所有屏幕边界的矩形...
This method returns the rectangle that contains all of the screens' bounds by using the lowest values for Left and Top, and the highest values for Right and Bottom...
获取虚拟显示器的大小,无需任何依赖项
Get the Size of a virtual display without any dependencies
从 SM_XVIRTUALSCREEN 等派生的值是物理坐标,不等于控制面板中屏幕缩放 125%、150%、175% 时的逻辑坐标。
Values, derived from SM_XVIRTUALSCREEN and so on are physical coordinates and not equals to logical coordinates at screen scaling 125%, 150%, 175% in Control Panel.
您会发现许多窗口操作尚不可用或可能永远不可用。我在 PInvoke.User32 nuget 包方面取得了最大的成功。
You'll find that many window manipulations are not yet available or may not ever be made available. I've had the greatest success with PInvoke.User32 nuget package.