TransparentForm Code Efficiency



I have a form with 5 labels on it. I'm using the labels to provide a read
out of data next to the cursor position. The cursor position is passed to
the function as MoveToX and MoveToY which instructs the code to move the
form to that location.

To show JUST the labels on the form and nothing else I'm using regions and
then combining them. Lastly, the window is set at the correct location and
placed on top using the SetWindowPos API. The entire function is contained
below.

While the code works great, it's actually a CPU hog --whose symptons are
worsened when it's called continually on the MouseMove with the mouse
cursor. I was wondering if anywhere here could see a way to optimize this
code. I placed some rough timers in here, which seem to indicate the problem
is with the SetWindowRgn and SetWindowPos APIs, but it just may be a facit
of the timer. If there's a better way to measure this, please let me know.

Thanks!
Evan

'Move form without a border declarations
Option Explicit
'Create different types of regions declares
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal
y1 As Long, ByVal x2 As Long, ByVal y2 As Long) As Long
'Sets the region
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long,
ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
'Combines the region
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long,
ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long)
As Long
'Type of combine
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As
Long

Const RGN_XOR = 3

Public Sub MakeTransparent(ByVal MoveToX As Long, MoveToY As Long)
'In case there's an error, ignore it
On Error Resume Next
Dim Regn As Long
Dim TmpRegn As Long
Dim TmpControl As Control
Dim Start As Single

Start = Timer

'makes everything invisible
Regn = CreateRectRgn(0, 0, 0, 0)

'A loop to check every control in the form
For Each TmpControl In Me
'Create a rectangular region with its parameters
If TmpControl.Visible = True And TmpControl.Width <> 0 Then
TmpRegn = CreateRectRgn(TmpControl.Left, TmpControl.Top,
TmpControl.Left + TmpControl.Width, TmpControl.Top + TmpControl.Height)
'Combines the regions
CombineRgn Regn, Regn, TmpRegn, RGN_XOR
DeleteObject TmpRegn
End If
Next TmpControl

Debug.Print "TIME TO COMPLETE STEP 1: " & Timer - Start
Start = Timer
'Make the regions
SetWindowRgn Me.hWnd, Regn, True
DeleteObject (Regn)
Debug.Print "TIME TO COMPLETE STEP 2: " & Timer - Start
Start = Timer
SetWindowPos Me.hWnd, HWND_TOPMOST, MoveToX, MoveToY, 0, 0, SWP_NOSIZE
Or SWP_NOACTIVATE Or SWP_SHOWWINDOW
Me.Visible = True
Debug.Print "TIME TO COMPLETE STEP 3: " & Timer - Start

End Sub


.