none
动态改变及恢复屏幕设置 RRS feed

  • 常规讨论

  • Const CCHDEVICENAME = 32
    Const CCHFORMNAME = 32

    Private Type DEVMODE
    dmDeviceName As String * CCHDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCHFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
    End Type  

      ------------------设置屏幕的核心API-----------------

    Private Declare Function ChangeDisplaySettings
    Lib "User32" Alias "ChangeDisplaySettingsA"
    (ByVal lpDevMode As Long, ByVal dwflags As Long) As Long

      ------------------设置屏幕的函数-----------------

    Public Function SetDispMode(Width As Integer,
    Height As Integer, Color As Integer) As Long

      (SetDispMode是自己构造的更改屏幕设置的函数来,它的三个参数Width、Height和Color分别是屏幕的横向分辨率、纵向分辨率,颜色位数,其值可为24,16,0等。0为原有颜色设置。)

    Const DM_PELSWIDTH = &H80000
    Const DM_PELSHEIGHT = &H100000
    Const DM_BITSPERPEL = &H40000
    Dim NewDevMode As DEVMODE
    Dim pDevmode As Long
    With NewDevMode
    .dmSize = 122
    If Color = 0 Then

      如果Color=0则只改变屏幕的分辨率,而不改变色彩。



    .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
    Else  

      如果Color不等0则改变屏幕的分辨率和色彩。

    .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
    End If
    .dmPelsWidth = Width
    .dmPelsHeight = Height
    If Color < > 0 Then
    .dmBitsPerPel = Color
    End If
    End With
    pDevmode = lstrcpy(NewDevMode, NewDevMode)

      得到一个指向NewDevMode结构的Long型的指针。



    ChangeDisplaySettings pDevmode, 0
    End Function  

      三、在工程窗体中,加入两个按钮Command1和Command2,其Caption属性分别为“800x600x16”和“恢复原设置”。

      其程序代码为:

      窗口的“通用|声明”区



    Option Explicit
    Dim H, V, Color As Long  

      声名变量,用于保存最初屏幕设置
      Private Sub Form_Load()
      ---------------以下代码用于得到最初的屏幕设备--------------



    H = GetDeviceCaps(Form1.hdc, HORZRES)
    V = GetDeviceCaps(Form1.hdc, VHORZRES)
    Color = GetDeviceCaps(Form1.hdc, BITSPIXEL)
    End Sub

    Private Sub Command1_Click()  

      调用SetDispMode函数改变屏幕设置



    SetDispMode 800, 600, 16
    End Sub

    Private Sub Command2_Click()  

      恢复最初屏幕设置

    SetDispMode Cint(H), Cint(V), Cint(Color)
    End Sub

      四、将程序编译执行。

      本程序执行后,如果单击Command1,则您的计算机屏幕显示模式将被设置为“800x600x16”的显示模式;如果单击Command2, 则您的计算机屏幕显示模式将被设置为原来的显示模式。此程序稍加修改,即可放置于桌面或任务栏中,直接快捷的修改屏幕设置。
    2009年5月27日 8:27