API function

brettvba

MrExcel MVP
Joined
Feb 18, 2002
Messages
1,030
does anyone have code for showing a current screen resolution

I have some for VB
how can i get this to work on vba i think it has something to do with a defined function

Any Ideas?


Public Function GetScreenResolution() As Boolean
Dim lTemp As String
'Temporary string to hold returned screen
'resolution
lTemp = GetSystemMetrics(SM_CXSCREEN) & _
"x" & GetSystemMetrics(SM_CYSCREEN)
'Call the API function twice to return
'screen size for each axis as format into
'the temporary string
If lTemp = "800x600" Then
'Check whether resolution is set to 800x600
GetScreenResolution = True
'True
Else
GetScreenResolution = False
'False
End If
End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try the following:

Option Explicit
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

'Constants for GetSystemMetrics

Const SM_CXSCREEN = 0 ' Width of screen
Const SM_CYSCREEN = 1 ' Height of screen


Sub Get_System_Metrics()

Dim XVal As Long, YVal As Long
YVal = GetSystemMetrics(SM_CYSCREEN)
XVal = GetSystemMetrics(SM_CXSCREEN)
MsgBox "Your Screen Resolution is " & XVal & " by " & YVal

End Sub
 
Upvote 0
Hi AL Thanks for that I also had the following code as well which I could not get to work. It is supposed to change the resolution automatically>Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, ByVal iModeNum As Long, _
lpDevMode As Any) As Boolean
Declare Function ChangeDisplaySettings Lib "user32" Alias _
"ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long

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 Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
Public Const DISP_CHANGE_FAILED = -1
Public Const DISP_CHANGE_BADMODE = -2
Public Const DISP_CHANGE_NOTUPDATED = -3
Public Const DISP_CHANGE_BADFLAGS = -4
Public Const DISP_CHANGE_BADPARAM = -5
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H2
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Sub Form_Load()
'Replace '800,600' with the resolution you want to switch to.
'You can change the color pallete to 32 - Bit by changing the '16' below with '32'
ChangeScreenSettings 800, 600, 16 - Bit
End Sub
Public Sub ChangeScreenSettings(lWidth As Integer, _
lHeight As Integer, lColors As Integer)
Dim tDevMode As DEVMODE, lTemp As Long, lIndex As Long
lIndex = 0
Do
lTemp = EnumDisplaySettings(0&, lIndex, tDevMode)
If lTemp = 0 Then Exit Do
lIndex = lIndex + 1
With tDevMode
If .dmPelsWidth = lWidth And .dmPelsHeight = lHeight _
And .dmBitsPerPel = lColors Then
lTemp = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
Exit Do
End If
End With
Loop
Select Case lTemp
Case DISP_CHANGE_SUCCESSFUL
MsgBox "The display settings changed successfully", _
vbInformation
Case DISP_CHANGE_RESTART
MsgBox "The computer must be restarted in order for the graphics mode to work", vbQuestion
Case DISP_CHANGE_FAILED
MsgBox "The display driver failed the specified graphics mode", vbCritical
Case DISP_CHANGE_BADMODE
MsgBox "The graphics mode is not supported", vbCritical
Case DISP_CHANGE_NOTUPDATED
MsgBox "Unable to write settings to the registry", vbCritical
Case DISP_CHANGE_BADFLAGS
MsgBox "You Passed invalid data", vbCritical
End Select
End Sub
 
Upvote 0
The code works, but you have the order mixed up. Try the following setup:

Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" _
(ByVal lpszDeviceName As Long, ByVal iModeNum As Long, _
lpDevMode As Any) As Boolean
Declare Function ChangeDisplaySettings Lib "user32" Alias _
"ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long

Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1
Public Const DISP_CHANGE_FAILED = -1
Public Const DISP_CHANGE_BADMODE = -2
Public Const DISP_CHANGE_NOTUPDATED = -3
Public Const DISP_CHANGE_BADFLAGS = -4
Public Const DISP_CHANGE_BADPARAM = -5
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H2
Public Const DM_BITSPERPEL = &H40000
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


Sub Form_Load()
'Replace '800,600' with the resolution you want to switch to.
'You can change the color pallete to 32 - Bit by changing the '16' below with '32'
ChangeScreenSettings 800, 600, 16 - Bit
End Sub
Public Sub ChangeScreenSettings(lWidth As Integer, _
lHeight As Integer, lColors As Integer)
Dim tDevMode As DEVMODE, lTemp As Long, lIndex As Long
lIndex = 0
Do
lTemp = EnumDisplaySettings(0&, lIndex, tDevMode)
If lTemp = 0 Then Exit Do
lIndex = lIndex + 1
With tDevMode
If .dmPelsWidth = lWidth And .dmPelsHeight = lHeight _
And .dmBitsPerPel = lColors Then
lTemp = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
Exit Do
End If
End With
Loop
Select Case lTemp
Case DISP_CHANGE_SUCCESSFUL
MsgBox "The display settings changed successfully", _
vbInformation
Case DISP_CHANGE_RESTART
MsgBox "The computer must be restarted in order for the graphics mode to work", vbQuestion
Case DISP_CHANGE_FAILED
MsgBox "The display driver failed the specified graphics mode", vbCritical
Case DISP_CHANGE_BADMODE
MsgBox "The graphics mode is not supported", vbCritical
Case DISP_CHANGE_NOTUPDATED
MsgBox "Unable to write settings to the registry", vbCritical
Case DISP_CHANGE_BADFLAGS
MsgBox "You Passed invalid data", vbCritical
End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top