Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can use following codes to change resolution but problem is that when I run my program it changes resolution(1280*1024) & show form which doesnt fit the screen though its width is 1280 & height is 1024. But when I close that program & run it again then it works I mean fit with the screen. It means first time when I run my program it changes resolution but cant fit form with screen. So can you tell me how should I do three things together like when I run my program it changes resolution & fit my form with screen & when I close it it will get back its previous resolution. Please help me.

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 Long) 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)
          lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
          End If
          End Sub
          End Module

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call changeRes(1280, 1024)
    End Sub
End Class
Posted
Updated 12-Feb-14 4:09am
v3
Comments
DaveAuld 19-Jul-10 7:40am    
Not good to change system resolution to the suit the application, if your app is permantently the foreground app, then change the system resolution and leave it......If i was an end user and an app changed the system i would not be happy, what happens when you task switch.....it screws everything else up.

For you to do this is still nasty as hell and a warning to all your users that you're not a very good programmer. Set up your form so it lays itself out on any resolution instead.
 
Share this answer
 
Delete these two rows.....This is very simple.................



VB
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)
          lResult = ChangeDisplaySettings(DevM, CDS_UPDATEREGISTRY)
          End If
          End Sub
          End Module
 
Share this answer
 
Comments
Dave Kreskowiak 12-Feb-14 10:08am    
First, changing the screen resolution just to run a single app is not a good idea.

Second, you're responding to a 4 YERAR OLD post! Don't.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900