Adding Rectangle/Box Around External Window

  • Thread starter Thread starter Alex Pierson
  • Start date Start date
A

Alex Pierson

I searched the VB and VB.net groups endlessly and cannot find any code
to help me do this.

What I'm trying to do is to draw a highlighted rectangle around a child
window from another application. I have the handle for the other
application window and can control it, but I cannot figure out how to
add a rectangle around it in VB.net.

Can anyone help me?

Thanks.

Alex
 
Alex,

Alex Pierson said:
What I'm trying to do is to draw a highlighted rectangle around a child
window from another application. I have the handle for the other
application window and can control it, but I cannot figure out how to
add a rectangle around it in VB.net.

A VB6 sample can be found here:

<URL:http://vb.mvps.org/samples/project.asp?id=wndpick>

Notice that you will have to rework the code to make it work with VB.NET.
 
Thanks for the link....But I can't seem to get it to work. Here is my
code:

Form code:

Declare Auto Function FindWindow Lib "user32" (ByVal lpClassWindow
As String, ByVal lpWindowName As String) As IntPtr

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim HWND As IntPtr = FindWindow(Nothing, "Calculator")
FrameWindow(HWND)
End Sub

Module code:

Module Module1
' Win32 APIs...
Private Declare Function IsWindow Lib "user32" (ByVal hWnd As
IntPtr) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As
IntPtr) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As
IntPtr, ByVal hDC As Long) 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 CreatePen Lib "gdi32" (ByVal nPenStyle As
Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal
nIndex As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As
Long, ByVal hObject As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex
As Long) As Long
Private Declare Function SetROP2 Lib "gdi32" (ByVal hDC As Long,
ByVal nDrawMode As Long) As Long
Private Declare Function CreateHatchBrush Lib "gdi32" (ByVal nIndex
As Long, ByVal crColor As Long) As Long
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As
Long) As Long
Private Declare Function GetWindowRgn Lib "user32" (ByVal hWnd As
IntPtr, ByVal hRgn As Long) As Long
Private Declare Function FrameRgn Lib "gdi32" (ByVal hDC As Long,
ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal
nHeight As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As
IntPtr, ByVal lpRect As RECT) As Long
Private Declare Function IsZoomed Lib "user32" (ByVal hWnd As
IntPtr) 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 DeleteObject Lib "gdi32" (ByVal hObject As
Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long)
As Long

Private Const PS_INSIDEFRAME As Long = 6
Private Const SM_CXSCREEN As Long = 0
Private Const SM_CYSCREEN As Long = 1
Private Const SM_CXBORDER As Long = 5
Private Const SM_CYBORDER As Long = 6
Private Const SM_CXFRAME As Long = 32
Private Const SM_CYFRAME As Long = 33
Private Const NULL_BRUSH As Long = 5
Private Const NULL_PEN As Long = 8
Private Const R2_NOT As Long = 6
Private Const HS_DIAGCROSS As Long = 5
Private Const CTLCOLOR_STATIC As Long = 6

' Region Flags
Private Const ERRORAPI As Long = 0
Private Const NULLREGION As Long = 1
Private Const SIMPLEREGION As Long = 2
Private Const COMPLEXREGION As Long = 3

Private Structure RECT
Dim Left As Long
Dim Top As Long
Dim Right As Long
Dim Bottom As Long
End Structure

Public Sub FrameWindow(ByVal hWnd As IntPtr, Optional ByVal
PenWidth As Long = 3)
Dim hDC As Long
Dim hRgn As Long
Dim hPen As Long
Dim hOldPen As Long
Dim hBrush As Long
Dim hOldBrush As Long
Dim OldMixMode As Long
Dim cxFrame As Long
Dim cyFrame As Long
Dim r As RECT

If IsWindow(hWnd) Then
hDC = GetWindowDC(hWnd)
hRgn = CreateRectRgn(0, 0, 0, 0)
hPen = CreatePen(PS_INSIDEFRAME, _
GetSystemMetrics(SM_CXBORDER) * PenWidth,
_
RGB(0, 0, 0))

hOldPen = SelectObject(hDC, hPen)
hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH))
OldMixMode = SetROP2(hDC, R2_NOT)

If GetWindowRgn(hWnd, hRgn) <> ERRORAPI Then
hBrush = CreateHatchBrush(HS_DIAGCROSS,
GetSysColor(CTLCOLOR_STATIC))
Call FrameRgn(hDC, hRgn, hBrush, _
GetSystemMetrics(SM_CXBORDER) * PenWidth,
_
GetSystemMetrics(SM_CYBORDER) * PenWidth)

Else
cxFrame = GetSystemMetrics(SM_CXFRAME)
cyFrame = GetSystemMetrics(SM_CYFRAME)
Call GetWindowRect(hWnd, r)

If IsZoomed(hWnd) Then
Call Rectangle(hDC, cxFrame, cyFrame, _
GetSystemMetrics(SM_CXSCREEN) +
cxFrame, _
GetSystemMetrics(SM_CYSCREEN) +
cyFrame)
Else
Call Rectangle(hDC, 0, 0, r.Right - r.Left,
r.Bottom - r.Top)
End If
End If

' // cleanup....
Call SelectObject(hDC, hOldPen)
Call SelectObject(hDC, hOldBrush)
Call SetROP2(hDC, OldMixMode)
Call DeleteObject(hPen)
Call DeleteObject(hBrush)
Call DeleteObject(hRgn)
Call ReleaseDC(hWnd, hDC)
End If
End Sub

End Module

Can anybody figure out whats wrong?
 
Alex,

Alex Pierson said:
Thanks for the link....But I can't seem to get it to work. Here is my
code:

'As Long' => 'As Int32' (or 'As Integer').
 
thanks.
works great now.
can you give me a quick and dirty: why?
 
Alex,

Alex Pierson said:
can you give me a quick and dirty: why?

In VB6 'Long' was a 32-bit datatype, but in .NET it's a 64-bit datatype.
In VB6 'Integer' was a 16-bit datatype, but in .NET it's a 32-bit datatype.
 
I spoke too soon. This code works for parent windows, but causes an
error when trying to box child windows. I can't figure out why this is
occurring. Ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top