VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This API function changes the display settings on your monitor.

by Jamal 331 (5 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 6th February 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This API function changes the display settings on your monitor.

API Declarations


Const WM_DISPLAYCHANGE = &H7E
Const HWND_BROADCAST = &HFFFF&
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const CCDEVICENAME = 32
Const CCFORMNAME = 32
Const DM_BITSPERPEL = &H40000
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const CDS_UPDATEREGISTRY = &H1
Const CDS_TEST = &H4
Const DISP_CHANGE_SUCCESSFUL = 0
Const DISP_CHANGE_RESTART = 1
Const BITSPIXEL = 12
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
End Type
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Dim OldX As Long, OldY As Long, nDC As Long


Rate This API function changes the display settings on your monitor.



    Dim DevM As DEVMODE, ScInfo As Long, erg As Long, an As VbMsgBoxResult
    erg = EnumDisplaySettings(0&, 0&, DevM)
    DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
    DevM.dmPelsWidth = X
    DevM.dmPelsHeight = Y
    DevM.dmBitsPerPel = Bits
    erg = ChangeDisplaySettings(DevM, CDS_TEST)
    Select Case erg&
        Case DISP_CHANGE_RESTART
            an = MsgBox("You need to restart your computer for new settings to take effect.", vbYesNo + vbSystemModal, "Info")
            If an = vbYes Then
                erg& = ExitWindowsEx(EWX_REBOOT, 0&)
            End If
        Case DISP_CHANGE_SUCCESSFUL
            erg = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
            ScInfo = Y * 2 ^ 16 + X
            SendMessage HWND_BROADCAST, WM_DISPLAYCHANGE, ByVal Bits, ByVal ScInfo
            MsgBox "Resolution has been changed...", vbOKOnly + vbExclamation, "Resolution Change"
        Case Else
            MsgBox "Display setting is not supported", vbOKOnly + vbCritical, "Error"
    End Select
End Sub
Private Sub Form_Load()
    Dim nDC As Long
    OldX = Screen.Width / Screen.TwipsPerPixelX
    OldY = Screen.Height / Screen.TwipsPerPixelY
    nDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0&)
    ChangeRes 640, 480, GetDeviceCaps(nDC, BITSPIXEL)
    ' You can use ChangeRes 800, 600, GetDeviceCaps(nDC, BITSPIXEL) or ChangeRes 1280, 1024, GetDeviceCaps(nDC, BITSPIXEL).
    ' Just change the two numbers above to your resolution in choice.
    ' Make sure that it is compatible with your monitor or it will cause an error.

End Sub
Private Sub Form_Unload(Cancel As Integer)
    ChangeRes OldX, OldY, GetDeviceCaps(nDC, BITSPIXEL)
    DeleteDC nDC
End Sub

Download this snippet    Add to My Saved Code

This API function changes the display settings on your monitor. Comments

No comments have been posted about This API function changes the display settings on your monitor.. Why not be the first to post a comment about This API function changes the display settings on your monitor..

Post your comment

Subject:
Message:
0/1000 characters