PtInRegion如何在VB.NET下工作 [英] How PtInRegion works under VB.NET
问题描述
大家好
有人可以告诉我如何使用PtInRegion?我一直在尝试
几天但没有成功。
我是否必须链接任何额外的库或.net组件以便我的代码
要运行吗?
我正在运行XP专业版和VS.Net版。 2002
我在下面列出了我的最新代码尝试
谢谢,
Wael
''############################################## ### ##################
公共类Form1
继承System.Windows.Forms .Form
#Region" Windows窗体设计器生成的代码
Public Sub New()
MyBase.New()
' 'Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化
< br $>
结束子
''表格覆盖处理以清理组件列表。
受保护的重载覆盖子处理(ByVal处理为布尔)
如果处置那么
如果不是(组件什么都没有)那么
components.Dispose()
结束如果
结束如果
MyBase.Dispose(处置)
结束子
'' Windows窗体设计器要求
私有组件As System.ComponentModel.IContainer
''注意:Windows窗体需要以下过程>
设计师
''可以使用Windows窗体设计器修改它。
'不要使用代码编辑器修改它。
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 =新的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 (假)
结束子
#End Region
#Region" WinAPI呼叫
私有声明功能CreatePolygonRgn& Libgdi32 _
(ByVal lpPoint As Point,_
ByVal nCount As Long,_
ByVal nPolyFillMode As Long)
>
私人声明功能PtInRegion& Libgdi32 _
(ByVal hRgn As Long,_
ByVal X As Long,_
ByVal Y As Long)
>
私有声明函数DeleteObject Lib" gdi32" _
(ByVal hObject As Long)长期
#End Region
#Region"变量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在X: &安培; e.X& " Y: &安培; e.Y
''查看鼠标指针是否在区域内
如果PtInRegion(RegionHandle1,e.X,e.Y)< 0然后
Me.BackColor = System.Drawing.Color.Red
否则
Me.BackColor = System.Drawing.Color.Yellow
结束如果
结束子
Private Sub Form1_Load(ByVal sender As Object,ByVal e As
System.EventArgs)处理MyBase.Load
''第一区域
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
Priva te Sub Form1_Paint(ByVal sender As Object,ByVal e As
System.Windows.Forms.PaintEventArgs)Handles MyBase.Paint
''创建笔。
Dim blackPen As New Pen(Color.Black,3)
''创建定义多边形的点。
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}
''将多边形绘制到屏幕上。
e.Graphics.DrawPolygon(blackPen,curvePoints)
结束Sub
结束班
''######################### ##########################################
>可以请告诉我如何使用PtInRegion?我一直在尝试几天但没有成功。
打开Option Strict
更改声明以使用Integers而不是Longs。
但是为什么还需要这些API?为什么你不使用GDI +,
管理的Region类及其IsVisible方法?
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
PtInRegion被宣布错误。更新了代码,使用GDI +创建了区域
。见下文
私有声明功能PtInRegion Lib" gdi32" _ $ />
(ByVal hRgn作为IntPtr,_
ByVal X作为整数,_
ByVal Y As Integer)作为整数
Dim ptrRegion作为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: &安培; eY
如果是PtInRegion(ptrRegion,eX,eY)那么
Me.BackColor = Color.Red
Else
Me.BackColor = Color.Yellow
结束如果
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object,ByVal e As
System.Windows.Forms.PaintEventArgs)处理MyBase .Paint
''创建定义多边形的点。
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.Dr awing2D.GraphicsPath
Dim rgn As Region
Dim curvePoints As Point()= {point1,point2,point3,point4,_
point5,point6,point7}
''
''使用GDI +创建区域
''
myPath.AddPolygon(curvePoints)
rgn =新区域(myPath)
''
''抽奖区
''
>
e.Graphics.DrawPath(Pens.Black,myPath)
''
''获取区域的句柄
''
ptrRegion = rgn.GetHrgn(e.Graphics)
End Sub
肯
----------------
我按照你的建议,但我收到了这个错误:
E :\ region \ region \ Form1.vb(126):Option Strict On禁止impl icit
从''Long''转换为''Integer''。
参考以下行:
RegionHandle1 = CreatePolygonRgn&(FirstRegion(0),1 + UBound(FirstRegion),
1)
============ ============================
我在第一行添加了Option Strict On代码并将每个
更改为整数。
你知道为什么我会收到这样的错误吗?
另外,哪里是个好地方我可以得到一些关于GDI +的信息???????
再次感谢,
Wael。
" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道
新闻:OQ ************* @ TK2MSFTNGP11.phx.gbl ...
有人可以告诉我如何使用PtInRegion?我一直在尝试
但是没有成功。
打开Option Strict
更改声明以使用Integers而不是Longs。 />
但是为什么你还需要这些API呢?你为什么不使用GDI +,
管理的Region类及其IsVisible方法?
Mattias
-
Mattias Sj?gren [ MVP] mattias @ mvps.org
http://www.msjogren.net/ dotnet / | http://www.dotnetinterop.com
请仅回复新闻组。
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
''################################################# ##################
>Can someone please tell me how do I use PtInRegion? I have been trying forfew 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
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
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
----------------
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.
"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:OQ*************@TK2MSFTNGP11.phx.gbl...Can someone please tell me how do I use PtInRegion? I have been trying forfew 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
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
这篇关于PtInRegion如何在VB.NET下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!