PDA

Click to See Complete Forum and Search --> : Screen Rez Question


Spuds
Sep 23rd, 2000, 08:33 AM
I've gotten a little bit of code that allows me to change the screen rez on the load up of my program (saving original settings, and switching back on program unload). ** The code is at the bottom **

Everything works fine, but I wish for the program not to go "full screen" when it loads. When the rez changes, I lose the Windows desktop (and program bar), and the only way to access the desktop is to Alt-Tab out of the program. This just isn't as clean as I want it.

Can someone please browse this code, and let me know how to, rather than "full-screening" the program, maximize the program on the screen?

Thank you, in advance.


** MODULE CODE **

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
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal
dwReserved As Long) As Long

Public Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_REBOOT = 2
Public Const EWX_FORCE = 4
Public Const CCDEVICENAME = 32
Public Const CCFORMNAME = 32
Public Const DM_BITSPERPEL = &H40000
Public Const DM_PELSWIDTH = &H80000
Public Const DM_PELSHEIGHT = &H100000
Public Const CDS_UPDATEREGISTRY = &H1
Public Const CDS_TEST = &H4
Public Const DISP_CHANGE_SUCCESSFUL = 0
Public Const DISP_CHANGE_RESTART = 1

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


** FORM 1 CODE **

Dim OriginalH As Integer, OriginalW As Integer

Private Sub Form_Load()

' get the original settings
OriginalH = (Screen.Height / 12)
OriginalW = (Screen.Width / 12)

' switch to the 800x600 setting
Setup_RunScreenRez 800, 600

End Sub

Private Sub Form_Unload(Cancel As Integer)

Setup_RunScreenRez OriginalW, OriginalH

End Sub

Private Sub Setup_RunScreenRez(Width As Integer, Height As Integer)

Dim DevM As DEVMODE

'Get the info into DevM
erg& = EnumDisplaySettings(0&, 0&, DevM)
'We don't change the colordepth, because a rebot will be necessary

DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT 'Or DM_BITSPERPEL
DevM.dmPelsWidth = Width 'ScreenWidth
DevM.dmPelsHeight = Height 'ScreenHeight
'DevM.dmBitsPerPel = 32 (could be 8, 16, 32 or even 4)

'Now change the display and check if possible
erg& = ChangeDisplaySettings(DevM, CDS_TEST)

'Check if succesfull
Select Case erg&
Case DISP_CHANGE_RESTART
an = MsgBox("You've to reboot", vbYesNo + vbSystemModal, "Info")
If an = vbYes Then
erg& = ExitWindowsEx(EWX_REBOOT, 0&)
End If
Case DISP_CHANGE_SUCCESSFUL
erg& = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
MsgBox "Everything's ok", vbOKOnly + vbSystemModal, "It worked!"
Case Else
MsgBox "Mode not supported", vbOKOnly + vbSystemModal, "Error"
End Select

End Sub

Yonatan
Sep 23rd, 2000, 10:07 AM
You're using some control called Setup.
If don't need it for anything else, remove it immediately! :rolleyes:
And use this code:

Option Explicit

Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

Private Const DM_BITSPERPEL = &H40000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000

Private Const DISP_CHANGE_SUCCESSFUL = 0

Private Const CDS_UPDATEREGISTRY = &H1
Private Const CDS_TEST = &H2

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 EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, ByVal iModeNum As Long, lpDevMode As DEVMODE) As Long
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As DEVMODE, ByVal dwFlags As Long) As Long

Function ChangeResolution(Optional ByVal ScreenX As Long = 0, Optional ByVal ScreenY As Long = 0, Optional ByVal ColorBits As Long = 0) As Boolean
Dim tDM As DEVMODE

With tDM
' Get current resolution settings:
.dmSize = Len(tDM)
If EnumDisplaySettings(vbNullString, 0, tDM) = 0 Then Exit Function

' Change screen width:
If Not ScreenX = 0 Then
.dmFields = DM_PELSWIDTH
.dmPelsWidth = ScreenX
End If

' Change screen height:
If Not ScreenY = 0 Then
.dmFields = .dmFields Or DM_PELSHEIGHT
.dmPelsHeight = ScreenY
End If

