Results 1 to 5 of 5

Thread: Screen Resolution Change Example Complete!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    2

    Screen Resolution Change Example Complete!

    Hi,

    Not sure how useful this is to anyone but I had alot of trouble finding a VB.NET Screen Resolution Change functionality, so I managed to get it working, and the code is below.

    Can be called by: ChangeRes(1024,768)

    Any comments are welcome, as are bug fixes!!!!

    VB Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Module resChanger
    4.  
    5.     Const ENUM_CURRENT_SETTINGS As Integer = -1
    6.     Const CDS_UPDATEREGISTRY As Integer = &H1
    7.     Const CDS_TEST As Long = &H2
    8.  
    9.     Const CCDEVICENAME As Integer = 32
    10.     Const CCFORMNAME As Integer = 32
    11.  
    12.     Const DISP_CHANGE_SUCCESSFUL As Integer = 0
    13.     Const DISP_CHANGE_RESTART As Integer = 1
    14.     Const DISP_CHANGE_FAILED As Integer = -1
    15.  
    16.     Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
    17.     Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Long) As Integer
    18.  
    19.     <StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
    20.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public dmDeviceName As String
    21.         Public dmSpecVersion As Short
    22.         Public dmDriverVersion As Short
    23.         Public dmSize As Short
    24.         Public dmDriverExtra As Short
    25.         Public dmFields As Integer
    26.         Public dmOrientation As Short
    27.         Public dmPaperSize As Short
    28.         Public dmPaperLength As Short
    29.         Public dmPaperWidth As Short
    30.         Public dmScale As Short
    31.         Public dmCopies As Short
    32.         Public dmDefaultSource As Short
    33.         Public dmPrintQuality As Short
    34.         Public dmColor As Short
    35.         Public dmDuplex As Short
    36.         Public dmYResolution As Short
    37.         Public dmTTOption As Short
    38.         Public dmCollate As Short
    39.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
    40.         Public dmUnusedPadding As Short
    41.         Public dmBitsPerPel As Short
    42.         Public dmPelsWidth As Integer
    43.         Public dmPelsHeight As Integer
    44.         Public dmDisplayFlags As Integer
    45.         Public dmDisplayFrequency As Integer
    46.     End Structure
    47.  
    48.     Public Sub changeRes(ByVal theWidth As Integer, ByVal theHeight As Integer)
    49.  
    50.         Dim DevM As DEVMODE
    51.  
    52.         DevM.dmDeviceName = New [String](New Char(32) {})
    53.         DevM.dmFormName = New [String](New Char(32) {})
    54.         DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
    55.  
    56.  
    57.         If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
    58.             Dim lResult As Integer
    59.  
    60.             DevM.dmPelsWidth = theWidth
    61.             DevM.dmPelsHeight = theHeight
    62.             DevM.dmPelsWidth = 1280
    63.             DevM.dmPelsHeight = 1024
    64.  
    65.             lResult = ChangeDisplaySettings(DevM, CDS_TEST)
    66.  
    67.             If lResult = DISP_CHANGE_FAILED Then
    68.                 MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
    69.             Else
    70.  
    71.                 lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
    72.  
    73.                 Select Case lResult
    74.                     Case DISP_CHANGE_RESTART
    75.                         MsgBox("You must restart your computer to apply these changes.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Has Changed")
    76.                     Case DISP_CHANGE_SUCCESSFUL
    77.                         MsgBox("Display Change Successful.", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Screen Resolution Successful")
    78.                     Case Else
    79.                         MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
    80.                 End Select
    81.             End If
    82.  
    83.  
    84.         End If
    85.     End Sub
    86.  
    87. End Module

  2. #2
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    Wonderful code!

    I just added some opt parameters that force the change to be... more smoother. (I hate when it's 60 Hertz and I can the monitor moving :P)
    VB Code:
    1. Public Sub Main()
    2.         MessageBox.Show(changeRes(1280, 1024).ToString)
    3.     End Sub
    4.  
    5.  
    6.     Public Function changeRes(ByVal theWidth As Integer, ByVal theHeight As Integer, _
    7.         Optional ByVal theFrequency As Integer = 85, Optional ByVal theBitDepth As Integer = 32) As Boolean
    8.  
    9.         Dim DevM As DEVMODE
    10.  
    11.         DevM.dmDeviceName = New [String](New Char(32) {})
    12.         DevM.dmFormName = New [String](New Char(32) {})
    13.         DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))
    14.  
    15.  
    16.         If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
    17.             Dim lResult As Integer
    18.  
    19.             DevM.dmPelsWidth = theWidth
    20.             DevM.dmPelsHeight = theHeight
    21.             DevM.dmBitsPerPel = theBitDepth
    22.             DevM.dmDisplayFrequency = theFrequency
    23.  
    24.             lResult = ChangeDisplaySettings(DevM, CDS_TEST)
    25.  
    26.             If lResult = DISP_CHANGE_FAILED Then
    27.                 Return False
    28.             Else
    29.  
    30.                 lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
    31.  
    32.                 Select Case lResult
    33.                     Case DISP_CHANGE_RESTART
    34.                         Return False
    35.                     Case DISP_CHANGE_SUCCESSFUL
    36.                         Return True
    37.                     Case Else
    38.                         Return False
    39.                 End Select
    40.  
    41.             End If
    42.  
    43.  
    44.         End If
    45.     End Function

  3. #3
    New Member
    Join Date
    Dec 2004
    Posts
    3

    Re: Screen Resolution Change Example Complete!

    SWEET! Just what ive been looking for. Just wondering, but how can i make it so it changes back to the original resolution on form close?

  4. #4
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265

    Re: Screen Resolution Change Example Complete!

    Be aware that changing the screen resolution of a monitor can seriously mess up the display on any other monitors in a dual-monitor or multi-monitor setup. You can really annoy some people by doing that sort of thing - I'd probably be one of them!. Just because you can do something doesn't necessarily mean that you should
    Nick.

  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Screen Resolution Change Example Complete!

    The code doesn't work i get a change sucessful but no changes on resolution... Anyway if a sold/distributed an application i would put Minimum Resolution 1024x768(for example) on the system requirement section
    Last edited by Asgorath; Jan 4th, 2005 at 07:26 AM.
    "The dark side clouds everything. Impossible to see the future is."

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