This is a function that allow to change the screen resolution.
My problem is I no understand how to adapt the Rev.1 function
VarSetCapacity(...)
and the Rev. 1 function
NumPut
to Rev. 2 equivalent function.
Thank you very much
Code: Select all
DisplaySet( sDispRes := False, iColorRes := False, iFrequency := False, iDispNum := 0, sDispPos := False )
{
; MS constants
Static CDS_TEST := 0x00000002
Static DISP_CHANGE_SUCCESSFUL := 0
Static DM_ORIENTATION := 0x00000001
Static DM_POSITION := 0x00000020
Static DM_BITSPERPEL := 0x00040000
Static DM_PELSWIDTH := 0x00080000
Static DM_PELSHEIGHT := 0x00100000
Static DM_DISPLAYFREQUENCY := 0x00400000
; ==============================================================================================================================
; AHK constants
Static TCHARsize := A_IsUnicode ? 64 : 32 ; size of TCHAR members dmDeviceName and dmFormName
Static DEVMODEsize := 92 + ( TCHARsize * 2 ) ; size of DEVMODE structure
Static offSize := 4 + TCHARsize ; dmSize
Static offFields := 8 + TCHARsize ; dmFields
Static offPosition := 12 + TCHARsize ; dmPosition
Static offColorRes := 40 + ( TCHARsize * 2 ) ; dmBitsPerPel
Static offWidth := 44 + ( TCHARsize * 2 ) ; dmPelsWidth
Static offHeight := 48 + ( TCHARsize * 2 ) ; dmPelsHeight
Static offFrequency := 56 + ( TCHARsize * 2 ) ; dmDisplayFrequency
; ==============================================================================================================================
; Create the DEVMODE structure
VarSetCapacity( DEVMODE, DEVMODEsize, 0 )
NumPut( DEVMODEsize, DEVMODE, offSize, "UShort" )
; =============================================================================================================================
; Initialize DEVMODEaddr and Fields with zero ( NULL ) in case the default registry settings shall be set.
DEVMODEaddr := 0
Fields := 0
; ==============================================================================================================================
; Check optional parameters.
If( sDispRes )
{
aiCoord := StrSplit( sDispRes, "|" )
If( aiCoord.MaxIndex() <> 2 )
{
ErrorLevel := "Bad parameter sDispRes!"
Return False
} ; If( Part0 <> 2 )
NumPut( aiCoord[ 1 ], DEVMODE, offWidth, "UInt" )
NumPut( aiCoord[ 2 ], DEVMODE, offHeight, "UInt" )
Fields |= DM_PELSWIDTH | DM_PELSHEIGHT
} ; If( sDispRes )
If( iColorRes )
{
NumPut( iColorRes, DEVMODE, offColorRes, "UInt" )
Fields |= DM_BITSPERPEL
} ; If( iColorRes )
If( iFrequency )
{
NumPut( iFrequency, DEVMODE, offFrequency, "UInt" )
Fields |= DM_DISPLAYFREQUENCY
} ; If( iFrequency )
If( iDispNum < 1 )
SysGet, iDispNum, MonitorPrimary
If( sDispPos )
{
aiPos := StrSplit( sDispPos, "|" )
SysGet, Displays, MonitorCount
If(( aiPos.MaxIndex() <> 2 ) || ( Displays < 2 ))
{
ErrorLevel := "Bad parameter sDispPos!"
Return False
} ; If(( Part0 <> 2 ) || ( Displays < 2 ))
If( iDispNum > Displays )
{
ErrorLevel := "Bad parameter iDispNum!"
Return False
} ; If( iDispNum > Displays )
NumPut( aiPos[ 1 ], DEVMODE, offPosition + 0, "UInt" )
NumPut( aiPos[ 2 ], DEVMODE, offPosition + 4, "UInt" )
Fields |= DM_POSITION
} ; If( sDispPos )
; ==============================================================================================================================
; Get the device name.
SysGet, DevName, MonitorName, %iDispNum%
If( DevName == "" )
{
ErrorLevel := "Bad parameter iDispNum!"
Return False
} ; If( DevName = "" )
; ==============================================================================================================================
; New settings will be checked, if any. If the DllCall returns an error the new settings won't be set.
If( Fields )
{
NumPut( Fields, DEVMODE, offFields, "UInt" )
DEVMODEaddr := &DEVMODE
If( RetVal := DllCall( "ChangeDisplaySettingsEx", "Str", DevName, "Ptr", DEVMODEaddr, "Ptr", 0, "UInt", CDS_TEST, "Ptr", 0, "Int" ) )
{
ErrorLevel := RetVal
Return False
} ; If( RetVal := DllCall( "ChangeDisplaySettingsEx", "Str", DevName, "Ptr", DEVMODEaddr, "Ptr", 0, "UInt", CDS_TEST, "Ptr", 0, "Int" ) )
} ; If( Fields )
; ==============================================================================================================================
; New settings will be set dynamically.
If( RetVal := DllCall( "ChangeDisplaySettingsEx", "Str", DevName, "Ptr", DEVMODEaddr, "Ptr", 0, "UInt", 0, "Ptr", 0, "Int" ) )
{
ErrorLevel := RetVal
Return False
} ; If( RetVal := DllCall( "ChangeDisplaySettingsEx", "Str", DevName, "Ptr", DEVMODEaddr, "Ptr", 0, "UInt", 0, "Ptr", 0, "Int" ) )
; ==============================================================================================================================
; All done successfully.
ErrorLevel := 0
Return True
}