用箭头键导航时,将鼠标指针移至活动单元格的中心
我正在尝试使用箭头键从单元格导航到单元格时,将鼠标指针移动到所选单元格的中心
在Excel 2010中,以下解决scheme完美地工作
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Private Sub Worksheet_SelectionChange(ByVal Target As Range) SetCursorPos _ ActiveWindow.ActivePane.PointsToScreenPixelsX(Target.Left + (Target.Width / 2)), _ ActiveWindow.ActivePane.PointsToScreenPixelsY(Target.Top + (Target.Height / 2)) End Sub
但是在Excel 2003中, ActiveWindow.ActivePane
没有PointsToScreenPixelsX
和PointsToScreenPixelsY
方法。 所以我试图find另一个解决scheme,如下面的解决scheme。 X轴工作正常,但Y轴不正确。
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Private Sub Worksheet_SelectionChange(ByVal Target As Range) SetCursorPos _ ActiveWindow.Application.ActiveWindow.PointsToScreenPixelsX((Target.Left + (Target.Width / 2)) / 0.75), _ ActiveWindow.Application.ActiveWindow.PointsToScreenPixelsY((Target.Top + (Target.Height / 2)) / 0.75) End Sub
我希望这个工作不pipe分辨率等任何想法?
这应该适用于旧版本的Excel。 这是笨重的,但它完成了工作。
Declare Function SetCursorPos Lib "user32" _ (ByVal x As Long, ByVal y As Long) As Long Declare Function GetDC Lib "user32" ( _ ByVal hwnd As Long) As Long Declare Function ReleaseDC Lib "user32" ( _ ByVal hwnd As Long, ByVal hDC As Long) As Long Declare Function GetDeviceCaps Lib "gdi32" ( _ ByVal hDC As Long, ByVal nIndex As Long) As Long Sub MoveMouseToRange(R As Range) Static lDPI&(1), lDC& If lDPI(0) = 0 Then lDC = GetDC(0) lDPI(0) = GetDeviceCaps(lDC, 88&) 'this is the horizontal 'resolution of the user's screen, 'in DPI lDPI(1) = GetDeviceCaps(lDC, 90&) 'vertical lDC = ReleaseDC(0, lDC) End If Zoom = R.Parent.Parent.Windows(1).Zoom x = (R.Left + 0.5 * R.Width) * Zoom / 100 / 72 * lDPI(0) y = (R.Top + 0.5 * R.Height) * Zoom / 100 / 72 * lDPI(1) SetCursorPos x, y End Sub