Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetDC Lib "user32" _
(ByVal HWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" _
(ByVal HWnd As Long, _
ByVal hdc As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hdc As Long, _
ByVal nIndex As Long) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function CreateRectRgn Lib "gdi32" _
(ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Private Declare Function RedrawWindow Lib "user32" _
(ByVal HWnd As Long, _
ByVal lprcUpdate As Long, _
ByVal hrgnUpdate As Long, _
ByVal fuRedraw As Long) As Long
Private Declare Function SetTimer Lib "user32" _
(ByVal HWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerfunc As Long) As Long
Private Declare Function KillTimer Lib "user32" _
(ByVal HWnd As Long, _
ByVal nIDEvent As Long) As Long
Private Const RDW_INVALIDATE = &H1
Private Const RDW_ALLCHILDREN = &H80
Private oTargetCell As Range
Private lTimerId As Long, lhwnd As Long
Sub StartFlashing()
AddFlashingEffect Sheets(1).Range("a1")
End Sub
Sub StopFlashing()
RemoveFlashingEffect Sheets(1).Range("a1")
End Sub
Private Sub AddFlashingEffect(Cell As Range)
Const lTimeInterv As Long = 1000
lhwnd = FindWindow("XLMAIN", Application.Caption)
Set oTargetCell = Cell
With oTargetCell
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=MOD(SECOND(NOW()),2)=1"
.FormatConditions(1).Interior.ColorIndex = 4
End With
lTimerId = SetTimer(0, 0, lTimeInterv, AddressOf TimerProc)
End Sub
Private Sub RemoveFlashingEffect(Cell As Range)
KillTimer 0, lTimerId
Cell.FormatConditions.Delete
End Sub
Private Sub TimerProc(ByVal HWnd As Long, _
ByVal uMsg As Long, _
ByVal nIDEvent As Long, _
ByVal dwTimer As Long)
Dim hRng As Long
On Error Resume Next
With GetRangeRect(oTargetCell)
hRng = CreateRectRgn(0, 0, .Right, .Bottom)
End With
RedrawWindow lhwnd, 0, hRng, RDW_INVALIDATE + RDW_ALLCHILDREN
End Sub
Private Function ScreenDPI(bVert As Boolean) As Long
Static lDPI(1), lDC
If lDPI(0) = 0 Then
lDC = GetDC(0)
lDPI(0) = GetDeviceCaps(lDC, 88)
lDPI(1) = GetDeviceCaps(lDC, 90)
lDC = ReleaseDC(0, lDC)
End If
ScreenDPI = lDPI(Abs(bVert))
End Function
Private Function PTtoPX _
(Points As Single, bVert As Boolean) As Long
PTtoPX = Points * ScreenDPI(bVert) / 72
End Function
Private Function GetRangeRect(ByVal rng As Range) As RECT
Dim OWnd As Window
Set OWnd = rng.Parent.Parent.Windows(1)
With rng
GetRangeRect.Left = PTtoPX(.Left * OWnd.Zoom / 100, 0) _
+ OWnd.PointsToScreenPixelsX(0)
GetRangeRect.Top = PTtoPX(.Top * OWnd.Zoom / 100, 1) _
+ OWnd.PointsToScreenPixelsY(0)
GetRangeRect.Right = PTtoPX(.Width * OWnd.Zoom / 100, 0) _
+ GetRangeRect.Left
GetRangeRect.Bottom = PTtoPX(.Height * OWnd.Zoom / 100, 1) _
+ GetRangeRect.Top
End With
End Function