Search code examples
vb.netvisual-studio-2010resolutionmonitor

Getting the optimal resolution in Visual Studio 2010


Is there any way of getting the monitors optimal resolution in vb?


Solution

  • Optimal Resolution Based on Advices

    If you want the optimal resolution for a CRT then you can follow Microsoft's advice (because there is no native resolution for a CRT):

    For a CRT monitor, it's important to change the screen resolution to the highest resolution available that provides 32-bit color and at least a 72-Hertz refresh rate.

    If you want the optimal resolution for a LCD monitor then you have to use its native resolution. Usually it's the highest resolution available for the monitor.

    Ways of Getting Optimal Resolution

    You can get resolution information from various sources:

    1. Windows API (with User32.dll)
    2. DirectX (using the SDK)
    3. Windows Management Instrumentation (a.k.a. WMI)

    Optimal Resolution Based on VB Code

    I'll use some WMI to query the resolution informations. We can use the Win32_VideoSettings class which gives us available resolutions by video controller but I had some funny/sad/empty results with it. For this one, I'm using the CIM_VideoControllerResolution class directly to get the maximum resolution:

    Imports System.Management
    '...
        Public Function GetMaximumResolution() As ManagementObject
    
                Dim className As String = "CIM_VideoControllerResolution"
                Dim computerName As String = "."
    
                Dim managementPath As New ManagementPath("\\" & computerName & "\root\cimv2:" & className)
                Dim scope As New ManagementScope(managementPath)
    
                Dim videoCtrlrRes As ManagementObjectCollection
                Using searcher As ManagementObjectSearcher = New ManagementObjectSearcher("select * from " & className)
                    searcher.Scope = scope
                    videoCtrlrRes = searcher.Get()
                End Using
    
                Dim videoCtrlrResList As New List(Of ManagementObject)
    
                For Each videoCtrlResItem In videoCtrlrRes
                    'Console.WriteLine(videoCtrlResItem("Description"))
                    videoCtrlrResList.Add(videoCtrlResItem)
                Next
    
                Dim maximumResolution As ManagementObject = videoCtrlrResList.
                    OrderBy(Function(vidSetting) vidSetting("HorizontalResolution")).
                    ThenBy(Function(vidSetting) vidSetting("VerticalResolution")).
                    ThenBy(Function(vidSetting) vidSetting("NumberOfColors")).
                    LastOrDefault()
                'Console.WriteLine(maximumResolution("Description"))
    
                Return maximumResolution
            End Function
    

    Note: if you want to get the resfresh rate, you can get it with the 'RefreshRate' property (ex. vidSetting("RefreshRate") )