there's still a problem...
@AngelFire: I'm painting the rectangle to an non-VB-window, so there's no Me.AutoRedraw
@DaSilvy: WM_REDRAW doesn't exist.
@jim mcnamara: I'm painting a transparent rectangle over a button on a toolbar in winword, so there's no background color.
I thought, InvalidateRectangle would do, but it only works for hwnd=0. This would refresh the whole screen (flicker, flicker....). I only want the painted rectangle to disappear. I've tried UpdateWindow, RedrawWindow, WM_PAINT, WM_NCPAINT etc. Since the rectangle is painted to the Winword application window, I also tried the VBA-function Application.ScreenRefresh. Nothing happens!:mad: The rectangle wouldn't disappear.:confused:
Please check the code I'm using, perhaps you have some suggestions, how to solve the problem.
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreateBrushIndirect Lib "gdi32" (lpLogBrush As LOGBRUSH) As Long
Private Declare Function CreatePenIndirect Lib "gdi32" (lpLogPen As LOGPEN) As Long
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, _
ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function InvalidateRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT, ByVal bErase As Long) As Long
Private Declare Function UpdateWindow Lib "user32" (ByVal hwnd As Long) As Long
'--------------------------------
Private Sub PaintRect (hwnd As Long, lpRect As RECT, lgBrush As LOGBRUSH, lgPen As LOGPEN)
Dim dc As Long, _
hPen As Long, _
hOldPen As Long, _
hBrush As Long, _
hOldBrush As Long
dc = GetWindowDC(hwnd)
hBrush = CreateBrushIndirect(lgBrush)
hPen = CreatePenIndirect(lgPen)
hOldPen = SelectObject(dc, hPen)
hOldBrush = SelectObject(dc, hBrush)
With lpRect
Rectangle dc, .Left, .Top, .Right, .Bottom
End With
DeleteObject SelectObject(dc, hOldPen)
DeleteObject SelectObject(dc, hOldBrush)
ReleaseDC hwnd, dc
End Sub
'--------------------------------
Private Sub KillRect(hwnd As Long, lpRect As RECT)
InvalidateRect hwnd, lpRect, RECT_ERASE
UpdateWindow hwnd
End Sub
Thanks for your help, Quix