How PtInRegion works under VB.NET

W

wael

Hi everyone

Can someone please tell me how do I use PtInRegion? I have been trying for
few days but no success.

Do I have to link any extra library or .net component in order for my code
to run?

I'm running XP Pro and VS.Net ver. 2002

I have included below my latest code attempt


Thanks,
Wael

'###################################################################

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents lbl1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough() Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.lbl1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(440, 328)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'lbl1
'
Me.lbl1.Location = New System.Drawing.Point(16, 312)
Me.lbl1.Name = "lbl1"
Me.lbl1.Size = New System.Drawing.Size(224, 40)
Me.lbl1.TabIndex = 1
Me.lbl1.Text = "Label1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(528, 357)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lbl1,
Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

#Region " WinAPI Calls "
Private Declare Function CreatePolygonRgn& Lib "gdi32" _
(ByVal lpPoint As Point, _
ByVal nCount As Long, _
ByVal nPolyFillMode As Long)

Private Declare Function PtInRegion& Lib "gdi32" _
(ByVal hRgn As Long, _
ByVal X As Long, _
ByVal Y As Long)

Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long
#End Region

#Region " Variables Decleartion "
Dim RegionHandle1
Dim FirstRegion(7) As Point
#End Region


Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

lbl1.Text = "Mouse is at X: " & e.X & " Y: " & e.Y

'See if the mouse pointer is inside the Region
If PtInRegion(RegionHandle1, e.X, e.Y) < 0 Then

Me.BackColor = System.Drawing.Color.Red

Else

Me.BackColor = System.Drawing.Color.Yellow

End If



End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'First Region

FirstRegion(0).X = 50
FirstRegion(0).Y = 50
FirstRegion(1).X = 100
FirstRegion(1).Y = 125
FirstRegion(2).X = 200
FirstRegion(2).Y = 5
FirstRegion(3).X = 250
FirstRegion(3).Y = 50
FirstRegion(4).X = 300
FirstRegion(4).Y = 100
FirstRegion(5).X = 350
FirstRegion(5).Y = 200
FirstRegion(6).X = 250
FirstRegion(6).Y = 250

RegionHandle1 = CreatePolygonRgn&(FirstRegion(0), 1 +
UBound(FirstRegion), 1)

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

' Create pen.
Dim blackPen As New Pen(Color.Black, 3)
' Create points that define polygon.
Dim point1 As New Point(50, 50)
Dim point2 As New Point(100, 25)
Dim point3 As New Point(200, 5)
Dim point4 As New Point(250, 50)
Dim point5 As New Point(300, 100)
Dim point6 As New Point(350, 200)
Dim point7 As New Point(250, 250)
Dim curvePoints As Point() = {point1, point2, point3, point4, _
point5, point6, point7}
' Draw polygon to screen.
e.Graphics.DrawPolygon(blackPen, curvePoints)




End Sub
End Class

'###################################################################
 
M

Mattias Sjögren

Can someone please tell me how do I use PtInRegion? I have been trying for
few days but no success.

Turn on Option Strict

Change the declarations to use Integers instead of Longs.


But why do you need these APIs anyway? Why arenät you using only GDI+,
the managed Region class and its IsVisible method?



Mattias
 
K

Ken Tucker [MVP]

Hi,

PtInRegion was declared wrong. Updated code to make the region
using GDI+. See below

Private Declare Function PtInRegion Lib "gdi32" _

(ByVal hRgn As IntPtr, _

ByVal X As Integer, _

ByVal Y As Integer) As Integer



Dim ptrRegion As IntPtr

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

Label1.Text = "Mouse is at X: " & e.X & " Y: " & e.Y

If PtInRegion(ptrRegion, e.X, e.Y) Then

Me.BackColor = Color.Red

Else

Me.BackColor = Color.Yellow

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

' Create points that define polygon.

Dim point1 As New Point(50, 50)

Dim point2 As New Point(100, 25)

Dim point3 As New Point(200, 5)

Dim point4 As New Point(250, 50)

Dim point5 As New Point(300, 100)

Dim point6 As New Point(350, 200)

Dim point7 As New Point(250, 250)

Dim myPath As New System.Drawing.Drawing2D.GraphicsPath

Dim rgn As Region

