PtInRegion always returns true

W

wael

Hi every one,

I am experimenting with "PtInRegion" WinAPI, I want my form to be red if the
mouse inside a specific region and yellow if it is out side that region.
The problem is my form turns red as soon as the mouse hits anywhere. Can
someone please tell me what am I doing wrong here??



Thanks in advance.
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

'###################################################################
 
S

Scott

Have you double checked your API declarations? The CreatePolygonRgn and
PtInRegion declarations don't seem to be returning anything. Try the
following declarations....

Declare Function CreatePolygonRgn Lib "gdi32.dll" ( _
ByRef lpPoint As POINTAPI, _
ByVal nCount As Int32, _
ByVal nPolyFillMode As Int32) As Int32

Declare Function PtInRegion Lib "gdi32.dll" ( _
ByVal hRgn As Int32, _
ByVal x As Int32, _
ByVal y As Int32) As Int32

Declare Function DeleteObject Lib "gdi32.dll" ( _
ByVal hObject As Int32) As Int32

I got these from a program called "ApiViewer 2003" that I downloaded off the
web. It gives API declarations in VB6 and VB.NET format....
 

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