473,387 Members | 1,573 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,387 software developers and data experts.

PtInRegion always returns true

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

'################################################# ##################
Nov 20 '05 #1
1 1979
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....
"wael" <wa**@gte.net> wrote in message
news:9E*************@nwrddc02.gnilink.net...
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

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

Nov 20 '05 #2

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

Similar topics

4
by: Randell D. | last post by:
Folks, I test if my PHP should process form data by checking if $_POST is an array - However I always find its condition proves true - Why or alternatvily, what other method can one use to check...
11
by: Bob Smith | last post by:
question in subject. does the standard say anything about this or is it platform dependent? any advice much appeciated /B
2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
9
by: fochie | last post by:
Greetings, I'm having a problem when I try to GET a file from my server via xmlhttp when using Mozilla. With IE I can get any type of file fine, get/display headers fine, etc. With Mozilla,...
5
by: Gernold Schneider | last post by:
I have a managed code function calling a unmanaged code function which returns a "bool". Obviously the return value is always "true", if i am not running the code in the debugger. If i run the...
10
by: wael | last post by:
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...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
2
by: Joe Rattz | last post by:
I am trying to create an XmlReader using XmlReader.Create, but it always returns {None}. I have tried several examples from the web, and Create always returns "{None}". Here is my code: ...
11
by: active | last post by:
The code below does much as expected. For example biSize=40 but biClrUsed is always zero!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.