代码之家  ›  专栏  ›  技术社区  ›  Timwi

C:获得完整的桌面尺寸?

  •  72
  • Timwi  · 技术社区  · 15 年前

    如何确定整个桌面的大小? 不是 “工作区”和 “屏幕分辨率”,两者都只指一个屏幕。我想找出虚拟桌面的总宽度和高度,每个监视器只显示一部分。

    9 回复  |  直到 6 年前
        1
  •  125
  •   Dennis    13 年前

    您有两种选择:

    1. PresentationFramework.dll演示

      SystemParameters.VirtualScreenWidth   
      SystemParameters.VirtualScreenHeight
      
    2. system.windows.forms.dll(系统.windows.forms.dll)

      SystemInformation.VirtualScreen.Width   
      SystemInformation.VirtualScreen.Height
      

    使用 第一购股权 如果您正在开发一个WPF应用程序。

        2
  •  28
  •   P Daddy    9 年前

    我认为是时候用一点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);
    
        3
  •  16
  •   Kirk Woll    14 年前

    检查:

    SystemInformation.VirtualScreen.Width
    SystemInformation.VirtualScreen.Height
    
        4
  •  7
  •   Andreas    8 年前

    要获得监视器的物理像素大小,您可以使用它。

    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);
        }
    
    
    }
    
        5
  •  6
  •   Kirk Woll    9 年前

    这并不能回答问题,但只是在所有屏幕中增加了对窗口点(位置)的额外了解。

    使用下面的代码找出一个点(例如窗口的最后一个已知位置)是否在整个桌面的边界内。否则,将窗口位置重置为默认位置 PBaseloc公司 ;

    代码不包含在任务栏或其他工具栏中,而是你自己在那里。

    示例使用:将窗口位置保存到数据库 A站 . 用户登录到 B站 使用2个监视器并将窗口移动到第二个监视器,注销并保存新位置。回到 A站 如果不使用上述代码,窗口将不会显示。

    我的进一步解决方案实现了将用户ID和站点的IP(&winloc)保存到给定应用程序的数据库或本地用户首选项文件中,然后加载到该站点应用程序的用户首选项中。

    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;
    
        6
  •  1
  •   Irshad huMpty duMpty    9 年前

    你可以使用 System.Drawing .

    您可以创建这样的函数

    public System.Windows.Form.Screen[] GetScreens(){
        Screen[] screens = Screen.AllScreens;
        return screens;
    }
    

    然后你可以得到屏幕1,2,等等,在这样的变量中:

    System.Windows.Form.Screen[] screens = func.GetScreens();
    System.Windows.Form.Screen screen1 = screens[0];
    

    然后你可以得到屏幕的边界:

    System.Drawing.Rectangle screen1Bounds = screen1.Bounds;
    

    使用此代码,您将获得 Width , Height 等。

        7
  •  1
  •   core.tweaks    8 年前

    我认为获得“真实”屏幕大小的最佳方法是直接从视频控制器获取值。


        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);
                    }
                }
            }
    }
    

    这应该可以完成这项工作;) 问候语。。。

        8
  •  1
  •   dynamichael    8 年前

    此方法返回包含所有屏幕边界的矩形,方法是使用左上角的最小值和右下角的最大值…

    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);
    }
    
        9
  •  0
  •   Konstantin S.    6 年前

    得到 尺寸 没有任何依赖关系的虚拟显示

    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);
    }