2013-04-09 149 views
3

當我的應用程序加載時系統屏幕分辨率需要更改爲1024 * 768。當其關閉時,它會更改爲原始屏幕解決方案。如何以編程方式更改屏幕分辨率

如何實現這個?有沒有可行的解決方案?基本上我正在使用vb.net,無論如何我歡迎來自C#的解決方案。

+0

http://stackoverflow.com/questions/215412/programmatically-change-screen-resolution – paul 2013-04-09 09:12:32

+1

http://www.codeproject.com/Articles/6810/Dynamic-Screen-Resolution – 2013-04-09 09:13:45

+0

爲什麼你想這樣做?有很多關於目標硬件的假設看起來非常可怕(例如,不是多顯示器,不是縱向顯示,在打開應用程序時不打開alt-tab,而是寬屏顯示器,不使用大字體) – 2013-04-09 09:14:41

回答

2

更改分辨率更困難。

這CodeProject上介紹瞭如何做到這一點在C# http://www.codeproject.com/KB/dotnet/changing-display-settings.aspx,你可以使用這個

以下網站幫你翻譯的C#代碼到VB http://www.developerfusion.com/tools/convert/csharp-to-vb/

編輯

這裏是樣品

Public Class Form1 

    Private Resolution As New ResolutionChanger 
    Private OldWidth As UInteger 
    Private OldHeight As UInteger 

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    Resolution.SetResolution(OldWidth, OldHeight) 
    End Sub 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    OldHeight = CUInt(Screen.PrimaryScreen.Bounds.Height) 
    OldWidth = CUInt(Screen.PrimaryScreen.Bounds.Width) 
    Select Case Resolution.SetResolution(800, 600) 
     Case ResolutionChanger.ChangeResult.Success 
     MsgBox("The Resolution was changed", MsgBoxStyle.OkOnly) 
     Case ResolutionChanger.ChangeResult.Restart 
     MsgBox("Restart your system to activate the new resolution setting", MsgBoxStyle.OkOnly) 
     Case ResolutionChanger.ChangeResult.Fail 
     MsgBox("The resolution couldn't be changed", MsgBoxStyle.OkOnly) 
     Case ResolutionChanger.ChangeResult.ResolutionNotSupported 
     MsgBox("The requested resolution is not supported by your system", MsgBoxStyle.OkOnly) 
    End Select 
    End Sub 

