Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Change Resolution of a specified monitor using ChangeDisplaySettingsEx.
[ All Languages » VB »  System]

Total Hit ( 8163)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


To change resolution of your primary display device you can use ChangeDisplaySettings API but if you are working with multiple monitors and you want to change resolution of a specified monitor then you have to use ChangeDisplaySettingsEx API. You can specify Display device name while calling ChangeDisplaySettingsEx.

Step-By-Step Example

- Create a standard exe project
- Add the following code in form1

Click here to copy the following block
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const CDS_TEST = &H4

Private Type DISPLAY_DEVICE
  cb As Long
  DeviceName As String * 32
  DeviceString As String * 128
  StateFlags As Long
  DeviceID As String * 128
  DeviceKey As String * 128
End Type

Private Type DEVMODE
  dmDeviceName As String * CCDEVICENAME
  dmSpecVersion As Integer
  dmDriverVersion As Integer
  dmSize As Integer
  dmDriverExtra As Integer
  dmFields As Long
  dmOrientation As Integer
  dmPaperSize As Integer
  dmPaperLength As Integer
  dmPaperWidth As Integer
  dmScale As Integer
  dmCopies As Integer
  dmDefaultSource As Integer
  dmPrintQuality As Integer
  dmColor As Integer
  dmDuplex As Integer
  dmYResolution As Integer
  dmTTOption As Integer
  dmCollate As Integer
  dmFormName As String * CCFORMNAME
  dmUnusedPadding As Integer
  dmBitsPerPel As Integer
  dmPelsWidth As Long
  dmPelsHeight As Long
  dmDisplayFlags As Long
  dmDisplayFrequency As Long
  dmICMMethod As Long  'NT 4.0
  dmICMIntent As Long  'NT 4.0
  dmMediaType As Long  'NT 4.0
  dmDitherType As Long  'NT 4.0
  dmReserved1 As Long  'NT 4.0
  dmReserved2 As Long  'NT 4.0
  dmPanningWidth As Long  'Win2000
  dmPanningHeight As Long  'Win2000
End Type

Private Declare Function ChangeDisplaySettingsEx Lib "user32" Alias "ChangeDisplaySettingsExA" ( _
    lpszDeviceName As String, _
    lpDevMode As Any, _
    ByVal hWnd As Long, _
    ByVal dwFlags As Long, _
    lParam As Any) As Long

Private Declare Function EnumDisplayDevices Lib "user32" Alias "EnumDisplayDevicesA" ( _
    Unused As Any, _
    ByVal iDevNum As Long, _
    lpDisplayDevice As DISPLAY_DEVICE, _
    ByVal dwFlags As Long) As Boolean

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim OldXRes As Long, OldYRes As Long, T As Long
Dim strDeviceName As String, DeviceNum As Integer

Private Sub Form_Load()

  Dim DD As DISPLAY_DEVICE, DevM As DEVMODE
  DD.cb = Len(DD)

  DeviceNum = 0

  If EnumDisplayDevices(ByVal 0&, DeviceNum, DD, ByVal 0&) Then
    strDeviceName = Left$(DD.DeviceName, InStr(1, DD.DeviceName, Chr$(0)) - 1)
  Else
    MsgBox "Can not read device name"
  End If

  OldXRes = Screen.Width / Screen.TwipsPerPixelX
  OldYRes = Screen.Height / Screen.TwipsPerPixelY

  DevM.dmSize = Len(DevM)
  'we want to change the horizontal and the vertical resolution
  DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
  DevM.dmPelsWidth = 640
  DevM.dmPelsHeight = 480

  'change the display settings
  'strDeviceName = "\\.\DISPLAY1" '//Monitor1
  'strDeviceName = "\\.\DISPLAY2" '//Monitor2
  'Call ChangeDisplaySettingsEx(ByVal strDeviceName, DevM, ByVal 0&, CDS_TEST, ByVal 0&)
  Call ChangeDisplaySettingsEx(ByVal strDeviceName, DevM, ByVal 0&, CDS_TEST, ByVal 0&)

  Sleep 5000
  DevM.dmPelsWidth = OldXRes
  DevM.dmPelsHeight = OldYRes

  'change the display settings back to the old settings
  Call ChangeDisplaySettingsEx(ByVal strDeviceName, DevM, ByVal 0&, CDS_TEST, ByVal 0&)
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.