Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Visual Basic
Expertise: Advanced
Jul 24, 1998



Application Security Testing: An Integral Part of DevOps

Change Display Settings on the Fly

When writing a game for Windows 95, set the display resolution to 640-by-480, set the color palette to True Color when it runs, and restore it to its initial mode when it ends. Use this function to implement it:

 '- Declares
Private Declare Function lstrcpy _
	Lib "kernel32" Alias "lstrcpyA" _
	(lpString1 As Any, lpString2 As Any) _
	As Long
Private Type DEVMODE
	dmDeviceName As String * CCHDEVICENAME
	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 * CCHFORMNAME
	dmUnusedPadding As Integer
	dmBitsPerPel As Integer
	dmPelsWidth As Long
	dmPelsHeight As Long
	dmDisplayFlags As Long
	dmDisplayFrequency As Long
End Type
Private Declare Function _
	ChangeDisplaySettings Lib _
	"User32" Alias "ChangeDisplaySettingsA" (_
	ByVal lpDevMode As Long, _
	ByVal dwflags As Long) As Long
'- code
' Here is the function that sets the display 
' mode. Width is the width of screen, Height 
' is the height of screen, Color is the number 
' of bits per pixel. Set the Color value to -1 
' if you only want to change the screen 
' resolution.
Public Function SetDisplayMode(Width As _
	Integer,Height As Integer, Color As _
	Integer) As Long
Const DM_PELSWIDTH = &H80000
Const DM_PELSHEIGHT = &H100000
Const DM_BITSPERPEL = &H40000
Dim NewDevMode As DEVMODE
Dim pDevmode As Long
With NewDevMode
	.dmSize = 122
	If Color = -1 Then
		.dmFields = DM_PELSWIDTH Or _
	End If
	.dmPelsWidth = Width
	.dmPelsHeight = Height

	If Color <> -1 Then
		.dmBitsPerPel = Color
	End If
End With
pDevmode = lstrcpy(NewDevMode, NewDevMode)
SetDisplayMode = ChangeDisplaySettings(pDevmode, 0)
End Function

You can change the display mode easily with this function. For example, write this code that changes the resolution to 640-by-480 and the color palette to 24-bit True Color:

 i = SetDisplayMode(640, 480, 24)

If the function is successful, it returns zero.

Huang Xiongbai
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date