473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Adding Rectangle/Box Around External Window

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

Nov 21 '05 #1
6 2647
Alex,

"Alex Pierson" <ar******@charter.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
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?

Nov 21 '05 #3
Alex,

"Alex Pierson" <ar******@charter.net> schrieb:
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').

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
thanks.
works great now.
can you give me a quick and dirty: why?

Nov 21 '05 #5
Alex,

"Alex Pierson" <ar******@charter.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6
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?

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
3
by: Lasse Eskildsen | last post by:
Hi, I have a panel on my form, and I would like to add a shadow around that panel, just like Xp as around the menus etc. I tried drawing it with the lineargradientbrush, but it doesn't look very...
0
by: Alex Pierson | last post by:
I am using SetROP2 to set the mix mode of a window's rectangle as R2_NOT and rectangle to draw a box around the window in inverse color. My problem is determining whether the current state of the...
5
by: DazedAndConfused | last post by:
I have a rectangle around text that I want to fill in with color. I do not know the height of the rectangle until I actually go through and draw out the text. Is there a way of filling in the...
1
by: jediknight | last post by:
Hi All, I am very new to ASP.NET but have used c# for windows very much. I would like to display an imagemap which has the background as the map of a city centre and icons denoting important...
0
by: Ichor | last post by:
I'm really sorry, I'm sure this is a horrible newb question. But I'm really new to Python and I've been looking all over and I can't get anything to work. I want to be able to draw rectangles...
2
by: zhengyan11 | last post by:
Hey, I encounter a problem when I am trying to add a pop up window on my page, which will trigger a function window.external.addSearchProvider(URL). This function will open a dialog box window....
10
by: sklett | last post by:
I have a situation where I'm getting in Image that has a gray (solid, same color) background with a smaller white rectangle inside. The position is not always the same. What I need to do is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.