|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
主代码如下(API和事件代码请看附件):- Option Explicit
- Public x&, y&
- Sub GDIDraw()
- Dim x&, y&, ZM#, r&, c&, x0&, y0&, flag&
- '可在此处设置开关
- ZM = ActiveWindow.Zoom / 100 '显示的缩放倍数
- Dim Rg As Range
- Set Rg = ActiveWindow.RangeSelection.Areas(1)
- Dim 左 As Double, 顶 As Double, 宽 As Double, 高 As Double
- With Rg
- 左 = .Left
- 顶 = .Top
- 宽 = .Width
- 高 = .Height
- End With
- Dim x1&, y1&, x2&, y2& '
- With ActiveWindow '对选定区域Rg的左上顶点和右下顶点,计算屏幕坐标(单位:像素)
- '先计算两个坐标系统的偏移量
- r = .SplitRow + 1: c = .SplitColumn + 1
- '窗格拆分或冻结时产生的偏移量
- If c > 1 Then x0 = Cells(1, c).Left * ZM * dpiX / INCH2POINTS '原点偏移量
- If r > 1 Then y0 = Cells(r, 1).Top * ZM * dpiY / INCH2POINTS
-
- If .DisplayHeadings Then '行号列标在窗口拆分时带来的坐标原点偏移量
- flag = 0
- If .Split = True Then
- If .FreezePanes = True Then
- flag = 3
- Else
- flag = 2
- End If
- Else
- If .FreezePanes = True Then flag = 1
- End If
- If flag > 0 Then
- .FreezePanes = False: .Split = False
- If c > 1 Then x = .PointsToScreenPixelsX(0)
- If r > 1 Then y = .PointsToScreenPixelsY(0) - .PointsToScreenPixelsY(.Top * ZM * dpiY / INCH2POINTS) '
- .SplitRow = r - 1: .SplitColumn = c - 1
- If flag = 2 Then .Split = True
- If flag = 3 Then .FreezePanes = True
- End If
- End If
- '最终的坐标:
- x1 = .PointsToScreenPixelsX(左 * ZM * dpiX / INCH2POINTS) + IIf(c > 1, x0 + x, 0) '坐标转换计算。X方向附加原点漂移值x0、x
- y1 = .PointsToScreenPixelsY(顶 * ZM * dpiY / INCH2POINTS) + IIf(r > 1, y0 + y, 0)
- x2 = x1 + 宽 * ZM * dpiX / INCH2POINTS
- y2 = y1 + 高 * ZM * dpiY / INCH2POINTS
- End With
- draww x1, y1, x2, y2, RGB(255, 0, 0) '聚光灯颜色rgb值,本例简单设为红色(可调用EXCEL内置的颜色自定义对话框)。如颜色不支持,则容易闪屏。
- End Sub
复制代码 |
|