Dim curvePoints As Point() = {point1, point2, point3, point4, _

point5, point6, point7}

'

' Create region using GDI+

'

myPath.AddPolygon(curvePoints)

rgn = New Region(myPath)

'

' Draw Region

'

e.Graphics.DrawPath(Pens.Black, myPath)

'

' Get handle of region

'

ptrRegion = rgn.GetHrgn(e.Graphics)

End Sub



Ken

----------------
 
W

wael

Thanks a lot Mattias for your kind response.

I followed your recommendations, but I get this error:

E:\region\region\Form1.vb(126): Option Strict On disallows implicit
conversions from 'Long' to 'Integer'.

referring to the following line:

RegionHandle1 = CreatePolygonRgn&(FirstRegion(0), 1 + UBound(FirstRegion),
1)
========================================

I added Option Strict On to the first line of the code and changed every
long to integer.

Do you know why I am getting such error?

Also, where is a good place I can get some infos about GDI+???????

Thanks Again,
Wael.
 
W

wael

Thanks Ken for your code and answer

I replaced your code with my code but I get the following error:

E:\region\Form1.vb(115): Type expected.

on the following two lines:
Dim rgn As region

rgn = New region(myPath)

==========================

Why do you think I am getting this error???



Also, where is a good place I can get some infos about GDI+???????

Thanks Again,
Wael.
 
J

james

A good resource, this newsgroup:
microsoft.public.dotnet.framework.drawing

Plus when you post, show a bit more code. Such as where you have defined
FirstRegion etc.
james

wael said:
Thanks a lot Mattias for your kind response.

I followed your recommendations, but I get this error:

E:\region\region\Form1.vb(126): Option Strict On disallows implicit
conversions from 'Long' to 'Integer'.

referring to the following line:

RegionHandle1 = CreatePolygonRgn&(FirstRegion(0), 1 + UBound(FirstRegion),
1)
========================================

I added Option Strict On to the first line of the code and changed every
long to integer.

Do you know why I am getting such error?

Also, where is a good place I can get some infos about GDI+???????

Thanks Again,
Wael.
 
J

Jay B. Harlow [MVP - Outlook]

Wael,
Also, where is a good place I can get some infos about GDI+???????
I find Charles Petzold's book "Programming Microsoft Windows with Microsoft
Visual Basic.NET" to be an excellent resource on programming GDI+ & most
every thing else in Windows Forms. The thing I really like about this book
it is written with the assumption that you are NOT using VS.NET, so you
learn how things really work...

Bob Powell (an MVP who specializes in GDI+) has a FAQ at:

http://www.bobpowell.net/gdiplus_faq.htm

Plus you could to the .NET section of MSDN and find any number of articles:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/netdevanchor.asp

(post if you want more specific links).

Hope this helps
Jay


wael said:
Thanks a lot Mattias for your kind response.

I followed your recommendations, but I get this error:

E:\region\region\Form1.vb(126): Option Strict On disallows implicit
conversions from 'Long' to 'Integer'.

referring to the following line:

RegionHandle1 = CreatePolygonRgn&(FirstRegion(0), 1 + UBound(FirstRegion),
1)
========================================

I added Option Strict On to the first line of the code and changed every
long to integer.

Do you know why I am getting such error?

Also, where is a good place I can get some infos about GDI+???????

Thanks Again,
Wael.
 
K

Ken Tucker [MVP]

Hi,

Try dim rgn as system.drawing.region

Ken
----------------
wael said:
Thanks Ken for your code and answer

I replaced your code with my code but I get the following error:

E:\region\Form1.vb(115): Type expected.

on the following two lines:
Dim rgn As region

rgn = New region(myPath)

==========================

Why do you think I am getting this error???



Also, where is a good place I can get some infos about GDI+???????

Thanks Again,
Wael.
 
W

wael

Thanks for the reply James.

The entire code is listed in the original post at the top of the thread,
thanks again.

james said:
A good resource, this newsgroup:
microsoft.public.dotnet.framework.drawing

Plus when you post, show a bit more code. Such as where you have defined
FirstRegion etc.
james
 
W

wael

Thanks a lot Ken it is working butifuly. Maybe I can go now and get
somthing usful done.
 

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

Top