我被要求编写代码,使其能够单击 Excel 中的图像并在其顶部添加形状(这是物理治疗师的身体图,该形状将指示患者疼痛的部位)。我的代码通过使用 ActiveX 图像控件的鼠标按下事件来完成此操作:
Private Sub bodypic_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
ClickShape x, y
End Sub
Sub ClickShape(x As Single, y As Single)
Dim shp As Shape
Dim cursor As Point
Set shp = ActiveSheet.Shapes.AddShape(msoShapeMathMultiply, x + ActiveSheet.Shapes("bodypic").Left, _
y + ActiveSheet.Shapes("bodypic").Top, 26, 26)
With shp.Fill
.ForeColor.RGB = RGB(255, 0, 0)
.BackColor.RGB = RGB(255, 0, 0)
End With
shp.Line.Visible = False
End Sub
问题是,当鼠标光标位于图表上方时,形状不可见。只有当鼠标移离图表时,形状才会出现。
我试过了各种方法 https://stackoverflow.com/questions/3735378/force-a-screen-update-in-excel-vba刷新屏幕,选择一个单元格,甚至通过更改光标位置Lib user32 中的 SetCursor 方法 https://support.microsoft.com/en-gb/help/152969/visual-basic-procedure-to-get-set-cursor-position。除了用户实际移动鼠标之外,似乎什么都不起作用。
要重现该问题:插入大约 200 x 500 像素的 ActiveX 图像控件,向该控件添加 jpeg 图像,向工作表添加鼠标按下代码,向模块添加单击形状代码。