|
#If VBA7 And Win64 Then
Public Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As LongPtr, ByVal nIndex As LongPtr) As Long
Public Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long
#Else
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
#End If
'鼠标屏幕坐标(x0,y0) 返回对应EXCEl窗口中位置磅(x,y)
Function CellScreenPos(x0 As Long, y0 As Long) As Variant
Const SplitBarWidth = 6
Const SplitBarHeight = 6
Const RoundConst = 0.5000001
Dim Wn As Window
Dim hdc As Long, px As Long, py As Long
Dim x As Double, y As Double '返回鼠标坐标在Excel屏幕中的位置
Dim dx As Double, dy As Double '相对拆分线的距离
Dim z As Double, spx As Double, spy As Double
On Error GoTo ErrorHandler
hdc = GetDC(0)
px = GetDeviceCaps(hdc, LOGPIXELSX) '获取屏幕分辨率
py = GetDeviceCaps(hdc, LOGPIXELSY) '获取屏幕分辨率
ReleaseDC 0, hdc: hdc = 0
Set Wn = ActiveWindow
z = Wn.Zoom / 100 'Excel缩放因子
spx = Wn.SplitHorizontal * px / 72 + RoundConst '垂直拆分线和Excel窗口左边距离
spy = Wn.SplitVertical * py / 72 + RoundConst '水平拆分线和Excel窗口上边距离
If Application.Version >= 12 Then
ppx = ActiveWindow.Panes(1).PointsToScreenPixelsX(0) 'Excel坐标原点
ppy = ActiveWindow.Panes(1).PointsToScreenPixelsY(0) 'Excel坐标原点
Else
ppx = ActiveWindow.PointsToScreenPixelsX(0) 'Excel坐标原点
ppy = ActiveWindow.PointsToScreenPixelsY(0) 'Excel坐标原点
End If
dx = x0 - ppx - Cells(Wn.Panes(1).ScrollRow, Wn.Panes(1).ScrollColumn).Left * px * z / 72 - spx
dy = y0 - ppy - Cells(Wn.Panes(1).ScrollRow, Wn.Panes(1).ScrollColumn).Top * py * z / 72 - spy
If dx > 0 Then If Not Wn.FreezePanes Then x = x - SplitBarWidth '如果拆分线未冻结
If dy > 0 Then If Not Wn.FreezePanes Then y = y - SplitBarHeight '如果拆分线未冻结
Select Case Sgn(Wn.SplitVertical) * 2 + Sgn(Wn.SplitHorizontal)
Case 0
x = x0 - ppx
y = y0 - ppy
Case 2 '水平
If dy >= 0 Then
x = x0 - ppx
y = y + dy + Cells(Wn.Panes(2).ScrollRow, Wn.Panes(2).ScrollColumn).Top * py * z / 72
Else
x = x0 - ppx
y = y0 - ppy
End If
Case 1 '垂直
If dx >= 0 Then
x = x + dx + Cells(Wn.Panes(2).ScrollRow, Wn.Panes(2).ScrollColumn).Left * px * z / 72
y = y0 - ppy
Else
x = x0 - ppx
y = y0 - ppy
End If
Case 3 '水平垂直
If dy >= 0 And dx < 0 Then
x = x0 - ppx
y = y + dy + Cells(Wn.Panes(3).ScrollRow, Wn.Panes(3).ScrollColumn).Top * py * z / 72
ElseIf dy >= 0 And dx >= 0 Then
x = x + dx + Cells(Wn.Panes(4).ScrollRow, Wn.Panes(4).ScrollColumn).Left * px * z / 72
y = y + dy + Cells(Wn.Panes(4).ScrollRow, Wn.Panes(4).ScrollColumn).Top * py * z / 72
End If
If dy < 0 And dx < 0 Then
x = x0 - ppx
y = y0 - ppy
ElseIf dy < 0 And dx >= 0 Then
x = x + dx + Cells(Wn.Panes(2).ScrollRow, Wn.Panes(2).ScrollColumn).Left * px * z / 72
y = y0 - ppy
End If
End Select
x = x * 72 / (px * z)
y = y * 72 / (py * z)
CellScreenPos = Array(x, y)
Exit Function
ErrorHandler:
If hdc <> 0 Then ReleaseDC 0, hdc
Exit Function
End Function |
评分
-
1
查看全部评分
-
|