none
Changing screen resolution with VB 2005

    Question

  • I'm having a difficult time converting a Windows API call that works fine in VB6 to run in VB 2005 (Windows XP with SP2 in both cases).  I reduced the problem code down to its core which is below. I'm trying to change the screen resolution with the ChangeDisplaySettingsEx call; the idea is that on entering the program it shifts to high resolution and at the end it restores the original settings. I left the restore part out because I get the same error. Which is "An invalid parameter was passed in. This can include an invalid flag or combination of flags."  I'm trying to use exactly the same parameters, but somewhere in the conversion process from VB6 a fatal change crept in.  My guess is that it's either in the ByRef/ByVal or Short/Integer/Long alternatives, or in the conversion of the Devmode structure which included fixed-length strings (OK in VB6 but not allowed in VB.NET). But all my fiddling so far has not produced a solution.  I got the same error code back in VB6 when I tried to set a display frequency that was not supported at the resolution I requested, but even leaving the values unmodified (in effect setting them to what they are now, with no change) gives the same error. Any ideas? So far as I can tell, there is no alternative to the API to accomplish this goal; would be interested to know if I missed it.

    '  ==========================
    'Imports System, .Drawing, .Windows.Forms
    Public Class frmDX9RKS
        Inherits Form
     '==== screen-resolution API, constants, etc. ====

      Public Declare Function ChangeDisplaySettingsEx Lib "user32" _
        Alias "ChangeDisplaySettingsExA" _
        (ByRef lpszDeviceName As Integer, _
         ByRef lpDevMode As DEVMODE, _
         ByVal hWnd As Integer, _
         ByVal dwFlags As Integer, _
         ByRef lParam As Integer) As Integer

      Const CCDEVICENAME As Integer = 32
      Const CCFORMNAME As Integer = 32

      Enum CDSXRC As Integer 'change-display-settings return codes
        'From winuser.h
        DISP_CHANGE_SUCCESSFUL = 0
        DISP_CHANGE_RESTART = 1
        DISP_CHANGE_FAILED = -1
        DISP_CHANGE_BADMODE = -2
        DISP_CHANGE_NOTUPDATED = -3
        DISP_CHANGE_BADFLAGS = -4
        DISP_CHANGE_BADPARAM = -5
      End Enum

      Public Structure DEVMODE
        <VBFixedString(CCDEVICENAME), _
          System.Runtime.InteropServices.MarshalAs( _
          System.Runtime.InteropServices.UnmanagedType.ByValArray, _
          SizeConst:=(CCDEVICENAME))> Public dmDeviceName() As Char
        Dim dmSpecVersion As Short
        Dim dmDriverVersion As Short
        Dim dmSize As Short
        Dim dmDriverExtra As Short
        Dim dmFields As Integer
        Dim dmOrientation As Short
        Dim dmPaperSize As Short
        Dim dmPaperLength As Short
        Dim dmPaperWidth As Short
        Dim dmScale As Short
        Dim dmCopies As Short
        Dim dmDefaultSource As Short
        Dim dmPrintQuality As Short
        Dim dmColor As Short
        Dim dmDuplex As Short
        Dim dmYResolution As Short
        Dim dmTTOption As Short
        Dim dmCollate As Short
        <VBFixedString(CCFORMNAME), _
          System.Runtime.InteropServices.MarshalAs( _
          System.Runtime.InteropServices.UnmanagedType.ByValArray, _
          SizeConst:=(CCFORMNAME))> Public dmFormName() As Char
        Dim dmUnusedPadding As Short
        Dim dmBitsPerPel As Short
        Dim dmPelsWidth As Integer
        Dim dmPelsHeight As Integer
        Dim dmDisplayFlags As Integer
        Dim dmDisplayFrequency As Integer
        Dim dmICMMethod As Integer 'NT 4.0
        Dim dmICMIntent As Integer 'NT 4.0
        Dim dmMediaType As Integer 'NT 4.0
        Dim dmDitherType As Integer 'NT 4.0
        Dim dmReserved1 As Integer 'NT 4.0
        Dim dmReserved2 As Integer 'NT 4.0
        Dim dmPanningWidth As Integer 'Win2000
        Dim dmPanningHeight As Integer 'Win2000
      End Structure

      Public Declare Function EnumDisplaySettings Lib "user32" _
        Alias "EnumDisplaySettingsA" ( _
        ByVal lpszDeviceName As Integer, _
        ByVal iModeNum As Integer, _
        ByRef lpDevMode As DEVMODE) As Boolean

    Shared Sub Main()
      Dim frm As New frmDX9RKS()
    End Sub    'Main

    Public Sub New()
      Const DM_PELSWIDTH As Integer = &H80000
      Const DM_PELSHEIGHT As Integer = &H100000
      Const DM_DISPLAYFREQUENCY As Integer = &H400000
      Const CDS_TEST As Integer = &H4

      InitializeComponent()  'required by the Windows Form Designer.
      ' Add any initialization after the Initialize-Component() call.

        Const HiResW As Integer = 1600 'hi-res width in pixels
        Const HiResH As Integer = 1200 'hi-res height in pixels
        Const ENUM_CURRENT_SETTINGS As Integer = -1

        Static OldWd As Integer 'screen width in pixels
        Static OldHt As Integer 'screen height in pixels
        Static OldFreq As Integer 'screen-refresh frequency

        Dim DevM As New DEVMODE  'added New in V3.2, 3/22/06
        Dim boRetn As Boolean 'boolean return code
        Dim ResChgRtCd As Integer 'As CDSXRC 'resolution-change return code

    Debug.Print("Original - width: " & DevM.dmPelsWidth & _
    "  Height: " & DevM.dmPelsHeight & _
    "  Freq: " & DevM.dmDisplayFrequency & _
    "  DevmodeSize: " & DevM.dmSize & _
    "  dmFields: " & DevM.dmFields)

        DevM.dmSize = CShort(Len(DevM)) 'set up dev-mode
          'set field bits: Horiz, Vert, Freq
        DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_DISPLAYFREQUENCY

          'save orig values and set to high resolution
          boRetn = EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, DevM)
    Debug.Print("Color: " & DevM.dmBitsPerPel & " bits/pixel")

          OldWd = DevM.dmPelsWidth 'save orig width in pixels
          OldHt = DevM.dmPelsHeight 'save orig height in pixels
          OldFreq = DevM.dmDisplayFrequency 'save orig refresh freq
          DevM.dmPelsWidth = HiResW
          DevM.dmPelsHeight = HiResH
          DevM.dmDisplayFrequency = 60 'use lower (hard-coded) freq for LCD hi-res
    Debug.Print("Before change - width: " & DevM.dmPelsWidth & _
    "  Height: " & DevM.dmPelsHeight & _
    "  Freq: " & DevM.dmDisplayFrequency & _
    "  DevmodeSize: " & DevM.dmSize & _
    "  dmFields: " & DevM.dmFields)
    'Stop
          ResChgRtCd = ChangeDisplaySettingsEx(0, DevM, 0, CDS_TEST, 0)

          If ResChgRtCd <> CDSXRC.DISP_CHANGE_SUCCESSFUL Then 'if change failed,
            subBadResChgDiag((ResChgRtCd)) ' tell why
            Stop
            Exit Sub
          End If

    End Sub

    Private Sub subBadResChgDiag(ByRef CDSRetCode As Integer)
      Dim strErr As String
      Dim strBase As String

      strBase = "Screen-resolution change failed;" & vbCrLf & vbCrLf & "Code is: "

      Select Case CDSRetCode
        Case CDSXRC.DISP_CHANGE_RESTART
          strErr = strBase & "CHANGE_RESTART"
        Case CDSXRC.DISP_CHANGE_FAILED
          strErr = strBase & "CHANGE_FAILED"
        Case CDSXRC.DISP_CHANGE_BADMODE
          strErr = strBase & "CHANGE_BADMODE"
        Case CDSXRC.DISP_CHANGE_NOTUPDATED
          strErr = strBase & "CHANGE_NOTUPDATED"
        Case CDSXRC.DISP_CHANGE_BADFLAGS
          strErr = strBase & "CHANGE_BADFLAGS"
        Case CDSXRC.DISP_CHANGE_BADPARAM
          strErr = strBase & "CHANGE_BADPARAM"
        Case Else
          strErr = strBase & "Unknown resolution-change error"
      End Select
      MsgBox(strErr)

    End Sub

    End Class

    Saturday, March 25, 2006 6:09 PM

