larsiusprime 06-25-2004, 11:27 PM I've found plenty of code snippets that say how to change the current resolution to whatever you want in VB 6 and previous, but HOW IN THE BLAZES do you do it in VB.Net?
What I want to do is basically grab myself a flash .swf and insert it into my VB program (already figured that out) and then get the old resolution on loading (figured that out too) and then change the resolution to, say 640x480 immediately and fullscreen the application also on loading. Then, on closing I want to change the resolution back to the way it was. That's all I really need insofar as VB is concerned.
Anyone know how to change resolutions in VB.NET? I only have the .Net version and the older versions of the solution don't seem to work...
OnErr0r 06-25-2004, 11:45 PM You'll be using the same APIs in vb.net, the difference will be in the data types used. It's just a matter of porting the declares and structures used to vb.net.
VB6 VB.NET
Long Integer
Integer Short
Byte Byte
larsiusprime 06-26-2004, 03:47 PM I'm really new to visual basic. I don't understand some things you guys take for granted, and I'm sure if I had the basics down I'd get exactly what you're trying to say. For instance, WHERE do I need to put the code and what changes do I need to make to it besides substituting deprecated datatypes? I get all these weird errors and since I know very little about VB I need some help. Anyway, here's the code I'm tyring to port from VB 6.0 to VB.NET:
'**************************************
' Name: ChangeRes (Fixed)
' Description:This Function will change
' your Windows Resolution. It is very simp
' le, and it does what most Resolution Cha
' nge Functions don't do, it changes the t
' he Bits Per Pixels as well as the Screen
' Width and Height.
' By: ScAnFrEaKisBack
'
' Inputs:Dim RetValue As Integer
RetValue = ChangeRes(800, 600, 32)
'
' Returns:1 = Resolution Successfully Ch
' anged
0 = Resolution Was Not Changed
'
' Assumes:If you have any direct questio
' ns or comments please E-Mail Gordon at s
' canfreak@rogers.com
'
' Side Effects:This Code used to have on
' e side effect and is now being fixed as
' I type this...which was the lack of api
' calls and const's and type's.
'
'This code is copyrighted and has' limited warranties.Please see http://w
' ww.Planet-Source-Code.com/vb/scripts/Sho
' wCode.asp?txtCodeId=39535&lngWId=1'for details.'**************************************
'This code goes at the very top of your
' source window or if you want to stick it
' in a module you need to take the "Privat
' e" off of each line.
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwflags As Long) As Long
Private Const CCDEVICENAME = 32
Private Const CCFORMNAME = 32
Private Const DM_BITSPERPEL = &H60000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private 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
'This function goes in your source windo
' w or if you want to stick it in a module
' you need to take the "Private" off of ea
' ch line.
Private Function ChangeRes(Width As Single, Height As Single, BPP As Integer) As Integer
On Error Goto ERROR_HANDLER
Dim DevM As DEVMODE, I As Integer, ReturnVal As Boolean, RetValue, OldWidth As Single, OldHeight As Single, OldBPP As Integer
Call EnumDisplaySettings(0&, -1, DevM)
OldWidth = DevM.dmPelsWidth
OldHeight = DevM.dmPelsHeight
OldBPP = DevM.dmBitsPerPel
I = 0
Do
ReturnVal = EnumDisplaySettings(0&, I, DevM)
I = I + 1
Loop Until (ReturnVal = False)
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = Width
DevM.dmPelsHeight = Height
DevM.dmBitsPerPel = BPP
Call ChangeDisplaySettings(DevM, 1)
RetValue = MsgBox("Do You Wish To Keep Your Screen Resolution To " & Width & "x" & Height & " - " & BPP & " BPP?", vbQuestion + vbOKCancel, "Change Resolution Confirm:")
If RetValue = vbCancel Then
DevM.dmPelsWidth = OldWidth
DevM.dmPelsHeight = OldHeight
DevM.dmBitsPerPel = OldBPP
Call ChangeDisplaySettings(DevM, 1)
MsgBox "Old Resolution(" & OldWidth & " x " & OldHeight & ", " & OldBPP & " Bit) Successfully Restored!", vbInformation + vbOKOnly, "Resolution Confirm:"
ChangeRes = 0
Else
ChangeRes = 1
End If
Exit Function
ERROR_HANDLER:
ChangeRes = 0
End Function
OnErr0r 06-27-2004, 09:34 AM As Any is gone, so you'll need to substitute the structure name there, or ByVal As Integer, if you need to pass a null. String * x also takes some special attributes which are spelled out in VB structure documentation. And constants now require a specific data type.
larsiusprime 06-27-2004, 01:21 PM Thanks a lot. As I said, I'm REALLY new to VB, so I'll have to hunt down the documentation, but now I have a general idea of what the problem is. Thanks a bunch. VB Structure documentation, you say?
I'm sorry I'm being such a drag, but although I AM a programmer, I don't know squat about VB. So I hate to be a bother, but could you give me an example of the replacements you mentioned? I don't want to ask you to do my work for me, just steer a complete newbie in the right direction.
for example:
*PseudoCode 6.0:
function foo badSytax()
{
this.dontDoStuffThisWay
}
*PseudoCode.NET:
function foo good_Syntax(void)
{
this[doItThisWayInstead]
}
larsiusprime 06-27-2004, 02:14 PM Okay, so I've made a little bit of progress, but let me show you what I've done so far. I made the changes with Ints and Shorts and Longs, and found out you need to take any declare statement with "As Any" and do one for Long and one for String, and I know I need to replace "Structure" for "Type"... so here's the updated code: what's still missing....
Thanks a bunch!
'**************************************
' Name: ChangeRes (Fixed)
' Description:This Function will change
' your Windows Resolution. It is very simp
' le, and it does what most Resolution Cha
' nge Functions don't do, it changes the t
' he Bits Per Pixels as well as the Screen
' Width and Height.
' By: ScAnFrEaKisBack
'
' Inputs:Dim RetValue As Short
Dim RetValue As Short = ChangeRes(800, 600, 32)
'
' Returns:1 = Resolution Successfully Ch
' anged 0 = Resolution Was Not Changed
'
' Assumes:If you have any direct questio
' ns or comments please E-Mail Gordon at s
' canfreak@rogers.com
'
' Side Effects:This Code used to have on
' e side effect and is now being fixed as
' I type this...which was the lack of api
' calls and const's and type's.
'
'This code is copyrighted and has' limited warranties.Please see http://w
'ww.Planet-Source-Code.com/vb/scripts/Sho
' wCode.asp?txtCodeId=39535&lngWId=1'for details.'**************************************
'This code goes at the very top of your
' source window or if you want to stick it
' in a module you need to take the "Privat
' e" off of each line.
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByVal lpDevMode As Long) As Boolean
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByVal lpDevMode As String) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByVal lpDevMode As Long, ByVal dwflags As Integer) As Integer
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByVal lpDevMode As String, ByVal dwflags As Integer) As Integer
Private Const CCDEVICENAME = 32
Private Const CCFORMNAME = 32
Private Const DM_BITSPERPEL = &H60000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Structure DEVMODE
dmDeviceName As String * CCDEVICENAME
dmSpecVersion As Short
dmDriverVersion As Short
dmSize As Short
dmDriverExtra As Short
dmFields As Integer
dmOrientation As Short
dmPaperSize As Short
dmPaperLength As Short
dmPaperWidth As Short
dmScale As Short
dmCopies As Short
dmDefaultSource As Short
dmPrintQuality As Short
dmColor As Short
dmDuplex As Short
dmYResolution As Short
dmTTOption As Short
dmCollate As Short
dmFormName As String * CCFORMNAME
dmUnusedPadding As Short
dmBitsPerPel As Short
dmPelsWidth As Integer
dmPelsHeight As Integer
dmDisplayFlags As Integer
dmDisplayFrequency As Integer
End Structure
'This function goes in your source windo
' w or if you want to stick it in a module
' you need to take the "Private" off of ea
' ch line.
Private Function ChangeRes(ByVal Width As Single, ByVal Height As Single, ByVal BPP As Short) As Short
On Error GoTo ERROR_HANDLER
Dim DevM As DEVMODE, I As Short, ReturnVal As Boolean, RetValue, OldWidth As Single, OldHeight As Single, OldBPP As S
Call EnumDisplaySettings(0&, -1, DevM)
OldWidth = DevM.dmPelsWidth
OldHeight = DevM.dmPelsHeight
OldBPP = DevM.dmBitsPerPel
I = 0
Do
ReturnVal = EnumDisplaySettings(0&, I, DevM)
I = I + 1
Loop Until (ReturnVal = False)
DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
DevM.dmPelsWidth = Width
DevM.dmPelsHeight = Height
DevM.dmBitsPerPel = BPP
Call ChangeDisplaySettings(DevM, 1)
RetValue = MsgBox("Do You Wish To Keep Your Screen Resolution To " & Width & "x" & Height & " - " & BPP & " BPP?", vbQuestion + vbOKCancel, "Change Resolution Confirm:")
If RetValue = vbCancel Then
DevM.dmPelsWidth = OldWidth
DevM.dmPelsHeight = OldHeight
DevM.dmBitsPerPel = OldBPP
Call ChangeDisplaySettings(DevM, 1)
MsgBox("Old Resolution(" & OldWidth & " x " & OldHeight & ", " & OldBPP & " Bit) Successfully Restored!", vbInformation + vbOKOnly, "Resolution Confirm:")
ChangeRes = 0
Else
ChangeRes = 1
End If
Exit Function
ERROR_HANDLER:
ChangeRes = 0
End Function
OnErr0r 06-27-2004, 02:32 PM The two As Any change to As DEVMODE. ByVal As Long for that structure would be an alternate declare in which you're passing null instead of a struct. I don't think you were (that was just informational). You haven't given your constants any data type yet.
Structure documentation (http://msdn.microsoft.com/library/en-us/vblr7/html/vastmStructure.asp)
MarshalAs (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondefaultmarshalingforstrings.asp) (do a text search for ByValTStr)
larsiusprime 06-27-2004, 04:39 PM Using what you told me, I finally fixed it!
I ended up using another code snippet instead, though. This other code snippet came in project form so I used the migration tool and then manually changed the as any references. I also had to make a second as any change to overload it to long in one of the other functions.
I'm not sure what "marshalling" is but I was getting errors with the old code snippet and I think the migration tool took care of it for me. Here's the finished, working code in VB.NET: *YAY*
' change resolution Constants, Types and Declares
' Using this option to shutdown windows does not send
' the WM_QUERYENDSESSION and WM_ENDSESSION messages to
' the open applications. Thus, those apps may loose
' any unsaved data.
'
Const EWX_FORCE As Short = 4
Const CCDEVICENAME As Short = 32
Const CCFORMNAME As Short = 32
Const DM_BITSPERPEL As Integer = &H40000
Const DM_PELSWIDTH As Integer = &H80000
Const DM_PELSHEIGHT As Integer = &H100000
Const CDS_UPDATEREGISTRY As Short = &H1s
Const CDS_TEST As Short = &H2s
Const CDS_FULLSCREEN As Short = &H4s
Const DISP_CHANGE_SUCCESSFUL As Short = 0
Const DISP_CHANGE_RESTART As Short = 1
Const HWND_BROADCAST As Integer = &HFFFF
Const WM_DISPLAYCHANGE As Integer = &H7E
Const SPI_SETNONCLIENTMETRICS As Short = 42
Private Structure DEVMODE
<VBFixedString(CCDEVICENAME), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropService s.UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public dmDeviceName As String
Dim dmSpecVersion As Short
Dim dmDriverVersion As Short
Dim dmSize As Short
Dim dmDriverExtra As Short
Dim dmFields As Integer
Dim dmOrientation As Short
Dim dmPaperSize As Short
Dim dmPaperLength As Short
Dim dmPaperWidth As Short
Dim dmScale As Short
Dim dmCopies As Short
Dim dmDefaultSource As Short
Dim dmPrintQuality As Short
Dim dmColor As Short
Dim dmDuplex As Short
Dim dmYResolution As Short
Dim dmTTOption As Short
Dim dmCollate As Short
<VBFixedString(CCFORMNAME), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropService s.UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
Dim dmUnusedPadding As Short
Dim dmBitsPerPel As Short
Dim dmPelsWidth As Integer
Dim dmPelsHeight As Integer
Dim dmDisplayFlags As Integer
Dim dmDisplayFrequency As Integer
End Structure
Private Overloads Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Boolean
'Private Overloads Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As Long) As Boolean
Private Overloads Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef lpDevMode As DEVMODE, ByVal dwFlags As Integer) As Integer
'Private Overloads Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef lpDevMode As Long, ByVal dwFlags As Integer) As Integer
Public Function ChangeRes(ByVal width As Integer, ByVal height As Integer, ByVal BPP As Short) As Integer
Dim DevM As Form1.DEVMODE
Dim lResult As Integer
Dim iAns As Short
lResult = EnumDisplaySettings(0, 0, DevM)
With DevM
.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
.dmPelsWidth = width
.dmPelsHeight = height
.dmBitsPerPel = BPP
End With
lResult = ChangeDisplaySettings(DevM, CDS_FULLSCREEN)
Select Case lResult
Case DISP_CHANGE_SUCCESSFUL
Call ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
Case Else
MsgBox("Mode not supported", MsgBoxStyle.SystemModal, "Error")
End Select
End Function
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
ChangeRes(640, 480, 32)
End Sub
End Class
OnErr0r 06-27-2004, 08:45 PM The constants really should all be Integer. I'm glad you got it working.
larsiusprime 06-28-2004, 06:19 PM The constants really should all be Integer. I'm glad you got it working.
I'll get on that.
|