Interoperation / Office Integration Topics include: COM, P/Invoke (e.g. Win32 API), marshaling, distributed applications, VBA and interacting with office apps

Go Back  Xtreme .NET Talk > .NET > Interoperation / Office Integration > VB.NET --> Screen Resolution Change Example Complete!


Reply
 
Thread Tools Display Modes
  #1  
Old 12-02-2003, 12:51 AM
joshuaand joshuaand is offline
Freshman

Preferred language:
VB.NET
 
Join Date: Sep 2003
Location: Australia
Posts: 45
joshuaand is on a distinguished road
Default VB.NET --> Screen Resolution Change Example Complete!



Hi,

Not sure how useful this is to anyone but I had alot of trouble finding a VB.NET Screen Resolution Change functionality, so I managed to get it working, and the code is below.

Can be called by: ChangeRes(1024,768)

Any comments are welcome, as are bug fixes!!!!


Code:
Imports System.Runtime.InteropServices

Public Module resChanger

    Const ENUM_CURRENT_SETTINGS As Integer = -1
    Const CDS_UPDATEREGISTRY As Integer = &H1
    Const CDS_TEST As Long = &H2

    Const CCDEVICENAME As Integer = 32
    Const CCFORMNAME As Integer = 32

    Const DISP_CHANGE_SUCCESSFUL As Integer = 0
    Const DISP_CHANGE_RESTART As Integer = 1
    Const DISP_CHANGE_FAILED As Integer = -1

    Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Integer
    Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Integer) As Integer

    <StructLayout(LayoutKind.Sequential)> Public Structure DEVMODE
        <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public dmDeviceName As String
        Public dmSpecVersion As Short
        Public dmDriverVersion As Short
        Public dmSize As Short
        Public dmDriverExtra As Short
        Public dmFields As Integer
        Public dmOrientation As Short
        Public dmPaperSize As Short
        Public dmPaperLength As Short
        Public dmPaperWidth As Short
        Public dmScale As Short
        Public dmCopies As Short
        Public dmDefaultSource As Short
        Public dmPrintQuality As Short
        Public dmColor As Short
        Public dmDuplex As Short
        Public dmYResolution As Short
        Public dmTTOption As Short
        Public dmCollate As Short
        <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public dmFormName As String
        Public dmUnusedPadding As Short
        Public dmBitsPerPel As Short
        Public dmPelsWidth As Integer
        Public dmPelsHeight As Integer
        Public dmDisplayFlags As Integer
        Public dmDisplayFrequency As Integer
    End Structure

    Public Sub changeRes(ByVal theWidth As Integer, ByVal theHeight As Integer)

        Dim DevM As DEVMODE

        DevM.dmDeviceName = New [String](New Char(32) {})
        DevM.dmFormName = New [String](New Char(32) {})
        DevM.dmSize = CShort(Marshal.SizeOf(GetType(DEVMODE)))


        If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
            Dim lResult As Integer

            DevM.dmPelsWidth = theWidth
            DevM.dmPelsHeight = theHeight
            DevM.dmPelsWidth = 1280
            DevM.dmPelsHeight = 1024

            lResult = ChangeDisplaySettings(DevM, CDS_TEST)

            If lResult = DISP_CHANGE_FAILED Then
                MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
            Else

                lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)

                Select Case lResult
                    Case DISP_CHANGE_RESTART
                        MsgBox("You must restart your computer to apply these changes.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Has Changed")
                    Case DISP_CHANGE_SUCCESSFUL
                        MsgBox("Display Change Successful.", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Screen Resolution Successful")
                    Case Else
                        MsgBox("Display Change Failed.", MsgBoxStyle.OKOnly + MsgBoxStyle.Critical, "Screen Resolution Change Failed")
                End Select
            End If


        End If
    End Sub

End Module

Last edited by snarfblam; 02-16-2011 at 04:32 PM. Reason: Corrected API declaration
Reply With Quote
  #2  
Old 12-02-2003, 01:04 AM
Mehyar's Avatar
Mehyar Mehyar is offline
Junior Contributor

Preferred language:
VB.NET
 
Join Date: Jun 2003
Location: Lebanon
Posts: 371
Mehyar is on a distinguished road
Default

But i guess you need permissions to execute that on the client computer. (Not every user has the right to change his/her screen resolution) So you have to be careful using it in your application.
__________________
Dream as if you'll live forever, live as if you'll die today
Reply With Quote
  #3  
Old 12-02-2003, 01:09 AM
joshuaand joshuaand is offline
Freshman

Preferred language:
VB.NET
 
Join Date: Sep 2003
Location: Australia
Posts: 45
joshuaand is on a distinguished road
Default

I havent tried it but I am assuming it will just fail to update the res, and should be handled correctly?

not sure though.
Reply With Quote
  #4  
Old 12-02-2003, 01:13 AM
Mehyar's Avatar
Mehyar Mehyar is offline
Junior Contributor

Preferred language:
VB.NET
 
Join Date: Jun 2003
Location: Lebanon
Posts: 371
Mehyar is on a distinguished road
Default

I am not sure if it will handle it, because once you try to change the resoltuion through code and the user running your application has no permission to do so then it will through a security exception, This exception is not caught any where in your code. So i guess you should use some error handling (Try .. Catch)
__________________
Dream as if you'll live forever, live as if you'll die today
Reply With Quote
  #5  
Old 12-02-2003, 01:17 AM
joshuaand joshuaand is offline
Freshman

Preferred language:
VB.NET
 
Join Date: Sep 2003
Location: Australia
Posts: 45
joshuaand is on a distinguished road
Default

For my application everyone will have priveleges to change screen res so its not an issue for me, anyone else who uses it may want to take that into account though.
Reply With Quote
  #6  
Old 01-24-2008, 11:44 AM
chandru08 chandru08 is offline
Newcomer
 
Join Date: Jan 2008
Posts: 1
chandru08 is on a distinguished road
Default Re: VB.NET --> Screen Resolution Change Example Complete!

i tryed with your code,

i im getting the error when i reach the line

lResult = ChangeDisplaySettings(DevM, CDS_TEST)

Error i got is...

PInvokeStackImbalance was detected

A call to PInvoke function 'Resolution!Resolution.resChanger::ChangeDisplaySettings' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.


can u give the solution...
Reply With Quote
  #7  
Old 01-30-2008, 05:22 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default Re: VB.NET --> Screen Resolution Change Example Complete!

Code:
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Long) As Integer
should be
Code:
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef DEVMODE As DEVMODE, ByVal flags As Integer) As Integer
__________________
e
Reply With Quote
  #8  
