在VB.NET中改變顯示器的分辯率

來源:互聯網
上載者:User
顯示 在VB.NET中,我們很容易獲得顯示器的分辯率,但是,要改變顯示器的分辯率就比較麻煩了。由於.NET的類庫沒有將EnumDisplaySettings 和ChangeDisplaySettings這兩個API函數進行封裝,但是我們得調用它們函數,相對於VB6來說,VB.NET調用API函數是有一些小的改動!
下面,我們就嘗試一下在VB.NET中,使用這兩個API函數。
建立一個項目,在Form1上添加兩個按鈕,一個名為btnGetDisp,將其Text屬性設定為“得到分辯率”;另一個按鈕名為btnSetDisp,Text屬性為“設定分辯率”。然後在代碼視窗裡添加以下代碼:

Private Const CCDEVICENAME As Short = 32
Private Const CCFORMNAME As Short = 32
Private Const DM_PELSWIDTH As Integer = &H80000
Private Const DM_PELSHEIGHT As Integer = &H100000

'重新整理頻率常量
Private Const DM_DISPLAYFREQUENCY As Integer = &H400000

'調用API函數
Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA"
(ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Boolean
'調用API函數
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA"
(ByRef lpDevMode As DEVMODE, ByVal dwflags As Integer) As Integer

'定義結構
Private Structure DEVMODE
<VBFixedString(CCDEVICENAME), System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=CCDEVICENAME)> Public 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
<VBFixedString(CCFORMNAME), System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=CCFORMNAME)> Public 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 Sub ChangeDisp(ByRef iWidth As Single, ByRef iHeight As Single)
Dim blnWorked As Boolean
Dim i As Integer
Dim DevM As Form1.DEVMODE
i = 0
Do
blnWorked = EnumDisplaySettings(0, i, DevM)
i = i + 1
Loop Until (blnWorked = False)

With DevM
.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_DISPLAYFREQUENCY
.dmPelsWidth = iWidth
.dmPelsHeight = iHeight
'重新整理頻率為85
.dmDisplayFrequency = 85
End With
Call ChangeDisplaySettings(DevM, 0)
End Sub

Private Sub btnGetDisp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDisp.Click
Dim X As Short = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
Dim Y As Short = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
MsgBox("您的顯示器解析度是" & X & " X " & Y)
End Sub

Private Sub btnSetDisp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetDisp.Click
If MsgBox("您確認要將顯示器解析度改為1024x768嗎?", MsgBoxStyle.OKCancel, "系統訊息") = MsgBoxResult.OK Then
'調用改變分辯率過程
ChangeDisp(1024, 768)
End If
End Sub

程式運行如下圖所示,點擊設定分辯率,將會把顯示器解析度改為1024x768,是不是很簡單?

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。