Click here to Skip to main content
15,883,827 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,
What I need to do is get a list of all supported screen resolutions of my primary monitor, in order to change the resolution to one of the supported modes. Anyone know how to do that ?
I am building this in VB 2010 express.
pls help, thanks in advance.

What I have tried:

ScreenResolution class. The following tells me if a given resolution is supported but does not list all the modes.

VB
<pre>Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class Resolution

    <structlayout(layoutkind.sequential)> _
    Public Structure DEVMODE1
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
        Public dmDeviceName As String
        Public dmSpecVersion As Short
        Public dmDriverVersion As Short
        Public dmSize As Short
        Public dmDriverExtra As Short
        Public dmFields As Integer
        Public dmOrientation As Short
        Public dmPaperSize As Short
        Public dmPaperLength As Short
        Public dmPaperWidth As Short
        Public dmScale As Short
        Public dmCopies As Short
        Public dmDefaultSource As Short
        Public dmPrintQuality As Short
        Public dmColor As Short
        Public dmDuplex As Short
        Public dmYResolution As Short
        Public dmTTOption As Short
        Public dmCollate As Short
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _
        Public dmFormName As String
        Public dmLogPixels As Short
        Public dmBitsPerPel As Short
        Public dmPelsWidth As Integer
        Public dmPelsHeight As Integer
        Public dmDisplayFlags As Integer
        Public dmDisplayFrequency As Integer
        Public dmICMMethod As Integer
        Public dmICMIntent As Integer
        Public dmMediaType As Integer
        Public dmDitherType As Integer
        Public dmReserved1 As Integer
        Public dmReserved2 As Integer
        Public dmPanningWidth As Integer
        Public dmPanningHeight As Integer
    End Structure

    Class User_32

        <dllimport("user32.dll")> _
        Public Shared Function EnumDisplaySettings(ByVal deviceName As String, ByVal modeNum As Integer, ByRef devMode As DEVMODE1) As Integer
        End Function

        <dllimport("user32.dll")> _
        Public Shared Function ChangeDisplaySettings(ByRef devMode As DEVMODE1, ByVal flags As Integer) As Integer
        End Function

        Public Const ENUM_CURRENT_SETTINGS As Integer = -1
        Public Const CDS_UPDATEREGISTRY As Integer = 1
        Public Const CDS_TEST As Integer = 2
        Public Const DISP_CHANGE_SUCCESSFUL As Integer = 0
        Public Const DISP_CHANGE_RESTART As Integer = 1
        Public Const DISP_CHANGE_FAILED As Integer = -1

    End Class

    Class CResolution

        Public Sub New(ByVal a As Integer, ByVal b As Integer)
            Dim screen As Screen = screen.PrimaryScreen
            Dim iWidth As Integer = a
            Dim iHeight As Integer = b
            Dim dm As DEVMODE1 = New DEVMODE1
            dm.dmDeviceName = New String(New Char(32) {})
            dm.dmFormName = New String(New Char(32) {})
            dm.dmSize = CType(Marshal.SizeOf(dm), Short)
            If Not (0 = User_32.EnumDisplaySettings(Nothing, User_32.ENUM_CURRENT_SETTINGS, dm)) Then
                dm.dmPelsWidth = iWidth
                dm.dmPelsHeight = iHeight
                Dim iRet As Integer = User_32.ChangeDisplaySettings(dm, User_32.CDS_TEST)

                If iRet = User_32.DISP_CHANGE_FAILED Then
                    MessageBox.Show("Unable to process your request")
                    MessageBox.Show("Description: Unable To Process Your Request. Sorry For This Inconvenience.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    iRet = User_32.ChangeDisplaySettings(dm, User_32.CDS_UPDATEREGISTRY)
                    Select Case iRet
                        Case User_32.DISP_CHANGE_SUCCESSFUL
                            ' break 
                        Case User_32.DISP_CHANGE_RESTART
                            MessageBox.Show("Description: You Need To Reboot For The Change To Happen." & Microsoft.VisualBasic.Chr(10) & " If You Feel Any Problem After Rebooting Your Machine" & Microsoft.VisualBasic.Chr(10) & "Then Try To Change Resolution In Safe Mode.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            ' break 
                        Case Else
                            MessageBox.Show("Description: Failed To Change The Resolution.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            ' break 
                    End Select
                End If

            End If
        End Sub

    End Class

End Class
Posted
Updated 15-Nov-18 22:04pm

1 solution

Why have you reposted this question? I have already provided you with links to the answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900