剖析講解VB.NET分辯率案例
使用VB.NET開發(fā)的程序員知道很容易獲得顯示器的分辯率,但是,要改變VB.NET分辯率是一件很讓人頭疼的事。由于VB.NET的類庫(kù)沒有將enumdisplaysettings 和ChangeDisplaySettings這兩個(gè)API函數(shù)進(jìn)行封裝,但是我們得調(diào)用它們函數(shù),相對(duì)于VB6來說,VB.NET調(diào)用API函數(shù)是有一些小的改動(dòng)!
下面,我們就嘗試一下在VB.NET分辯率調(diào)試,使用這兩個(gè)api函數(shù)。
新建一個(gè)項(xiàng)目,在form1上添加兩個(gè)按鈕,一個(gè)名為btngetdisp,將其text屬性設(shè)置為“得到分辯率”;另一個(gè)按鈕名為btnsetdisp,text屬性為“設(shè)置分辯率”。然后在代碼窗口里添加以下代碼:
- 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
- '調(diào)用API函數(shù)
- private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Integer, ByVal iModeNum As Integer, ByRef lpDevMode As DEVMODE) As Boolean
- '調(diào)用api函數(shù)
- private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (ByRef lpDevMode As DEVMODE, ByVal dwflags As Integer) As Integer
- '定義結(jié)構(gòu)
- 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
- '改變分辯率過程,參數(shù)一寬度,參數(shù)二高度
- 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)
- ii = 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("您確認(rèn)要將顯示器分辨率改為1024x768嗎?", MsgBoxStyle.OKCancel, "系統(tǒng)消息") = MsgBoxResult.OK Then
- '調(diào)用改變分辯率過程
- changedisp(1024, 768)
- end If
- end Sub
程序運(yùn)行所示,點(diǎn)擊設(shè)置VB.NET分辯率,將會(huì)把顯示器分辨率改為1024x768,刷新頻率為85,是不是很簡(jiǎn)單?
【編輯推薦】