End Class 
' 
'========== RESOLUTION CHANGER =============================== 
' 
' 
Class ResolutionChanger 
    Public Enum ChangeResult 
    Success 
    Restart 
    Fail 
    ResolutionNotSupported 
    End Enum 

    Public Function SetResolution(ByVal Width As UInteger, ByVal Height As UInteger) As ChangeResult 
    Dim DevMode As New DEVMODEA 
    If User_32.EnumDisplaySettingsA(Screen.PrimaryScreen.DeviceName, ENUM_CURRENT_SETTINGS, DevMode) Then 
     DevMode.dmPelsWidth = Width 
     DevMode.dmPelsHeight = Height 
     Dim ReturnValue = User_32.ChangeDisplaySettingsA(DevMode, CDS_TEST) 
     If ReturnValue = DISP_CHANGE_FAILED Then 
     'The Requested resolution is not supported by the system 
     Return ChangeResult.ResolutionNotSupported 
     Else 
     ReturnValue = User_32.ChangeDisplaySettingsA(DevMode, CDS_UPDATEREGISTRY) 
     Select Case ReturnValue 
      Case DISP_CHANGE_RESTART 
      'The resolution cannot be change dynamically on every system 
      'Windows 9x and some Laptop (XP,Vista,Windows7) have to reboot. 
      Return ChangeResult.Restart 
      Case DISP_CHANGE_SUCCESSFUL 
      'Resolution was changed 
      'This is not an assurance that the new resolution will render 
      'proprely on every system. It only means that the registery was 
      'updated succesfuly and that the driver have not return any 
      'error 
      Return ChangeResult.Success 
      Case Else 
      'An error has caused the resolution not to be changed 
      Return ChangeResult.Fail 
     End Select 
     End If 
    End If 
    End Function 
    ' 
    '============Region Interop ============================================== 
    ' 
    ' 
    Private Const ENUM_CURRENT_SETTINGS As Integer = -1 
    Private Const CDS_UPDATEREGISTRY As Integer = 1 
    Private Const CDS_TEST As Integer = 2 
    Private Const DISP_CHANGE_SUCCESSFUL As Integer = 0 
    Private Const DISP_CHANGE_RESTART As Integer = 1 
    Private Const DISP_CHANGE_FAILED As Integer = -1 

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _ 
    Public Structure DEVMODEA 
    'BYTE[32] 
    <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=32, ArraySubType:=System.Runtime.InteropServices.UnmanagedType.I1)> _ 
    Public dmDeviceName() As Byte 
    Public dmSpecVersion As UShort 
    Public dmDriverVersion As UShort 
    Public dmSize As UShort 
    Public dmDriverExtra As UShort 
    Public dmFields As UInteger 
    Public Union1 As Anonymous_2338c0fc_03a3_4514_b536_fb9bb5df14c5 
    Public dmColor As Short 
    Public dmDuplex As Short 
    Public dmYResolution As Short 
    Public dmTTOption As Short 
    Public dmCollate As Short 
    <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=32, ArraySubType:=System.Runtime.InteropServices.UnmanagedType.I1)> _ 
    Public dmFormName() As Byte 
    Public dmLogPixels As UShort 
    Public dmBitsPerPel As UInteger 
    Public dmPelsWidth As UInteger 
    Public dmPelsHeight As UInteger 
    Public Union2 As Anonymous_7557e508_845c_4777_b9f2_a1496c1c7b21 
    Public dmDisplayFrequency As UInteger 
    Public dmICMMethod As UInteger 
    Public dmICMIntent As UInteger 
    Public dmMediaType As UInteger 
    Public dmDitherType As UInteger 
    Public dmReserved1 As UInteger 
    Public dmReserved2 As UInteger 
    Public dmPanningWidth As UInteger 
    Public dmPanningHeight As UInteger 
    End Structure 

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)> _ 
    Public Structure Anonymous_2338c0fc_03a3_4514_b536_fb9bb5df14c5 
    <System.Runtime.InteropServices.FieldOffsetAttribute(0)> _ 
    Public Struct1 As Anonymous_a67d541d_da92_408e_8852_89977e56cead 
    <System.Runtime.InteropServices.FieldOffsetAttribute(0)> _ 
    Public Struct2 As Anonymous_d973d7e7_ad4c_4155_86fe_6d2b51ab5f04 
    End Structure 

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Explicit)> _ 
    Public Structure Anonymous_7557e508_845c_4777_b9f2_a1496c1c7b21 
    <System.Runtime.InteropServices.FieldOffsetAttribute(0)> _ 
    Public dmDisplayFlags As UInteger 
    <System.Runtime.InteropServices.FieldOffsetAttribute(0)> _ 
    Public dmNup As UInteger 
    End Structure 

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _ 
    Public Structure Anonymous_a67d541d_da92_408e_8852_89977e56cead 
    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 
    End Structure 

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _ 
    Public Structure Anonymous_d973d7e7_ad4c_4155_86fe_6d2b51ab5f04 
    Public dmPosition As POINTL 
    Public dmDisplayOrientation As UInteger 
    Public dmDisplayFixedOutput As UInteger 
    End Structure 

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _ 
    Public Structure POINTL 
    Public x As Integer 
    Public y As Integer 
    End Structure 

    Partial Public Class User_32 
    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="EnumDisplaySettingsA")> _ 
    Public Shared Function EnumDisplaySettingsA(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)> ByVal lpszDeviceName As String, ByVal iModeNum As Integer, <System.Runtime.InteropServices.OutAttribute()> ByRef lpDevMode As DEVMODEA) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean 
    End Function 

    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="ChangeDisplaySettingsA")> _ 
    Public Shared Function ChangeDisplaySettingsA(<System.Runtime.InteropServices.OutAttribute()> ByRef lpDevMode As DEVMODEA, ByVal dwFlags As UInteger) As Integer 
    End Function 
    End Class 

End Class 
+0

我試試這個代碼,但它不在除外。這意味着我將改變解決方案並運行應用程序,但我的應用程序不會受到影響 – Sathish 2013-04-09 09:29:52

1

雖然我討厭一個桌面程序改變我的決議,並立即折騰出來

但編碼你可以使用這樣的事情

這個API功能可讓您更改顯示設置:

 [DllImport("user32.dll")] 
     static extern int ChangeDisplaySettings(
     ref DEVMODE devMode, int flags); 

This文章解釋瞭如何使用這個功能