Excel形状位置受到Windows显示缩放设置的干扰 [英] Excel Shape position disturbed by Windows Display Zoom settings
问题描述
我想在Excel中获得准确的Shape位置.我注意到
这是我想要解决的错误行为,该错误会在125%的显示缩放下发生:
以下是我向SO提出的不显眼的挑战,这可能是回答此问题的里程碑:获取Windows显示缩放值
我无法重现您的问题.我正在使用150%,即使在最后一个单元格中,Excel中的定位也是正确的.
此外,也无需纠正任何内容.
但是您的代码可能存在一些问题:
- 避免使用
ThisWorkbook.ActiveSheet
,并使用Target.Parent
,这样更可靠. - 还应避免使用
ActiveCell
和使用Target
,因为ActiveCell
可能尚未更改为您单击的单元格.Target
是您双击的单元格,不是ActiveCell
.
请尝试一下.我怀疑DPI是问题,我怀疑这是与 ActiveCell
相关的问题.
选项显式私人子Worksheet_BeforeDoubleClick(ByVal目标为范围,取消为布尔值)取消=正确关于错误继续Target.Parent.Shapes("BlueRectangle").DeleteOn Error GoTo 0'总是在预期的错误后重新激活错误处理形状变暗设置shp = Target.Parent.Shapes.AddShape(msoShapeRectangle,Target.Left,Target.Top,Target.Width,Target.Height)shp.Name ="BlueRectangle"结束子
I would like to get accurate Shape position in Excel. I noticed that Shape.Top is being disturbed by Windows Display Zoom settings.
To reproduce the bug, please right click on a sheet name > View code > and paste the VBA code in the sheet VBA editor.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
On Error Resume Next
ThisWorkbook.ActiveSheet.Shapes("BlueRectangle").Delete
Dim sh As Object
Set sh = ThisWorkbook.ActiveSheet.Shapes.AddShape(msoShapeRectangle, ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height)
sh.Name = "BlueRectangle"
End Sub
This code creates Rectange shape in the double clicked cell. Everything works fine as long as the display zoom of Windows settings is set up to 100%. However when we change display zoom in Windows settings to 125% then the rectangle is created in a slightly different place than the Active cell. There is a difference of 1 row in the location height for every 100 rows of Excel. So, when I click A100 cell then the Rectangle is created in A99 cell.
I would like to correct the location Rectangle creation so that Windows Zoom Display is taken into account.
Here is behavior with 100% Display Zoom:
Here is a buggy behavior I would like to fix which happens with 125% Display Zoom:
Here is the related inconspicuous challenge I threw on SO which might be a milestone in answering this question: Get Windows display zoom value
I cannot reproduce your issue. I'm working with 150% and positioning is correct in Excel even for the very last cells.
Also there should be nothing need to be corrected.
But there might be some issues with your code:
- Avoid
ThisWorkbook.ActiveSheet
and useTarget.Parent
this is more reliable. - Also avoid using
ActiveCell
and useTarget
becauseActiveCell
might not have changed to the cell you clicked on yet.Target
is the cell you doubleclicket notActiveCell
.
Give the follwing a try. I doupt that the DPI is the issue and I suspect it is a ActiveCell
related issue.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
On Error Resume Next
Target.Parent.Shapes("BlueRectangle").Delete
On Error GoTo 0 'always re-activate error handling after an expected error
Dim shp As Shape
Set shp = Target.Parent.Shapes.AddShape(msoShapeRectangle, Target.Left, Target.Top, Target.Width, Target.Height)
shp.Name = "BlueRectangle"
End Sub
这篇关于Excel形状位置受到Windows显示缩放设置的干扰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!