Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Changing the screen resolution in VB.NET

Posted on 2006-03-25
3
Medium Priority
?
391 Views
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

        Do
            blnWorked = EnumDisplaySettings(0, i, DevM)
            If blnWorked Then
                If DevM.dmPelsWidth = iWidth And DevM.dmPelsHeight = iHeight Then
                    Debug.WriteLine(DevM.dmBitsPerPel)
                    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
0
Comment
Question by:gbilios
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 16290571
The starting position of the form is controlled by the "StartPostion" property...

http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx

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)
0
 

Author Comment

by:gbilios
ID: 16291933
you mean like this:

Dim instance As New Form

        Dim value As FormStartPosition

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


OR

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

Expert Comment

by:graye
ID: 16294664
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

572 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