locked
get list resolution from monitor

    Question

  • my code, i'm converting this code from vb to vb.net and seems i was missing something, seems EnumDisplaySetting can't read values and returning 0

     

     Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
     Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
     Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Integer) As Integer
    
     Public Const LVS_EX_FULLROWSELECT As Integer = &H20
     Public Const LVM_FIRST As Integer = &H1000
     Public Const LVM_SETEXTENDEDLISTVIEWSTYLE As Integer = (LVM_FIRST + 54)
    
     Public Const LOGPIXELSX As Integer = 88
     Public Const LOGPIXELSY As Integer = 90
     Public Const BITSPIXEL As Integer = 12
     Public Const HORZRES As Integer = 8
     Public Const VERTRES As Integer = 10
    
     Const CCDEVICENAME As Integer = 32
     Const CCFORMNAME As Integer = 32
    
     Public Const DM_BITSPERPEL As Integer = &H40000
     Public Const DM_PELSWIDTH As Integer = &H80000
     Public Const DM_PELSHEIGHT As Integer = &H100000
     Public Const DM_DISPLAYFLAGS As Integer = &H200000
    
     <StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
     <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> 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
     <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
     Public dmUnusedPadding As Short
     Public dmBitsPerPel As Short
     Public dmPelsWidth As Integer
     Public dmPelsHeight As Integer
     Public dmDisplayFlags As Integer
     Public dmDisplayFrequency As Integer
     End Structure
    
     Public Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
     Dim DM As DEVMODE
     Dim dMode As Integer
     Dim r As Integer
    
     'set the DEVMODE flags and structure size
     DM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
     DM.dmSize = Len(DM)
    
     'The first mode is 0
     dMode = 0
    
     Do While EnumDisplaySettings(Nothing, dMode, DM) > 0
    
      'if the BitsPerPixel is greater than 4
      '(16 colours), then add the item to the list
      If DM.dmBitsPerPel >= 4 Then Call LVAdd()
    
      'increment and call again. Continue until
      'EnumDisplaySettings returns 0 (no more settings)
      dMode = dMode + 1
    
     Loop
     End Sub
    
     'vars set in load
     Private Sub LVAdd()
     Dim DM As DEVMODE
     
     itmX.Items.Add(CStr(DM.dmPelsWidth & " X " & DM.dmPelsHeight))
     itmX.Items.Add(CStr(currVRes & " X " & currHRes))
    
     If CompareSettings() Then
      'itmX.SubItems(2) = "Current"
      itmX.SelectedIndex = True
     End If
    
     End Sub
    


     






    • Edited by Chaeril Tuesday, June 14, 2011 3:18 PM
    Tuesday, June 14, 2011 2:07 PM

Answers

  • 18 times,

    800 X 480   with 6 different frequency (60Hz, 70hz, 73Hz, 75Hz, 85Hz) and 3 different colorDepth (32 bits, 16 bits, 256 color)

    it makes 18 modes for 800 x 480

    Wednesday, June 15, 2011 9:44 AM
  • Hmm OK, i think i can fix it with a flag to not add same resolution

     

    Thanks

    Wednesday, June 15, 2011 11:19 AM

All replies

  • To add subitems to a ListViewItem, you need to use the Add method

    itmX.SubItems.Add(CStr(DM.dmPelsWidth) & CStr(DM.dmPelsHeight))
    
    If CompareSettings(DM) Then
      itmX.SubItems.Add("Current")
      itmX.Selected = True
    End If
    
    


    [Edit]

    Looks like you changed the code in your post while I was typing my reply.  Your latest code does not seem to use SubItems, so I', not sure what your question means.

     


    Tuesday, June 14, 2011 3:06 PM
  • thanks for quick reply

    i got nullreferenceexception when adding DM.dmPelsWidth and DM.dmPelsheight to subitems, if i change listboxview to combobox it run but only show 0X0, seems it can't read the value. i update code above with combo box

    Tuesday, June 14, 2011 3:16 PM
  • What values do you get using the VB6 program?

    Tuesday, June 14, 2011 3:43 PM
  • it should get list of resolution supported by monitor, here's link to VB6 version, enum display
    Tuesday, June 14, 2011 5:56 PM
  • it should get list of resolution supported by monitor

    Does it?  If the VB6 version doesn't work, neither will the upgrade.
    Tuesday, June 14, 2011 6:00 PM
  • Yes, i managed to get it running on vb net for now but it returned same value couple times, i think it need something like DISTINCT function :D

    It returned (in combo box list)

    1366 x 768 <- why this first? < change dMode to 0 and this fixed but

    800 x 480 18 times

    800 x 600 18 times (i didn't count this :))

    ...

     

    new code

     Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpdmode As DEVMODE) As Boolean
     'Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Integer) As Integer
    
     Const ENUM_CURRENT_SETTINGS As Integer = -1
     Const CDS_UPDATEREGISTRY As Integer = &H1
     Const CDS_TEST As Long = &H2
    
     Const CCDEVICENAME As Integer = 32
     Const CCFORMNAME As Integer = 32
    
     Const DISP_CHANGE_SUCCESSFUL As Integer = 0
     Const DISP_CHANGE_RESTART As Integer = 1
     Const DISP_CHANGE_FAILED As Integer = -1
    
     'Public Const dm_BITSPERPEL As Integer = &H40000
     'Public Const dm_PELSWIDTH As Integer = &H80000
     'Public Const dm_PELSHEIGHT As Integer = &H100000
     'Public Const dm_DISPLAYFLAGS As Integer = &H200000
    
     <StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
      <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> 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
      <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
      Public dmUnusedPadding As Short
      Public dmBitsPerPel As Short
      Public dmPelsWidth As Integer
      Public dmPelsHeight As Integer
    
      Public dmDisplayFlags As Integer
      Public dmDisplayFrequency As Integer
     End Structure
    
     Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
     Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
    
     Public Sub W3ResChanger_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
      Dim DevM As DEVMODE
    
      DevM.dmDeviceName = New [String](New Char(32) {})
      DevM.dmFormName = New [String](New Char(32) {})
      DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
    
      Dim dMode = -1
      'While 0 <> 
      Do While EnumDisplaySettings(Nothing, dMode, DevM) = True
       'Dim lResult As Integer
       If DevM.dmPelsWidth > 640 Then
        LVAdd(DevM)
       End If
       dMode += 1
       'End While
      Loop
     End Sub
    
     'vars set in load
     Private Sub LVAdd(DevM)
      Dim currres As String
    
      If DevM.dmPelsHeight = intX And DevM.dmPelsWidth = intY Then
       currres = "Monitor Resolution"
      Else
       currres = ""
      End If
    
      itmX.Items.Add(CStr(DevM.dmPelsWidth) & " X " & CStr(DevM.dmPelsHeight) & CStr(currres))
      If currres <> "" Then
       itmX.SelectedItem = True
      End If
     End Sub
    
    
    


     

     


    • Edited by Chaeril Wednesday, June 15, 2011 3:51 AM fixed 1366 first
    Wednesday, June 15, 2011 3:50 AM
  • 18 times,

    800 X 480   with 6 different frequency (60Hz, 70hz, 73Hz, 75Hz, 85Hz) and 3 different colorDepth (32 bits, 16 bits, 256 color)

    it makes 18 modes for 800 x 480

    Wednesday, June 15, 2011 9:44 AM
  • Hmm OK, i think i can fix it with a flag to not add same resolution

     

    Thanks

    Wednesday, June 15, 2011 11:19 AM