Old 08-02-2010, 11:50 AM
ikebotch ikebotch is offline
Newcomer
 
Join Date: Aug 2010
Posts: 1
ikebotch is on a distinguished road
Default Re: VB.NET --> Screen Resolution Change Example Complete!

Works perfectly well but just check line 62 and 63 and set line 60 and 61 to the width and the height

ie

DevM.dmPelsWidth = theWidth
DevM.dmPelsHeight = theHeight
' DevM.dmPelsWidth = 1024
' DevM.dmPelsHeight = 768
Reply With Quote
  #9  
Old 02-16-2011, 09:05 AM
ScottN ScottN is offline
Newcomer
 
Join Date: Feb 2011
Posts: 1
ScottN is on a distinguished road
Default Re: VB.NET --> Screen Resolution Change Example Complete!

Hi, thanks for the sample code.

- Can this be adapted to support multiple displays?
- How about for activating/deactivating displays?
- What if a user has a TV out from their video card, how to detect and support changing supported resolutions on it?

Thanks
Reply With Quote
  #10  
Old 02-16-2011, 04:37 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default Re: VB.NET --> Screen Resolution Change Example Complete!

Hi ScottN. It's generally best to put a new question in its own thread, with a link to a related thread if appropriate.

Quote:
Originally Posted by ScottN View Post
- Can this be adapted to support multiple displays?
The Screen class can get info about mulitple displays. I don't know how to change settings for multiple displays.

Quote:
Originally Posted by ScottN View Post
- How about for activating/deactivating displays?
- What if a user has a TV out from their video card, how to detect and support changing supported resolutions on it?
I would expect the TV out to behave as a normal display. As for detecting changes and activating/deactivating, again, I don't know.
__________________
e
Reply With Quote
  #11  
Old 05-05-2011, 09:31 AM
ken thompson ken thompson is offline
Newcomer
 
Join Date: May 2011
Posts: 1
ken thompson is on a distinguished road
Default Re: VB.NET --> Screen Resolution Change Example Complete!

Did you mean to ignore the parameters and force one setting?:

If 0 <> EnumDisplaySettings(Nothing, ENUM_CURRENT_SETTINGS, DevM) Then
Dim lResult As Integer

DevM.dmPelsWidth = theWidth
DevM.dmPelsHeight = theHeight
DevM.dmPelsWidth = 1280
DevM.dmPelsHeight = 1024
Reply With Quote
  #12  
Old 07-06-2011, 07:29 AM
andrew15 andrew15 is offline
Newcomer
 
Join Date: Jul 2011
Posts: 1
andrew15 is on a distinguished road
Default Re: VB.NET --> Screen Resolution Change Example Complete!

Agree with you.... also have same thought.

Quote:
Originally Posted by Mehyar View Post
But i guess you need permissions to execute that on the client computer. (Not every user Get GED Online has the right to change his/her screen resolution) So you have to be careful using it in your application.

Last edited by andrew15; 07-07-2011 at 07:19 AM.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing screen resolution in .NET headkaze Graphics and Multimedia 2 09-28-2006 11:10 AM
How do i get the Screen Resolution? LDV Graphics and Multimedia 2 05-18-2004 07:06 AM
Change screen resolution with vb. net? AlexMesquita Windows Forms 2 01-11-2004 11:17 PM
screen resolution change cartque Graphics and Multimedia 1 07-02-2003 09:07 AM
api screen resolution change malooga General 1 02-13-2003 05:58 PM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe