[英]C#: Get complete desktop size?
如何找出整個桌面的大小? 不是“工作區域”也不是“屏幕分辨率”,兩者都只指一個屏幕。 我想找出每個顯示器只顯示一部分的虛擬桌面的總寬度和高度。
您有兩種選擇:
PresentationFramework.dll
SystemParameters.VirtualScreenWidth SystemParameters.VirtualScreenHeight
System.Windows.Forms.dll中
SystemInformation.VirtualScreen.Width SystemInformation.VirtualScreen.Height
如果您正在開發WPF應用程序,請使用第一個選項 。
我認為現在是時候用一點LINQ把這個答案更新了,這樣可以很容易地用一個表達式獲得整個桌面大小。
Console.WriteLine(
Screen.AllScreens.Select(screen=>screen.Bounds)
.Aggregate(Rectangle.Union)
.Size
);
我的原始答案如下:
我想你想要的是這樣的:
int minx, miny, maxx, maxy;
minx = miny = int.MaxValue;
maxx = maxy = int.MinValue;
foreach(Screen screen in Screen.AllScreens){
var bounds = screen.Bounds;
minx = Math.Min(minx, bounds.X);
miny = Math.Min(miny, bounds.Y);
maxx = Math.Max(maxx, bounds.Right);
maxy = Math.Max(maxy, bounds.Bottom);
}
Console.WriteLine("(width, height) = ({0}, {1})", maxx - minx, maxy - miny);
請記住,這並不能說明整個故事。 多個監視器可以交錯排列,或者以非矩形形狀排列。 因此,可能不是(minx,miny)和(maxx,maxy)之間的所有空間都是可見的。
編輯:
我剛剛意識到使用Rectangle.Union
代碼可能會更簡單:
Rectangle rect = new Rectangle(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue);
foreach(Screen screen in Screen.AllScreens)
rect = Rectangle.Union(rect, screen.Bounds);
Console.WriteLine("(width, height) = ({0}, {1})", rect.Width, rect.Height);
校驗:
SystemInformation.VirtualScreen.Width
SystemInformation.VirtualScreen.Height
這並沒有回答這個問題,只是增加了對所有屏幕內窗口點(位置)的額外見解。
使用下面的代碼來確定Point(例如,最后一個已知的窗口位置)是否在整個桌面的范圍內。 如果沒有,將窗口的位置重置為默認的pBaseLoc ;
代碼不考慮TaskBar或其他工具欄,你自己在那里。
示例使用:將窗口位置從站A保存到數據庫。 用戶使用2個監視器登錄到B站並將窗口移動到第2個監視器,注銷保存新位置。 返回站A ,除非使用上述代碼,否則不會顯示窗口。
我進一步解決了將userID和工作站的IP(&winLoc)保存到給定應用程序的數據庫或本地用戶prefs文件,然后加載該工作站和應用程序的用戶首選項。
Point pBaseLoc = new Point(40, 40)
int x = -500, y = 140;
Point pLoc = new Point(x, y);
bool bIsInsideBounds = false;
foreach (Screen s in Screen.AllScreens)
{
bIsInsideBounds = s.Bounds.Contains(pLoc);
if (bIsInsideBounds) { break; }
}//foreach (Screen s in Screen.AllScreens)
if (!bIsInsideBounds) { pLoc = pBaseLoc; }
this.Location = pLoc;
要獲得監視器的物理像素大小,您可以使用它。
static class DisplayTools
{
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
private enum DeviceCap
{
Desktopvertres = 117,
Desktophorzres = 118
}
public static Size GetPhysicalDisplaySize()
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
int physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.Desktopvertres);
int physicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.Desktophorzres);
return new Size(physicalScreenWidth, physicalScreenHeight);
}
}
您可以使用System.Drawing
的Bounds。
您可以創建這樣的函數
public System.Windows.Form.Screen[] GetScreens(){
Screen[] screens = Screen.AllScreens;
return screens;
}
而且你可以在這樣的變量中獲得屏幕一,二等等:
System.Windows.Form.Screen[] screens = func.GetScreens();
System.Windows.Form.Screen screen1 = screens[0];
然后你可以得到屏幕的界限:
System.Drawing.Rectangle screen1Bounds = screen1.Bounds;
使用此代碼,您將獲得所有屬性,如Width
, Height
等。
此方法使用Left和Top的最低值以及Right和Bottom的最高值返回包含所有屏幕邊界的矩形...
static Rectangle GetDesktopBounds() {
var l = int.MaxValue;
var t = int.MaxValue;
var r = int.MinValue;
var b = int.MinValue;
foreach(var screen in Screen.AllScreens) {
if(screen.Bounds.Left < l) l = screen.Bounds.Left ;
if(screen.Bounds.Top < t) t = screen.Bounds.Top ;
if(screen.Bounds.Right > r) r = screen.Bounds.Right ;
if(screen.Bounds.Bottom > b) b = screen.Bounds.Bottom;
}
return Rectangle.FromLTRB(l, t, r, b);
}
我認為獲得“真實”屏幕大小的最佳方法是直接從視頻控制器獲取值。
using System;
using System.Management;
using System.Windows.Forms;
namespace MOS
{
public class ClassMOS
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_VideoController");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("CurrentHorizontalResolution: {0}", queryObj["CurrentHorizontalResolution"]);
Console.WriteLine("-----------------------------------");
Console.WriteLine("CurrentVerticalResolution: {0}", queryObj["CurrentVerticalResolution"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
這應該做的工作;)問候......
獲取沒有任何依賴關系的虛擬顯示的大小
public enum SystemMetric
{
VirtualScreenWidth = 78, // CXVIRTUALSCREEN 0x0000004E
VirtualScreenHeight = 79, // CYVIRTUALSCREEN 0x0000004F
}
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(SystemMetric metric);
public static Size GetVirtualDisplaySize()
{
var width = GetSystemMetrics(SystemMetric.VirtualScreenWidth);
var height = GetSystemMetrics(SystemMetric.VirtualScreenHeight);
return new Size(width, height);
}
派生自 SM_XVIRTUALSCREEN 等的值是物理坐標,不等於控制面板中屏幕縮放 125%、150%、175% 的邏輯坐標。
您會發現許多窗口操作尚不可用或可能永遠無法使用。 我使用 PInvoke.User32 nuget 包取得了最大的成功。
var screenX = PInvoke.User32.GetSystemMetrics(PInvoke.User32.SystemMetric.SM_CXSCREEN);
var screenY = PInvoke.User32.GetSystemMetrics(PInvoke.User32.SystemMetric.SM_CYSCREEN);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.