Search Tools Links Login

Check and Change Resolution


With a little API magic, you can check the current screen resolution, and change it at runtime if needed.

Module

Option Explicit

'Declares:

Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
Private Declare Function EnumDisplaySettings Lib "user32.dll" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Public Declare Function ChangeDisplaySettings Lib "user32.dll" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000

Public 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

Public DevChg As DEVMODE

Public Sub ChangeRes(iWidth As Single, iHeight As Single)
    Dim a As Boolean
    Dim i&
    i = 0

    Do
        a = EnumDisplaySettings(0&, i&, DevChg)
        i = i + 1
    Loop Until (a = False)

    Dim b&
    DevChg.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
    DevChg.dmPelsWidth = iWidth
    DevChg.dmPelsHeight = iHeight
    b = ChangeDisplaySettings(DevChg, 0)

End Sub

Usage

Public Sub CheckRes()
    Dim X As Long, Y As Long
    Dim MsgTitle As String
    Dim MsgStyle As Long
    Dim Msg As String
    Dim MsgResponse As Long

    Y = GetSystemMetrics(0)
    X = GetSystemMetrics(1)

    If X < 600 And Y < 800 Then
        MsgTitle = "Invalid Resolution"
        MsgStyle = vbYesNo + vbCritical
        Msg = "You current video resolution is: " & Y & " X " & X & vbCrLf
        Msg = Msg & "This program requires a resolution 800 x 600 or greater." & vbCrLf & vbCrLf
        Msg = Msg & "Do you wish me to adjust your resolution to 800x600?"
        MsgResponse = MsgBox(Msg, MsgStyle, MsgTitle)
        If MsgResponse = vbYes Then
            Call ChangeRes(800, 600)
            End
        End If
    End If
End Sub

About this post

Posted: 2019-10-02
By: DavidCostelloe
Viewed: 391 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.