C# 如何以编程方式更改屏幕分辨率

C# 如何以编程方式更改屏幕分辨率,c#,vb.net,screen-resolution,C#,Vb.net,Screen Resolution,加载应用程序时,系统屏幕分辨率需要更改为1024*768。当其关闭时,它将更改为原始屏幕解决方案 如何做到这一点?。有没有可行的解决办法?基本上,我是在使用vb.net,无论如何,我欢迎C#的解决方案。更改分辨率更困难 这个代码项目描述了如何在C#中实现它 你可以用这个 下面的网站将帮助您将C#代码翻译成VB 编辑 这是样品 Public Class Form1 Private Resolution As New ResolutionChanger Private OldWidth A

加载应用程序时,系统屏幕分辨率需要更改为1024*768。当其关闭时,它将更改为原始屏幕解决方案


如何做到这一点?。有没有可行的解决办法?基本上,我是在使用vb.net,无论如何,我欢迎C#的解决方案。

更改分辨率更困难

这个代码项目描述了如何在C#中实现它 你可以用这个

下面的网站将帮助您将C#代码翻译成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
公共类表单1
作为新决议转换器的私人决议
专用OldWidth作为UInteger
私家侦探
私有子表单1_FormClosing(ByVal sender作为对象,ByVal e作为System.Windows.Forms.FormClosingEventArgs)处理Me.FormClosing
分辨率。设置分辨率(OldWidth、OldHeight)
端接头
私有子表单1_Load(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
OldHeight=CUInt(Screen.PrimaryScreen.Bounds.Height)
OldWidth=CUInt(Screen.PrimaryScreen.Bounds.Width)
选择案例分辨率。设置分辨率(800600)
案例解决转换器.ChangeResult.Success
MsgBox(“分辨率已更改”,MsgBoxStyle.OkOnly)
案例解析转换器.ChangeResult.Restart
MsgBox(“重新启动系统以激活新的分辨率设置”,MsgBoxStyle.OkOnly)
案例解析转换器.ChangeResult.Fail
MsgBox(“无法更改分辨率”,MsgBoxStyle.OkOnly)
案例解决方案转换器.ChangeResult.ResolutionNotSupported
MsgBox(“您的系统不支持请求的分辨率”,MsgBoxStyle.OkOnly)
结束选择
端接头
末级
'
'=============分辨率更改器===============================
'
'
类解析转换器
公共枚举更改结果
成功
重新启动
失败
决议不受支持
结束枚举
公共函数设置分辨率(ByVal宽度作为UInteger,ByVal高度作为UInteger)作为ChangeResult
将DevMode设置为新DEVMODEA
如果用户_32.EnumDisplaySettingsA(Screen.PrimaryScreen.DeviceName、ENUM_当前_设置、DevMode),则
DevMode.dmPelsWidth=宽度
DevMode.dmPelsHeight=高度
Dim ReturnValue=User_32.更改显示设置A(DevMode,CDS_测试)
如果ReturnValue=DISP\u CHANGE\u失败,则
'系统不支持请求的解决方案
返回ChangeResult.ResolutionNotSupported
其他的
ReturnValue=User_32.ChangeDisplaySettingsA(DevMode,CDS_UPDATEREGISTRY)
选择Case ReturnValue
案例显示更改重新启动
'不能在每个系统上动态更改分辨率
“Windows 9x和一些笔记本电脑(XP、Vista、Windows7)必须重新启动。
返回ChangeResult。重新启动
案例展示变更成功
"决议改变了
"这并不保证新的决议会带来,
"要依靠各种制度,。这只意味着登记处是
'已成功更新,且驱动程序未返回任何
“错误
返回changesult.Success
其他情况
'一个错误导致无法更改分辨率
返回ChangeResult。失败
结束选择
如果结束
如果结束
端函数
'
'=============区域互操作==============================================
'
'
私有常量枚举当前设置为整数=-1
Private Const CDS_UPDATEREGISTRY As Integer=1
私有常量CDS_测试为整数=2
Private Const DISP\u CHANGE\u作为整数成功=0
私有常量显示更改重新启动为整数=1
Private Const DISP\u CHANGE\u因整数=-1而失败
_
公共结构模式
'字节[32]
_
公共dmDeviceName()作为字节
作为UShort的公共版本
公共dmDriverVersion作为UShort
公共dmSize为UShort
公共dmDriverExtra As UShort
作为UInteger的公共dmFields
公共工会1匿名_2338c0fc_03a3_4514_b536_fb9bb5df14c5
公共颜色为短
公共数字媒体作为短媒体
公共数据解析为简短
公共DMT选项简称
公共数据管理系统将其整理为短数据
_
公共dmFormName()作为字节
公共dmLogPixels作为UShort
公共dmBitsPerPel作为UInteger
公共dmPelsWidth As UInteger
公共DMPELSHEGHT As UInteger
公共工会2,作为匿名的_7557e508 _845c _4777 _b9f2 _a1496c1c7b21
公共dmDisplayFrequency作为UInteger
作为UInteger的公共管理方法
作为UInteger的公共管理
作为UInteger的公共dmMediaType
作为UInteger的公共dmDitherType
公共dmReserved1作为UInteger
公共dmReserved2作为UInteger
公共dmPanningWidth作为UInteger
公共dmPanningHeight As UInteger
端部结构
_
公共结构匿名_2338c0fc_03a3_4514_b536_fb9bb5df14c5
_
作为匿名的公共结构1_a67d541d_da92_408e_8852_89977e56cead
_
作为匿名的公共结构2_d973d7e7_ad4c_4155_86fe_6d2b51ab5f04
端部结构
_
公共结构匿名_7557e508_845c_4777_b9f2_a1496c1c7b21
_
公共dmDisplayFlags作为UInteger
_
作为UInteger的公共dmNup
端部结构
_
公共结构匿名协会_a67d541d_da92_408e_8852_89977e56cead
公众导向
公共纸张尺寸为短
公共文件长度为短
公共宽度为短
公营房屋
公共数字媒体副本简称
公共dmDefaultSource为短源
公营房屋质素
端部结构
_
公共结构匿名组织\u d973d7e7\u ad4c\u 4155\u 86fe\u 6d2b51ab5f04
公共位置作为点
作为UInteger的公共DMD
公共dmDisplayFixedOutput作为UInteger
端部结构
_
公共结构点
公共x作为整数
公共y作为整数
端部结构
部分公共类用户\u 32
_
公共共享函数EnumDisplaySettingsA(ByVal lpszDeviceName作为字符串,ByVal iMod
        [DllImport("user32.dll")]
        static extern int ChangeDisplaySettings(
        ref DEVMODE devMode, int flags);