Changing the screen resolution in VB.NET

Posted on 2006-03-25
Last Modified: 2008-02-01
There was a post asked by MOsosky on 07/23/2004 01:08AM EST about wanting to know how to change the screen resolution in VB.NET.  I copied and pasted the code from the orginal comment here.  

ChangeRes(1024, 768, 32) 'i added this line in the form load and i changed the resolution accordingly - it worked.  I tested the code on other machines and the form starts in different parts of the screen. I want to know how to make sure the form starts in the  same part of the screen.

Imports System.Runtime.InteropServices

    Private Const CCDEVICENAME = 32
    Private Const CCFORMNAME = 32
    Private Const DM_PELSWIDTH = &H80000
    Private Const DM_PELSHEIGHT = &H100000

       <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=4)> _
    Private Structure DEVMODE
        <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=CCDEVICENAME)> Dim 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
        <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=CCFORMNAME)> Dim 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 Declare Ansi Function EnumDisplaySettings Lib "user32" _
        Alias "EnumDisplaySettingsA" _
        (ByVal lpszDeviceName As Integer, _
        ByVal iModeNum As Integer, _
        ByRef lpDevMode As DEVMODE) As Boolean

    Private Declare Ansi Function ChangeDisplaySettings Lib "user32" _
        Alias "ChangeDisplaySettingsA" _
        (ByRef lpDevMode As DEVMODE, _
        ByVal dwflags As Integer) As Integer

    Private Sub ChangeRes(ByVal iWidth As Integer, ByVal iHeight As Integer, Optional ByVal iColorDepth As Integer = 0)
        Dim blnWorked As Boolean
        Dim i As Integer
        Dim DevM As DEVMODE

        i = 0

            blnWorked = EnumDisplaySettings(0, i, DevM)
            If blnWorked Then
                If DevM.dmPelsWidth = iWidth And DevM.dmPelsHeight = iHeight Then
                    If iColorDepth > 0 Then
                        DevM.dmBitsPerPel = iColorDepth
                    End If
                    ChangeDisplaySettings(DevM, 0)
                    Exit Sub
                End If
            End If
            i = i + 1
        Loop Until (blnWorked = False)

    End Sub
Question by:gbilios
    LVL 41

    Accepted Solution

    The starting position of the form is controlled by the "StartPostion" property...

    If you change the property to "Manual", and se the Location property to 10, 10 then it will always appear in the same spot (10 pixels down, and 10 pixels from the left)

    Author Comment

    you mean like this:

    Dim instance As New Form

            Dim value As FormStartPosition

            value = instance.StartPosition.CenterParent
            instance.StartPosition = value


     Me.Location = New Point(200, 200)
    LVL 41

    Expert Comment

    Either will do it...   Most folks adjust the properties of the form form the GUI (the lower right-hand corner of the IDE), rather than with code

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    character entities and entity references in XML 17 79
    fix34  challenge 9 70
    maxMirror challenge 10 68
    wefewf 2 28
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now