Results 1 to 9 of 9

Thread: Question about Resolution

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Canada
    Posts
    5

    Question about Resolution

    Um.... I'm kind of new here to Visual Basic, and I was wondering, how do you change the resolution in Visual Basic?

    Say, if i wanted to change the resolution to 640*480, how would I do that? Do I need some kind of DirectX function?

    Please help, thanks

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Canada
    Posts
    5

    Post an example?

    Can you post an example, please? Sorry, I'm quite new to VB, so I'm not really sure how that code works.

    So If you have the time, could you post a small example showing how to change the resolution, please?

  4. #4
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    You're going to have to put a command button on form.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const CCDEVICENAME = 32
    4. Private Const CCFORMNAME = 32
    5.  
    6. Private Const DISP_CHANGE_SUCCESSFUL = 0
    7. Private Const DISP_CHANGE_RESTART = 1
    8. Private Const DISP_CHANGE_FAILED = -1
    9. Private Const DISP_CHANGE_BADMODE = -2
    10. Private Const DISP_CHANGE_NOTUPDATED = -3
    11. Private Const DISP_CHANGE_BADFLAGS = -4
    12. Private Const DISP_CHANGE_BADPARAM = -5
    13.  
    14. Private Const CDS_UPDATEREGISTRY = &H1
    15. Private Const CDS_TEST = &H2
    16.  
    17. Private Const DM_BITSPERPEL = &H40000
    18. Private Const DM_PELSWIDTH = &H80000
    19. Private Const DM_PELSHEIGHT = &H100000
    20.  
    21. Private Type DEVMODE
    22. dmDeviceName As String * CCDEVICENAME
    23. dmSpecVersion As Integer
    24. dmDriverVersion As Integer
    25. dmSize As Integer
    26. dmDriverExtra As Integer
    27. dmFields As Long
    28. dmOrientation As Integer
    29. dmPaperSize As Integer
    30. dmPaperLength As Integer
    31. dmPaperWidth As Integer
    32. dmScale As Integer
    33. dmCopies As Integer
    34. dmDefaultSource As Integer
    35. dmPrintQuality As Integer
    36. dmColor As Integer
    37. dmDuplex As Integer
    38. dmYResolution As Integer
    39. dmTTOption As Integer
    40. dmCollate As Integer
    41. dmFormName As String * CCFORMNAME
    42. dmUnusedPadding As Integer
    43. dmBitsPerPel As Integer
    44. dmPelsWidth As Long
    45. dmPelsHeight As Long
    46. dmDisplayFlags As Long
    47. dmDisplayFrequency As Long
    48. End Type
    49.  
    50. Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
    51. Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwflags As Long) As Long
    52.  
    53. Function ChangeScreenSettings(lWidth As Integer, lHeight As Integer, lColors As Integer)
    54. Dim tDevMode As DEVMODE, lTemp As Long, lIndex As Long
    55. ' Declare variables
    56. lIndex = 0
    57. Do
    58. 'Instilise Do loop
    59. lTemp = EnumDisplaySettings(0&, lIndex, tDevMode)
    60. ' Call the APi function
    61. If lTemp = 0 Then Exit Do
    62. ' If there is no more data or an erro occurs
    63. ' then return 0 and exit do
    64. lIndex = lIndex + 1
    65. ' Increase the index to be enumerated
    66.  
    67. With tDevMode
    68. If .dmPelsWidth = lWidth And .dmPelsHeight = lHeight And .dmBitsPerPel = lColors Then
    69. lTemp = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
    70. Exit Do
    71. End If
    72. End With
    73. ' Set the new data
    74. ' Change the display type. This depends on the paramter used
    75. ' It can either be:
    76. ' 0 - Dynamic change if possible
    77. ' CDS_UPDATEREGISTRY - Dynamically change if possible
    78. ' and if not possibel then update registry for change
    79. ' on the next boot-up
    80. ' CDS_TEST - Test the new settings
    81. Loop
    82.  
    83. End Function
    84.  
    85. Private Sub Command1_Click()
    86.     ChangeScreenSettings 800, 600, 16
    87.    
    88. End Sub


    "X-mas is 24.Desember you English morons.." - NoteMe

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Here is the code, made clearer and commented "better":

    VB Code:
    1. 'add into a module
    2. Option Explicit
    3.  
    4. Private Const CCDEVICENAME = 32
    5. Private Const CCFORMNAME = 32
    6.  
    7. Private Const DISP_CHANGE_SUCCESSFUL = 0
    8. Private Const DISP_CHANGE_RESTART = 1
    9. Private Const DISP_CHANGE_FAILED = -1
    10. Private Const DISP_CHANGE_BADMODE = -2
    11. Private Const DISP_CHANGE_NOTUPDATED = -3
    12. Private Const DISP_CHANGE_BADFLAGS = -4
    13. Private Const DISP_CHANGE_BADPARAM = -5
    14.  
    15. Private Const CDS_UPDATEREGISTRY = &H1
    16. Private Const CDS_TEST = &H2
    17.  
    18. Private Const DM_BITSPERPEL = &H40000
    19. Private Const DM_PELSWIDTH = &H80000
    20. Private Const DM_PELSHEIGHT = &H100000
    21.  
    22. Private Type DEVMODE
    23.     dmDeviceName As String * CCDEVICENAME
    24.     dmSpecVersion As Integer
    25.     dmDriverVersion As Integer
    26.     dmSize As Integer
    27.     dmDriverExtra As Integer
    28.     dmFields As Long
    29.     dmOrientation As Integer
    30.     dmPaperSize As Integer
    31.     dmPaperLength As Integer
    32.     dmPaperWidth As Integer
    33.     dmScale As Integer
    34.     dmCopies As Integer
    35.     dmDefaultSource As Integer
    36.     dmPrintQuality As Integer
    37.     dmColor As Integer
    38.     dmDuplex As Integer
    39.     dmYResolution As Integer
    40.     dmTTOption As Integer
    41.     dmCollate As Integer
    42.     dmFormName As String * CCFORMNAME
    43.     dmUnusedPadding As Integer
    44.     dmBitsPerPel As Integer
    45.     dmPelsWidth As Long
    46.     dmPelsHeight As Long
    47.     dmDisplayFlags As Long
    48.     dmDisplayFrequency As Long
    49. End Type
    50.  
    51. Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
    52. Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwflags As Long) As Long
    53.  
    54. 'this function returns true if it was able to change the resolution
    55. 'usage: ChangeScreenSettings 800, 600, 32
    56. 'will set resolution to 800 x 600 at 32-bit colors
    57. Public Function ChangeScreenSettings(lWidth As Integer, lHeight As Integer, lColors As Integer) As Boolean
    58.     Dim tDevMode As DEVMODE, lTemp As Long, lIndex As Long
    59.     'start from the first setting
    60.     lIndex = 0
    61.     'loop until a matching resolution setting is found
    62.     Do
    63.         'call API: get settings to tDevMode
    64.         lTemp = EnumDisplaySettings(0&, lIndex, tDevMode)
    65.         'no more data, exit loop
    66.         If lTemp = 0 Then Exit Do
    67.         'increase index
    68.         lIndex = lIndex + 1
    69.         With tDevMode
    70.             'check if width, height and amount of colors match
    71.             If .dmPelsWidth = lWidth And .dmPelsHeight = lHeight And .dmBitsPerPel = lColors Then
    72.                 'change the resolution to the demanded one as we found a matching setting
    73.                 lTemp = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
    74.                 'we were able to set the demanded resolution!
    75.                 ChangeScreenSettings = True
    76.                 Exit Do
    77.             End If
    78.         End With
    79.     Loop
    80. End Function


    Then you can make a command button to a form and add following code:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If Not ChangeScreenSettings(800, 600, 32) Then
    3.         MsgBox "Failed to change resolution"
    4.     End If
    5. End Sub


    Hope this helps

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Canada
    Posts
    5

    OMG

    Wow... You are my life saver! Thank you sooooooo much!

    That's so cool!

    Thanks.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Canada
    Posts
    5

    umm...

    One, more question...

    Sorry to bother you, but how do you change it back to the default resolution that the person had before?

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Hmm... in the loop, look for the current resolution setting also. So first you loop looking for current resolution and the resolution to change to, and then loop is done (both settings found) then change the resolution and return the old resolution index number as the function return value.

    Complex?

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    Canada
    Posts
    5

    Sorry....

    I'm a very "Weak" programmer in Visual Basic, so can you give me an example of how to do that, that would really help me out.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width