' Change bits per pixel:
If Not ColorBits = 0 Then
.dmFields = .dmFields Or DM_BITSPERPEL
.dmBitsPerPel = ColorBits
End If
End With

' Test if resolution is OK, and if it is, change it:
If ChangeDisplaySettings(tDM, CDS_TEST) = DISP_CHANGE_SUCCESSFUL Then ChangeResolution = (ChangeDisplaySettings(tDM, CDS_UPDATEREGISTRY) = DISP_CHANGE_SUCCESSFUL)
End Function

Then just use the ChangeResolution function. Include a ScreenX, ScreenY and ColorBits. They are all optional so you can change only one or only two if you want.
For example, when you want to retain the current resolution but change to 16-bit color, or retain the current color mode but change to 800x600.
It will return False if it failed (100 x 200 x 3 would make it fail, for instance ;)), or True if it succeeded.

Example:

Dim OriginalX As Long, OriginalY As Long

Private Sub Form_Load()
' Preserve original resolution
OriginalX = Screen.Width / Screen.TwipsPerPixelX
OriginalY = Screen.Height / Screen.TwipsPerPixelY
' Change to 800x600 (bits per pixel don't change)
Call ChangeResolution(800, 600)
' Maximize the window to fullscreen, without hiding the taskbar, etc.
WindowState = vbMaximized
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' Change to the original resolution
Call ChangeResolution(OriginalX, OriginalY)
End Sub

jtm7699
Oct 4th, 2000, 08:39 AM
This changing resoultion works nice, but it also changes the Freq. How can you keep that the same or set it back.

I know this is an old thread sorry

Yonatan
Oct 4th, 2000, 09:05 AM
Here's the same code, tweaked to allow you to change the frequency as well:

Option Explicit

Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

Private Const DM_BITSPERPEL = &H40000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Const DM_DISPLAYFREQUENCY = &H400000

Private Const DISP_CHANGE_SUCCESSFUL = 0

Private Const CDS_UPDATEREGISTRY = &H1
Private Const CDS_TEST = &H2

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 EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As String, ByVal iModeNum As Long, lpDevMode As DEVMODE) As Long
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As DEVMODE, ByVal dwFlags As Long) As Long

Function ChangeResolution(Optional ByVal ScreenX As Long = 0, Optional ByVal ScreenY As Long = 0, Optional ByVal ColorBits As Long = 0, Optional ByVal nFrequency As Long = -1) As Boolean
Dim tDM As DEVMODE

With tDM
' Get current resolution settings:
.dmSize = Len(tDM)
If EnumDisplaySettings(vbNullString, 0, tDM) = 0 Then Exit Function

' Change screen width:
If Not ScreenX = 0 Then
.dmFields = .dmFields Or DM_PELSWIDTH
.dmPelsWidth = ScreenX
End If

' Change screen height:
If Not ScreenY = 0 Then
.dmFields = .dmFields Or DM_PELSHEIGHT
.dmPelsHeight = ScreenY
End If

' Change bits per pixel:
If Not ColorBits = 0 Then
.dmFields = .dmFields Or DM_BITSPERPEL
.dmBitsPerPel = ColorBits
End If

' Change frequency:
If Not nFrequency = -1 Then
.dmFields = .dmFields Or DM_DISPLAYFREQUENCY
.dmDisplayFrequency = nFrequency
End If
End With

' Test if resolution is OK, and if it is, change it:
If ChangeDisplaySettings(tDM, CDS_TEST) = DISP_CHANGE_SUCCESSFUL Then ChangeResolution = (ChangeDisplaySettings(tDM, CDS_UPDATEREGISTRY) = DISP_CHANGE_SUCCESSFUL)
End Function

Don't be sorry, it's not your fault that it's an old thread. :rolleyes:
The function still returns False on failure and True on success, but now it has another way to fail! (Invalid frequency)

AeroII
Dec 15th, 2004, 01:03 PM
can someone send me the source code for this? I cant seem to get it to work. Im using vb.net though, so can someone send me source or convert it for me?