Answers

  • Thanks for the suggestion.  As it happened, I was just rummaging around some more and found the ChangeDisplaySettings call; it only works on the default display adapter unlike ChangeDisplaySettingsEx, but that's all I need for this app.  So far as I can tell, the problem was somewhere in the three null arguments I was trying to set (based on help files for C++ usage, which was all I could find for the API), and in converting from the "As Any" which is no longer allowed in a Declare statement.
    Sunday, March 26, 2006 6:29 PM

All replies

  • Just forget trying to use the API to change the display mode. Instead:

    1. Add a COM reference to 'DirectX 7 for Visual Basic Type library'

    2. At the top of your code for your main form, before the class declaration, add 'Imports DxVbLib'

    3. After the class declaration, add:

         Dim dx as New DirectX7

         Dim dd as DirectDraw7

    4. Add the following code to your Form_Load event handler:

        dd=dx.DirectDrawCreate("")

        dd.setDisplayMode(800,600,16,0,CONST_DDSDMFLAGS.DDSDM_DEFAULT)

    Note: Change '800' to the desired width of the display mode, '600' should be the height, and '16' should be the color depth.

    Example

     

    Imports DxVBLib

    Public Class frmMain

      Dim dx As New DirectX7

      Dim dd As DirectDraw7

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Show()

        dd = dx.DirectDrawCreate("")

        dim Height as Integer

        dim Width as Integer

        dim Depth as Integer

        Height=600 ' Change this to the correct number

        Width=800 ' Change this to the correct number

        Depth=16 ' Change this to the correct number

        dd.SetDisplayMode(Height, Width, Depth, 0, CONST_DDSDMFLAGS.DDSDM_DEFAULT)

      End Sub

    End Class

    Sunday, March 26, 2006 11:17 AM
  • Thanks for the suggestion.  As it happened, I was just rummaging around some more and found the ChangeDisplaySettings call; it only works on the default display adapter unlike ChangeDisplaySettingsEx, but that's all I need for this app.  So far as I can tell, the problem was somewhere in the three null arguments I was trying to set (based on help files for C++ usage, which was all I could find for the API), and in converting from the "As Any" which is no longer allowed in a Declare statement.
    Sunday, March 26, 2006 6:29 PM
  • Hi Robinjam,

                          I have tried this code with my system , But, Its shows the exception like

    NotImplementedException was unhandled by user code on CONST_DDSDMFLAGS.DDSDM_DEFAULT

    And, Please tell me, what sholud i do for this error.

    Sujatha.

     

     

     

    Friday, March 31, 2006 5:23 AM
  • Sorry, I don't know what went wrong. Try just replacing CONST_DDSDMFLAGS.DDSDM_DEFAULT with 0.
    Friday, April 14, 2006 11:21 AM
  • Hey,

    Actually, I have did a mistake, Now its works fine.

    Thanks for the help and reply.

    Nice to meet you.

    Regards,

    Sujatha.

     

     

     

    Monday, April 17, 2006 4:35 AM
  • You complete noob

    Actually upon reflection that was totally uncalled for. You aren't a noob at all.

    Tuesday, April 18, 2006 11:08 AM
  • Although the DirectX method involves simpler coding, there is a caveat to using this method: You are only able to change to display modes which DirectX recognizes and not necessarily to any of the modes which your display adapter might support.

    For example, on my system I am only able to change BitDepth to 16 or 32 bits. Nothing lower. If I want to change to 256-color mode I cannot do it with DirectX.

    (refer to http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_m_Summer_04/directx/direct3d/gettingstarted/direct3dsurfaces/surfaceformats.asp for details about supported formats).

    I'm really not too familiar with the DirectDraw API, so if someone knows a way to enumerate and change to all the adapters supported modes, please post.

    FYI, I'm working on wrapping the API functions into a VB.Net (2005) class which can be dropped into an app or used via DLL. I'll post back when it is finished for anyone who might be interested.

    Cheers,

    Dean

    Sunday, April 30, 2006 5:45 AM
  • Thanks for this excellent code

     

    It worked!!!

     

    // PCM2 and bevin

    Sunday, May 28, 2006 2:02 PM
  • i found a problem with the return resolution, i changed from 1024 x 768 to 800 x 600.

    when the program exited, it put the resolution back to 1024 x 768 however the working desktop was actually fixed in the res of 800 x 600. people will only find this error if they shrink to a lower res.

    if they put this in an exit code it will fix the problem and the user will have full control of the desktop:

    dd.RestoreDisplayMode()

     

     

     

    Thursday, June 01, 2006 10:30 PM
  • here's a working api way:

    Code Block
    Imports System.Runtime.InteropServices

    ' see msdn on "Changing Screen Orientation Programmatically" for tablet pc.
    ' note: I couldn't change orientation on xp, or find anyone claiming they could!
    ' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dntablet/html/tbconChgScrn.asp


    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Friend Structure DEVMODE

        <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 dmPositionX As Integer
        Public dmPositionY As Integer
        Public dmDisplayOrientation As Integer
        Public dmDisplayFixedOutput As Integer
        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

    Friend
    Class NativeMethods

        
    ' PInvoke declaration for EnumDisplaySettings Win32 API
        <DllImport("user32.dll", CharSet:=CharSet.Ansi)> _
        
    Public Shared Function EnumDisplaySettings( _
        
    ByVal lpszDeviceName As String, _
        
    ByVal iModeNum As Integer, _
        
    ByRef lpDevMode As DEVMODE) As Integer
        End Function

        ' PInvoke declaration for ChangeDisplaySettings Win32 API
        <DllImport("user32.dll", CharSet:=CharSet.Ansi)> _
        
    Public Shared Function ChangeDisplaySettings( _
        
    ByRef lpDevMode As DEVMODE, _
        
    ByVal dwFlags As Integer) As Integer
        End Function

    End
    Class

    Public
    Class ResolutionChanger

        
    Private Shared Function CreateDevMode() As DEVMODE
            
    Dim dm As New DEVMODE
            dm.dmDeviceName =
    New String(New Char(32) {})
            dm.dmFormName =
    New String(New Char(32) {})
            dm.dmSize =
    CShort(Marshal.SizeOf(dm))
            
    Return dm
        
    End Function

        Public Enum DisplayChangeResultCode
            DISP_CHANGE_SUCCESSFUL = 0
            DISP_CHANGE_RESTART = 1
            DISP_CHANGE_FAILED = -1
            DISP_CHANGE_BADMODE = -2
            DISP_CHANGE_NOTUPDATED = -3
            DISP_CHANGE_BADFLAGS = -4
            DISP_CHANGE_BADPARAM = -5
            DISP_CHANGE_BADDUALVIEW = -6
        
    End Enum

        Public Shared Sub ChangeResolution(ByVal width As Integer, ByVal height As Integer, ByVal freq As Integer)

            
    Const DM_PELSWIDTH As Integer = &H80000
            
    Const DM_PELSHEIGHT As Integer = &H100000
            
    Const DM_DISPLAYFREQUENCY As Integer = &H400000
            
    Const ENUM_CURRENT_SETTINGS As Integer = -1
            
    Dim DevM As DEVMODE = CreateDevMode()
            
    Dim enumResult As Integer
            Dim changeResult As DisplayChangeResultCode

            DevM.dmFields = DM_PELSWIDTH
    Or DM_PELSHEIGHT Or DM_DISPLAYFREQUENCY

            enumResult = NativeMethods.EnumDisplaySettings(
    Nothing, ENUM_CURRENT_SETTINGS, DevM)

            DevM.dmPelsWidth = width
            DevM.dmPelsHeight = height
            DevM.dmDisplayFrequency = freq

            changeResult =
    CType(NativeMethods.ChangeDisplaySettings(DevM, 0), DisplayChangeResultCode)

            
    If changeResult <> DisplayChangeResultCode.DISP_CHANGE_SUCCESSFUL Then
                Throw New Exception("Failed to change resolution: " & changeResult.ToString)
            
    End If

        End Sub

    End
    Class

     

    see also :

    http://cid-862dee3ec267cb5c.skydrive.live.com/self.aspx/Public/DisplaySettings.zip

    Friday, June 02, 2006 12:51 AM
  • Hi,

    It seems like the initial poster figured out how to change the user's display using the API. I tried the DirectX and even though the code is well documented and explained, it doesn't work on my XP platform.

    I was wondering if anyone has been able to get this to function with Visual Basic Express 2005. I have been tasked to accomplish this in VB Express and I just cannot find a work-able solution.  Any assistance would be helpful.

    Thanks!

    Christine

     

    Saturday, June 03, 2006 4:15 PM
  • I'm not entirely sure what the problem is. The code I wrote using DirectX was written in VB Express 2005 (I can't afford the full version ). It should work with any video card supported by DirectX. Could you explain in more detail what the problem is?
    Wednesday, June 21, 2006 3:47 PM
  • And sorry it took me so long to reply...
    Wednesday, June 21, 2006 8:45 PM
  • It's no problem at all. I wanted to do something different anyway. :)
    Thursday, June 22, 2006 8:01 AM
  •  

    You can also use the powerfull builtin computer.screen:

     

    Dim width, height As Double

    Dim widescreen as integer = 0

    width = 1.0

    height = 1.0

    If My.Computer.Screen.Bounds.Width >= 1280 Then

    widescreen = 1

    width = 1.25!

    End If

    If My.Computer.Screen.Bounds.Height >= 1024 Then

    widescreen = 1

    height = 1.25!

    End If

    If widescreen = 1 Then

    Me.Scale(New System.Drawing.SizeF(width, height))

    End If

    (You can check the height and weight of the current resolution and then change it to the correct resolution)

     

    Friday, October 06, 2006 8:53 AM
  •  

     hi jo0ls,

    I am interested in your code. But not sure how to use it any suggestions on how to get to the mode changing commands. I'll continue to test it (no errors) on execution but nothing seems to be happening.....

    Friday, November 24, 2006 2:24 AM
  • jbassmanp,

    If you were referring to the DirectDraw method of changing the display resolution, please make sure that:

    1. You have at least version 7 of DirectX
    2. You have added a reference to DirectX in your project
    3. Your video card supports the resolution you provided

    Otherwise your app will not work, and it may not even return an error at all.

    Friday, November 24, 2006 1:02 PM
  •  jo0ls wrote:
    here's a working api way:

    Imports System.Runtime.InteropServices
     
    ' see msdn on "Changing Screen Orientation Programmatically" for tablet pc.
    ' note: I couldn't change orientation on xp, or find anyone claiming they could!
    ' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dntablet/html/tbconChgScrn.asp
     
     
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Friend Structure DEVMODE
     
        <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 dmPositionX As Integer
        Public dmPositionY As Integer
        Public dmDisplayOrientation As Integer
        Public dmDisplayFixedOutput As Integer
        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
     
    Friend Class NativeMethods
     
        ' PInvoke declaration for EnumDisplaySettings Win32 API
        <DllImport("user32.dll", CharSet:=CharSet.Ansi)> _
        Public Shared Function EnumDisplaySettings( _
        ByVal lpszDeviceName As String, _
        ByVal iModeNum As Integer, _
        ByRef lpDevMode As DEVMODE) As Integer
        End Function
     
        ' PInvoke declaration for ChangeDisplaySettings Win32 API
        <DllImport("user32.dll", CharSet:=CharSet.Ansi)> _
        Public Shared Function ChangeDisplaySettings( _
        ByRef lpDevMode As DEVMODE, _
        ByVal dwFlags As Integer) As Integer
        End Function
     
    End Class
     
    Public Class ResolutionChanger
     
        Private Shared Function CreateDevMode() As DEVMODE
            Dim dm As New DEVMODE
            dm.dmDeviceName = New String(New Char(32) {})
            dm.dmFormName = New String(New Char(32) {})
            dm.dmSize = CShort(Marshal.SizeOf(dm))
            Return dm
        End Function
     
        Public Enum DisplayChangeResultCode
            DISP_CHANGE_SUCCESSFUL = 0
            DISP_CHANGE_RESTART = 1
            DISP_CHANGE_FAILED = -1
            DISP_CHANGE_BADMODE = -2
            DISP_CHANGE_NOTUPDATED = -3
            DISP_CHANGE_BADFLAGS = -4
            DISP_CHANGE_BADPARAM = -5
            DISP_CHANGE_BADDUALVIEW = -6
        End Enum
     
        Public Shared Sub ChangeResolution(ByVal width As Integer, ByVal height As Integer, ByVal freq As Integer)
     
            Const DM_PELSWIDTH As Integer = &H80000
            Const DM_PELSHEIGHT As Integer = &H100000
            Const DM_DISPLAYFREQUENCY As Integer = &H400000
            Const ENUM_CURRENT_SETTINGS As Integer = -1
            Dim DevM As DEVMODE = CreateDevMode()
            Dim enumResult As Integer
            Dim changeResult As DisplayChangeResultCode
     
            DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_DISPLAYFREQUENCY
     
            enumResult = NativeMethods.EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM)
     
            DevM.dmPelsWidth = width
            DevM.dmPelsHeight = height
            DevM.dmDisplayFrequency = freq
     
            changeResult = CType(NativeMethods.ChangeDisplaySettings(DevM, 0), DisplayChangeResultCode)
     
            If changeResult <> DisplayChangeResultCode.DISP_CHANGE_SUCCESSFUL Then
                Throw New Exception("Failed to change resolution: " & changeResult.ToString)
            End If
     
        End Sub
     
    End Class
    How to you use this. I've been testing this what controls or settings textboxes etc do you use here. What do you veiw and how do you do it one example is all I need.
    Sunday, November 26, 2006 8:54 PM
  • Bonjour,

    Dommage que c'est en anglais !!

    Merci

    Thursday, February 15, 2007 1:00 PM
  • Oui!

    Mais je parle un peut de Francais. Si vous voulez, je peux traduire.

    Thursday, February 15, 2007 4:35 PM
  • Hi, I have read with interest your question and answer.   This is something i've been looking for for ages.   Can someone tell me if this will work in the vba environment.    I construct a lot of Access databases and every one uses their own screen res.   It would be great if I could set the res to 1024 x 768 and then reset back to the users res on exit.    Thanks in anticipation.
    Tuesday, May 08, 2007 11:34 AM
  •  

    i found a problem with the return resolution, i changed from 1024 x 768 to 800 x 600.

    when the program exited, it put the resolution back to 1024 x 768 however the working desktop was actually fixed in the res of 800 x 600. people will only find this error if they shrink to a lower res.

     

    If you put this in an exit code it will fix the problem and the resolution returned correctly:

     

    dd.RestoreDisplayMode()

     

    Depending on which way you change res

    Tuesday, May 08, 2007 12:19 PM
  • i am incredulated that nobody has had any problems with their compiler processing lines such as:

     

    'Const DM_PELSWIDTH As Integer = &H80000'

     

    since when were integers able to exceed 4 hex digits?

    Friday, July 27, 2007 1:54 AM
  • Since VB7

     

    VB.Net Long 64 bit digit.

    VB6 Long 32 bit digit.

     

    VB.Net: Integer 32bit digit.

    VB6: Integer 16 bit dgit.

     

    VB.Net: Short 16 bit digit.

    ?

     

    Console.WriteLine(Integer.MaxValue.ToString("X"))
    Console.WriteLine(
    UInteger.MaxValue.ToString("X"))
     
    Output:

    7FFFFFFF

    FFFFFFFF

     

    Most of these constants are DWORDS though, so to be fussy you should declare them as UInteger and add UI to the end. Then you can declare the ones that are larger than &H7FFFFFFF. But UInteger didn't exist in earlier versions of the framework.

     

    Const DM_PELSWIDTH As UInteger = &H80000UI

     

     

    Friday, August 03, 2007 5:38 PM
  • I find it best to use the int plus the bit representation, e.g. Int32.  Integers have always represented the native size on a platform.  On 64 bit systems integers are now 64 bits.

    Friday, August 03, 2007 6:17 PM
  • Integer is still 32 bits, IntPtr changed (obviously, otherwise they couldn't point to all that lovely new memory).

     

    imports system.runtime.interopservices

    imports system.evironment

     

    Console.WriteLine(GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"))
    Console.WriteLine(Marshal.SizeOf(
    GetType(Integer)))
    Console.WriteLine(Marshal.SizeOf(
    GetType(IntPtr)))
    Console.WriteLine(Marshal.SizeOf(
    GetType(Int32)))
    Console.WriteLine(Marshal.SizeOf(
    GetType(Int64)))
    Console.WriteLine(Marshal.SizeOf(
    GetType(Boolean)))
     
    Vista 64:
     
    AMD64
    4
    8
    4
    8
    4
     
    XP 32 on virtual PC:
     
    x86
    4
    4
    4
    8
    4
    Saturday, August 04, 2007 1:06 AM
  • Can anyone please tell me if I need to install the DirectX SDK in order for the solution above to work?

    I can not add a COM reference to DirectX 7, there is no listing for a DirectX COM in the list... I am running VS 2005.

     

    Any help would be a fantastic! Smile

    Thanks

    Sunday, January 20, 2008 3:44 PM
  • The one I posted just uses api calls not directX.

     

    To use it, paste it into a new class file in your project. Then you can call the method...

     

     

    Code Block
    Public Class Form1

        
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Click
            ResolutionChanger.ChangeResolution(800, 600, 60)
        
    End Sub

    End
    Class

     

     

     
     
    You can enumerate the available resolutions too.
     
     
    Sunday, January 20, 2008 8:41 PM

  • ResolutionChanger.ChangeResolution(800, 600, 60) 
    That changed the Desktop res,
    but how do we return it to default state on form unload or program exit?

    EDIT: Well nevermind, I figured it out
    Thanks, Dato

    Sunday, January 20, 2008 9:18 PM
  • Dude you are amazing, what a fantastic API script.

    Thank you soooo much for replying and for taking the time to write a script like this.

     

    It works fine and sets my resolution to the desired settings, but I have two questions if I may...

     

    1) As asked above, how do we find the curent setting and return to this resolution after the application has closed?

    2) Do you know of any .NET methods for getting the same functionality?

     

    Thank you once again

    Kind regards

     

    Sunday, January 20, 2008 10:40 PM


  • Well here is how I did it

    Code Block




    'Set the Screen Res Variables

        Public HorSize As Integer
        Public VertSize As Integer

    'Call these up before Changing resolution
    'This Gets the Computers current Resolution
            HorSize = My.Computer.Screen.Bounds.Width
            VertSize = My.Computer.Screen.Bounds.Height      
           
    'Then to reset to default you can use it like this

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
            ResolutionChanger.ChangeResolution(HorSize, VertSize, 60)
    End Sub

    Sunday, January 20, 2008 11:07 PM
  • Awsome, thanks!

     

    Now all we need is a .NET version of this code..... Smileplz someone

     

    Thank you all

     

    Monday, January 21, 2008 12:38 AM
  • Er it is .Net. I think I know what you meant though. There isn't a way built into the framework to do it. It's bad practice to go around changing user settings without good reason, and the display settings are definately a user setting.

    Here's a slightly extended version, I'm trying out my Windows Live Skydrive thing for the first time...

    http://cid-862dee3ec267cb5c.skydrive.live.com/self.aspx/Public/DisplaySettings.zip


    I noticed there is ChangeDisplaySettingsEx, which offers more options. I'll play with that some other time.

    I've not tested changing any screen but the primary, it might not work at the moment.
    Monday, January 21, 2008 4:22 AM
  •  robinjam wrote:

    Just forget trying to use the API to change the display mode. Instead:

    1. Add a COM reference to 'DirectX 7 for Visual Basic Type library'

    ...


    How do i "add a COM" ???

    It gives me these errors:

    Warning    1    Namespace or type specified in the Imports 'DxVBLib' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.    C:\Users\Diogo\Documents\Visual Studio 2005\Projects\VB\DefinicoesDesistema\DefinicoesDesistema\Form2.vb    1    9    DefinicoesDesistema

    Error    2    Type 'DirectX7' is not defined.    C:\Users\Diogo\Documents\Visual Studio 2005\Projects\VB\DefinicoesDesistema\DefinicoesDesistema\Form2.vb    5    19    DefinicoesDesistema

    Error    3    Type 'DirectDraw7' is not defined.    C:\Users\Diogo\Documents\Visual Studio 2005\Projects\VB\DefinicoesDesistema\DefinicoesDesistema\Form2.vb    7    15    DefinicoesDesistema

    Error    4    Name 'CONST_DDSDMFLAGS' is not declared.    C:\Users\Diogo\Documents\Visual Studio 2005\Projects\VB\DefinicoesDesistema\DefinicoesDesistema\Form2.vb    27    52    DefinicoesDesistema


    Thanks
    Friday, May 02, 2008 7:04 PM
  • Now i know how to add a com reference, but in the COM list, there isn't any 'DirectX 7 for Visual Basic Type library'

    Anyone helps?
    Friday, May 02, 2008 7:40 PM
  • Install the DirectX SDK or search for dx7vb.dll, but check this thread:   http://forums.microsoft.com/msdn/showpost.aspx?postid=1223939&siteid=1&sb=0&d=1&at=7&ft=11&tf=0&pageid=3

     

    Friday, May 02, 2008 8:07 PM
  • I installed Microsoft directx sdk (march 2008)

    The code for this version should have something different, because it still gives me errors... It works with directx 9, isn't it? Can anyone please change the code to this version?

    Thanks
    Friday, May 02, 2008 11:55 PM
  • 'Put the following code in a module, and you will get a function SetResolution ready to work!

    Imports System.Runtime.InteropServices

    Private Declare Auto Function EnumDisplaySettings Lib "user32.dll" ( _
        <MarshalAs(UnmanagedType.LPTStr)> ByVal lpszDeviceName As String, _
        ByVal iModeNum As Int32, _
        ByRef lpDevMode As DEVMODE _
    ) As Boolean

    Private Declare Auto Function ChangeDisplaySettings Lib "user32.dll" ( _
        ByRef lpDevMode As DEVMODE, _
        ByVal dwFlags As Int32 _
    ) As Int32

    Private Const DM_BITSPERPEL As Int32 = &H40000
    Private Const DM_PELSWIDTH As Int32 = &H80000
    Private Const DM_PELSHEIGHT As Int32 = &H100000

    Private Const DISP_CHANGE_SUCCESSFUL As Int32 = 0

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure POINTL
        Public x As Int32
        Public y As Int32
    End Structure

    <StructLayout(LayoutKind.Explicit)> _
    Private Structure DEVMODE_union1
        ' struct {
            <FieldOffset(0)> Public dmOrientation As Int16
            <FieldOffset(2)> Public dmPaperSize As Int16
            <FieldOffset(4)> Public dmPaperLength As Int16
            <FieldOffset(6)> Public dmPaperWidth As Int16
        ' }
        <FieldOffset(0)> Public dmPosition As POINTL
    End Structure

    <StructLayout(LayoutKind.Explicit)> _
    Private Structure DEVMODE_union2
        <FieldOffset(0)> Public dmDisplayFlags As Int32
        <FieldOffset(0)> Public dmNup As Int32
    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Private Structure DEVMODE
        Private Const CCDEVICENAME As Int32 = 32
        Private Const CCFORMNAME As Int32 = 32

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> _
        Public dmDeviceName As String
        Public dmSpecVersion As Int16
        Public dmDriverVersion As Int16
        Public dmSize As Int16
        Public dmDriverExtra As Int16
        Public dmFields As Int32
        Public u1 As DEVMODE_union1
        Public dmScale As Int16
        Public dmCopies As Int16
        Public dmDefaultSource As Int16
        Public dmPrintQuality As Int16
        Public dmColor As Int16
        Public dmDuplex As Int16
        Public dmYResolution As Int16
        Public dmTTOption As Int16
        Public dmCollate As Int16
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> _
        Public dmFormName As String
        Public dmUnusedPadding As Int16
        Public dmBitsPerPel As Int16
        Public dmPelsWidth As Int32
        Public dmPelsHeight As Int32
        Public u2 As DEVMODE_union2
        Public dmDisplayFrequency As Int32
        Public dmICMMethod As Int32
        Public dmICMIntent As Int32
        Public dmMediaType As Int32
        Public dmDitherType As Int32
        Public dmReserved1 As Int32
        Public dmReserved2 As Int32
        Public dmPanningWidth As Int32
        Public dmPanningHeight As Int32
    End Structure

    Public Function SetResolution( _
        ByVal Width As Int32, _
        ByVal Height As Int32, _
        ByVal BitsPerPixel As Int16 _
    ) As Boolean
        Dim dm As DEVMODE
        If Not EnumDisplaySettings(Nothing, 0, dm) Then
            Return False
        Else
            With dm
                .dmFields = _
                    DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
                .dmPelsWidth = Width
                .dmPelsHeight = Height
                .dmBitsPerPel = BitsPerPixel
            End With
            Return (ChangeDisplaySettings(dm, 0) = DISP_CHANGE_SUCCESSFUL)
        End If
    End Function

    • Proposed as answer by BGQQ Thursday, May 24, 2012 7:37 PM
    Friday, August 12, 2011 10:21 PM