您可以用下面給出這一小段代碼檢測當(dāng)前屏幕分辨率,然后根據(jù)結(jié)果作出反應(yīng)──例如,重新調(diào)整窗體大小以適應(yīng)用程序戶分辨率。
創(chuàng)新互聯(lián)公司是一家專業(yè)提供犍為企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、網(wǎng)站制作、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為犍為眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
Public Function CheckRez(pixelWidth As Long, pixelHeight As Long) As Boolean
'
Dim lngTwipsX As Long
Dim lngTwipsY As Long
'
' convert pixels to twips
lngTwipsX = pixelWidth * 15
lngTwipsY = pixelHeight * 15
'
' check against current settings
If lngTwipsX Screen.Width Then
CheckRez = False
Else
If lngTwipsY Screen.Height Then
CheckRez = False
Else
CheckRez = True
End If
End If
'
End Function
Next, run the following code at the start of the program:
If CheckRez(640, 480) = False Then
MsgBox "Incorrect screen size!"
Else
MsgBox "Screen Resolution Matches!"
End If
Public?Class?Form1
Dim 初始化控件自動大小調(diào)整與窗口的寬度比例 As Integer
Dim 初始化控件自動大小調(diào)整與窗口的高度比例 As Integer
Private?Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
初始化控件自動大小調(diào)整與窗口的寬度比例 = Me.Width / 控件自動大小調(diào)整.Width
初始化控件自動大小調(diào)整與窗口的高度比例 = Me.Height / 控件自動大小調(diào)整.Height
顯示控件的位置坐標(biāo)()
End?Sub
Private?Sub Form1_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd
' Form1_ResizeEnd? 這個事件是 窗口大小變化完成后 再進(jìn)行操作的
控件自動大小調(diào)整.Location = New Point(控件自動大小調(diào)整.Left, 控件自動大小調(diào)整.Top)
'設(shè)置控件的初始左位置坐標(biāo)? 與? 上位置坐標(biāo)
控件自動大小調(diào)整.Size = New Point(Int(Me.Width / 初始化控件自動大小調(diào)整與窗口的寬度比例), Int(Me.Height / 初始化控件自動大小調(diào)整與窗口的高度比例))
'更改控件的大小? 按第一次窗口的比例 進(jìn)行調(diào)整
顯示控件的位置坐標(biāo)()
End Sub
Private?Sub 顯示控件的位置坐標(biāo)()
Label1.Text = "控件的上邊距坐標(biāo): " 控件自動大小調(diào)整.Top
Label2.Text = "控件的左邊距坐標(biāo): " 控件自動大小調(diào)整.Left
Label3.Text = "控件的寬度大小: " 控件自動大小調(diào)整.Width
Label4.Text = "控件的高度大?。?" 控件自動大小調(diào)整.Height
End Sub
End?Class
設(shè)計與分辨率無關(guān)的窗體
缺省情況下,當(dāng)改變屏幕分辨率時,Microsoft Visual Basic 不會改變窗體與控件的尺寸。這就意味著在分辨率為 1024 X 768 的屏幕上設(shè)計的窗體,在分辨率為 640 X 480 的屏幕中運(yùn)行時會伸出屏幕的邊界之外。如果想創(chuàng)建不管使用什么樣的屏幕分辨率都能有相同比例的窗體和控件,必須在最低的分辨率下設(shè)計窗體,或者將改變窗體的代碼添加到程序中去。
避免尺寸問題的最簡單的方法是在 640 X 480 的分辨率下設(shè)計窗體。如果更喜歡在高一些的分辨率下工作,仍需要考慮窗體在低一些的分辨率下將如何顯示。實現(xiàn)這一點(diǎn)的方法是用“Form Layout”窗口預(yù)覽窗體的大小和位置。您也可以使用“Resolution Guides”觀察在低分辨率時屏幕的哪些部分是可見的。要切換到“Resolution Guides”,可以在“Form Layout”窗口單擊鼠標(biāo)右鍵,從彈出菜單上選擇“Resolution Guides”菜單項。
在運(yùn)行時,Visual Basic 根據(jù)設(shè)計時的位置來放置窗體。如果設(shè)計時在 1024 X 768 的分辨率上運(yùn)行,并把窗體放到屏幕的右下角,則當(dāng)它在比較低的分辨率下運(yùn)行時該窗體可能看不見。為了避免這種情況的發(fā)生,在設(shè)計時可從“Form Layout”窗口的彈出菜單選擇“Startup Position”菜單項來設(shè)置窗體的啟動位置。同樣,您也可以在運(yùn)行時用下面的 Form Load 事件中的代碼來設(shè)置窗體的位置:
Private Sub Form_Load()
Me.Move Screen.Width - Width , 0
End Sub
'下面代碼除字體外,基本能自動適應(yīng)不同分辨率了:
Option Explicit
Dim MeWidth As Long
Dim MeHeight As Long
Private Type ctr
Width As Long
Height As Long
Left As Long
Top As Long
End Type
Dim myctr() As ctr
Private Sub Form_Load()
Dim i As Long, kx As Single, ky As Single
MeWidth = Me.ScaleWidth
MeHeight = Me.ScaleHeight
ScaleHeight = 1000 ' 設(shè)置高度的單位值。
ScaleWidth = 1000 ' 設(shè)置寬度的單位值。
ReDim myctr(Controls.Count)
'把每個控件的屬性存入自定義類型數(shù)組
For i = 0 To Controls.Count - 1
myctr(i).Width = Controls(i).Width
myctr(i).Height = Controls(i).Height
myctr(i).Left = Controls(i).Left
myctr(i).Top = Controls(i).Top
Next
kx = Screen.Width / 1024 / 15
ky = Screen.Height / 768 / 15
Width = Width * kx
Height = Height * ky
Move Screen.Width - Width , 0
End Sub
Private Sub Form_Resize()
Dim i As Long
Dim MyControl As Control
ScaleHeight = 1000 ' 設(shè)置高度的單位值。
ScaleWidth = 1000 ' 設(shè)置寬度的單位值。
'把自定義類型數(shù)組存入每個控件的屬性
For i = 0 To Controls.Count - 1
Controls(i).Width = myctr(i).Width
Controls(i).Height = myctr(i).Height
Controls(i).Left = myctr(i).Left
Controls(i).Top = myctr(i).Top
Next
End Sub
'注意代碼中以下2行:
'kx = Screen.Width / 1024 / 15
'ky = Screen.Height / 768 / 15
'其中的1024 和768 表示在窗體設(shè)置時的屏幕分辨率,如在其他分辨率設(shè)置時可取相應(yīng)值
默認(rèn)單位是像素
96是系統(tǒng)的一種設(shè)定,每英寸的點(diǎn)數(shù),是系統(tǒng)界面用小字體時的設(shè)置
用像素數(shù)除以dpi沒有意義
這篇文章介紹了VB.NET設(shè)置屏幕分辨率、顏色位數(shù)、刷新率
實例代碼,有需要的朋友可以參考一下
復(fù)制代碼
代碼如下:
Private
Declare
Function
GetDeviceCaps
Lib
"gdi32"
(ByVal
hdc
As
Long,
ByVal
nIndex
As
Long)
As
Long
Private
Declare
Function
ChangeDisplaySettings
Lib
"user32"
Alias
"ChangeDisplaySettingsA"
(lpDevMode
As
Any,
ByVal
dwflags
As
Long)
As
Long
Private
Const
CCDEVICENAME
As
Long
=
32
Private
Const
CCFORMNAME
As
Long
=
32
Private
Const
DM_BITSPERPEL
As
Long
=
H40000
Private
Const
DM_PELSWIDTH
As
Long
=
H80000
Private
Const
DM_PELSHEIGHT
As
Long
=
H100000
Private
Const
DM_DISPLAYFLAGS
As
Long
=
H200000
Private
Const
DM_DISPLAYFREQUENCY
=
H400000
Private
Const
CDS_FORCE
As
Long
=
H80000000
Private
Const
BITSPIXEL
As
Long
=
12
Private
Const
HORZRES
As
Long
=
8
Private
Const
VERTRES
As
Long
=
10
Private
Const
VREFRESH
=
116
Private
Type
DEVMODE
dmDeviceName
As
String
*
CCDEVICENAME
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
*
CCFORMNAME
dmUnusedPadding
As
Integer
dmBitsPerPel
As
Integer
dmPelsWidth
As
Long
dmPelsHeight
As
Long
dmDisplayFlags
As
Long
dmDisplayFrequency
As
Long
End
Type
Private
Sub
cmdChangeDesktopMode_Click()
Dim
DM
As
DEVMODE
With
DM
.dmPelsWidth
=
CInt(txtNewWidth.Text)
.dmPelsHeight
=
CInt(txtNewHeight.Text)
.dmBitsPerPel
=
CInt(txtNewColor.Text)
.dmDisplayFrequency
=
CInt(txtNewFreq.Text)
.dmFields
=
DM_PELSWIDTH
Or
DM_PELSHEIGHT
Or
DM_BITSPERPEL
Or
DM_DISPLAYFREQUENCY
.dmSize
=
LenB(DM)
End
With
If
ChangeDisplaySettings(DM,
CDS_FORCE)
Then
MsgBox
"錯誤!不支持此模式!"
End
If
End
Sub
Private
Sub
Form_Load()
txtOldWidth.Text
=
GetDeviceCaps(Me.hdc,
HORZRES)
txtOldHeight.Text
=
GetDeviceCaps(Me.hdc,
VERTRES)
txtOldColor.Text
=
GetDeviceCaps(Me.hdc,
BITSPIXEL)
txtOldFreq.Text
=
GetDeviceCaps(Me.hdc,
VREFRESH)
End
Sub
保存前加一句 myImage2.SetResolution(300, 300) 你設(shè)置的bMape不是保存的主畫布 所以無效,設(shè)置分辨率就是 SetResolution(X,Y)