您的位置:首页 > 编程语言 > VB

VB中Screen.TwipsPerPixelX和Screen.TwipsPerPixelY在Windows7中的问题

2020-02-01 16:34 2386 查看

测试在XP系统下,DPI计算似乎没问题
Screen.TwipsPerPixelX=1440/DPI=1440/96=15
Screen.TwipsPerPixelX=1440/DPI=1440/120=12
Screen.TwipsPerPixelX=1440/DPI=1440/144=10

测试在Windows7下(x64,SP1),DPI为96或120时,各种主题下获取似乎都正确.
当DPI为100%(96),三种主题下,获取的值为15
当DPI为125%(120),三种主题下,获取的值为12
当DPI为150%(144),主题为Windows7(Aero)时,获取的值为15(不正确)
当DPI为150%(144),主题为经典或Basic时,获取的值为10

简单来说如果主题为Windows7默认,DPI为150%时,获取的Screen.TwipsPerPixelX和Screen.TwipsPerPixelY将不准确.
用Screen.Width / Screen.TwipsPerPixelX计算分辨率也就不正确了

上图是在Basic主题下截图


上图是在经典主题下截图


上图是在Windows7下截图


从Windows7主题切换到其他主题时,可以看到最后一个窗口的实际显示比例和另外两个不同

图片未做任何处理,使用的Print截屏
发现Screen.Width和Screen.Height两个值在Windows7主题下也有5点误差,不知道是否显示器的问题,不知道是否与显示器有关.

因为在Windows7主题下打开的窗体切换之后和其他窗体不一样大,但在125%DPI时没有这个问题,推测很可能是Windows7本身的问题.

Private Sub Command1_Click()
屏幕宽度 = Screen.Width / Screen.TwipsPerPixelX
屏幕高度 = Screen.Height / Screen.TwipsPerPixelY

窗体宽度 = Me.Width / Screen.TwipsPerPixelX
窗体高度 = Me.Height / Screen.TwipsPerPixelY

窗体工作区宽度 = Me.ScaleWidth / Screen.TwipsPerPixelX
窗体工作区高度 = Me.ScaleHeight / Screen.TwipsPerPixelY

屏幕宽度比 = Screen.TwipsPerPixelX
屏幕高度比 = Screen.TwipsPerPixelY

窗体两侧边框 = 窗体宽度 - 窗体工作区宽度
框体上下边框 = 窗体高度 - 窗体工作区高度

Text1 = ""
Text1 = Text1 & "屏幕宽度" & 屏幕宽度 & vbCrLf
Text1 = Text1 & "屏幕高度" & 屏幕高度 & vbCrLf
Text1 = Text1 & "窗体宽度" & 窗体宽度 & vbCrLf
Text1 = Text1 & "窗体高度" & 窗体高度 & vbCrLf
Text1 = Text1 & "窗体工作区宽度" & 窗体工作区宽度 & vbCrLf
Text1 = Text1 & "窗体工作区高度" & 窗体工作区高度 & vbCrLf
Text1 = Text1 & "屏幕宽度比" & 屏幕宽度比 & vbCrLf
Text1 = Text1 & "屏幕高度比" & 屏幕高度比 & vbCrLf
Text1 = Text1 & "窗体两侧边框" & 窗体两侧边框 & vbCrLf
Text1 = Text1 & "框体上下边框" & 框体上下边框 & vbCrLf

Text1 = Text1 & "Screen.Width" & Screen.Width & vbCrLf
Text1 = Text1 & "Screen.Height" & Screen.Height & vbCrLf

Text1 = Text1 & "Me.Width" & Me.Width & vbCrLf
Text1 = Text1 & "Me.Height" & Me.Height & vbCrLf

Text1 = Text1 & "Me.ScaleWidth" & Me.ScaleWidth & vbCrLf
Text1 = Text1 & "Me.ScaleHeight" & Me.ScaleHeight & vbCrLf

Text1 = Text1 & "Me.Left" & Me.Left & vbCrLf
Text1 = Text1 & "Me.Top" & Me.Top & vbCrLf
End Sub

补充测试:
ScaleX方法测试结果也不准确
Me.ScaleX(1, 3, 1)等同于Screen.TwipsPerPixelX

Private Sub Command2_Click()
Dim dpi_x As Single, dpi_y As Single, px_twip As Single
dpi_x = Me.ScaleX(Screen.Width, 1, 3) / Me.ScaleX(Screen.Width, 1, 5)
dpi_y = Me.ScaleY(Screen.Height, 1, 3) / Me.ScaleY(Screen.Height, 1, 5)
px_twip = Me.ScaleX(1, 3, 1)

Text1 = Text1 & "1像素 = " & Me.ScaleX(1, 3, 1) & " 缇" & vbCrLf
Text1 = Text1 & "1像素 = " & Me.ScaleY(1, 3, 1) & " 缇" & vbCrLf
Text1 = Text1 & "DPI_X = " & dpi_x & " " & vbCrLf
Text1 = Text1 & "DPI_Y = " & dpi_y & " " & vbCrLf

End Sub
  • 点赞
  • 收藏
  • 分享
  • 文章举报
星云网络 发布了2 篇原创文章 · 获赞 0 · 访问量 275 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: