Highlighting Objects

The following function uses a Win32 function to draw a rectangle that will highlight a given object using the object coordinates. The following example can be found in the HighlightObject.vbs file located in the <Installdir>\CodeSamplesPlus folder.

' Declare necessary APIs
Extern.Declare micHwnd, "GetDesktopWindow", "User32.DLL", "GetDesktopWindow"
Extern.Declare micULong, "GetWindowDC", "User32.DLL", "GetWindowDC", micHwnd
Extern.Declare micInteger, "ReleaseDC", "User32.DLL", "ReleaseDC", micHwnd, micULong
Extern.Declare micULong, "CreatePen", "Gdi32.DLL", "CreatePen", micInteger, micInteger, micDword
Extern.Declare micInteger, "SetROP2", "Gdi32.DLL", "SetROP2", micULong, micInteger
Extern.Declare micULong, "SelectObject", "Gdi32.DLL", "SelectObject", micULong, micULong
Extern.Declare micULong, "DeleteObject", "Gdi32.DLL", "DeleteObject", micULong
Extern.Declare micULong, "GetStockObject", "Gdi32.DLL", "GetStockObject", micInteger
Extern.Declare micULong, "Rectangle", "Gdi32.DLL", "Rectangle", micULong, micInteger, micInteger, micInteger, micInteger

Function HighlightRect (X, Y, W, H, Times)

	' Get the Desktop DC
	hDC = Extern.GetWindowDC (Extern.GetDesktopWindow)
	' Create a three pixel wide pen
	hPen = Extern.CreatePen (6, 3, RGB(0, 0, 0)) ' PS_INSIDEFRAME, 3 , RGB(0, 0, 0)
	Extern.SetROP2 hDC, 6 ' hDC, R2_NOT
	Extern.SelectObject hDC, hPen
	' Use an empty fill
	Extern.SelectObject hDC, Extern.GetStockObject (5) ' NULL_BRUSH

	' Do the highlight
	For i = 0 to Times * 2 + 1
		Extern.Rectangle hDC, X, Y, X + W, Y + H
		wait 0, 50
	Next
	' CleanUp
	Extern.ReleaseDC Extern.GetDesktopWindow, hDC
	Extern.DeleteObject hPen
End Function