讨论我的一个课题,关于VB.net的
我现在有一个课题,要达到的目的就是要让用户随意载入一张图片,然后用鼠标勾画出选区来(选区可以使各种的图形),然后保存起来这些选区,用户自己定义每个选区连接什么文件。每个选区会有一些不用的事件响应,比如不同的选区就要连接不用的数据库中的内容,这究竟该怎么做呢?大家可不可以给些意见,让我能学习一下。。。。我觉得这个课题很有意思阿,现在我已经可以建立选取了,用createpath来建立,然后其他就没有思路了,不知道该如何让这个选区有效。
希望大家可以多多指教阿 --------------------编程问答-------------------- 随便写了一段代码,看看行不?
--------------------编程问答-------------------- 这个问题就是一个热点问题,想网页上点击图片不通的地方连接不通的页面一样
Public Class Form1
'选区属性,根据情况可自行添加
Private Structure PathProperty
Public path As System.Drawing.Drawing2D.GraphicsPath
Public db As String
Public Sub New(ByVal _path As System.Drawing.Drawing2D.GraphicsPath, ByVal _db As String)
path = _path
_db = db
End Sub
End Structure
Dim paths As New ArrayList
Dim path As New System.Drawing.Drawing2D.GraphicsPath
Dim p As Point = Nothing
Dim test As PathProperty = Nothing
Private Sub Form1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
path.CloseAllFigures()
paths.Add(New PathProperty(path, "Not Set"))
p = Nothing
path = New System.Drawing.Drawing2D.GraphicsPath
Me.Refresh()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then '鼠标左键进行选区绘制
Dim p1 As New Point(e.X, e.Y)
If p <> Nothing Then path.AddLine(p, p1)
p = p1
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then '鼠标右键测试选区
test = Nothing
For Each pp As PathProperty In paths
If pp.path.IsVisible(e.X, e.Y) Then
test = pp
Exit For
End If
Next
If Not test.Equals(Nothing) Then
Me.CreateGraphics.DrawPath(Pens.Red, test.path)
If test.db = "" Then
paths.Remove(test)
test.db = InputBox(" 请设置该选区对应的数据库:", " 设置 ", "Not set") '此处可以对选区进行一些设置
paths.Add(test)
Me.Refresh()
Else
MsgBox(String.Format("右键点击选区的数据库:{0}", test.db))
End If
End If
End If
End Sub
'绘制所有选区
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
For Each pp As PathProperty In paths
If pp.Equals(test) Then
e.Graphics.DrawPath(Pens.Red, pp.path)
Else
e.Graphics.DrawPath(Pens.Black, pp.path)
End If
Next
End Sub
End Class
我以前写过一个VB6程序,就是在图片随意创建一系列点,这些点构成一个密闭的图形,然后鼠标移动到这个图形上,系统就能侦测到,如果在这个例子扩充一下就是你的要求了
Form代码:
Dim RgnPt() As POINTAPI '热点数组
Dim RgnObject As Long: Dim RgnCount As Long: Dim Flag As Integer
Private Sub P1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Flag = -1 Then '必须在按下"创建热点"后才有效,且在按下"使用热点"后失效
If Button = 1 Then '必须按下鼠标左键才有效
RgnCount = RgnCount + 1
ReDim Preserve RgnPt(RgnCount)
RgnPt(RgnCount).X = X
RgnPt(RgnCount).Y = Y
P1.DrawWidth = 5
P1.PSet (X, Y), RGB(255, 0, 0)
End If
End If
End Sub
Private Sub P1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If PtInRegion(RgnObject, X, Y) = 1 Then
Shape1.BorderColor = RGB(0, 255, 0)
Shape1.FillColor = RGB(0, 255, 0)
Else
Shape1.BorderColor = RGB(255, 0, 0)
Shape1.FillColor = RGB(255, 0, 0)
End If
End Sub
Private Sub CrtRgn_Click()
Dim Tmp As Long
RgnCount = 0
P1.Cls
Tmp = DeleteObject(RgnObject)
Flag = -1
Shape1.Visible = False
End Sub
Private Sub DspRgn_Click()
Dim I As Long
P1.DrawWidth = 2
For I = 1 To UBound(RgnPt) - 1
P1.Line (RgnPt(I).X, RgnPt(I).Y)-(RgnPt(I + 1).X, RgnPt(I + 1).Y), RGB(0, 255, 0)
Next
P1.Line (RgnPt(UBound(RgnPt)).X, RgnPt(UBound(RgnPt)).Y)-(RgnPt(1).X, RgnPt(1).Y), RGB(0, 255, 0)
RgnObject = CreatePolygonRgn(RgnPt(1), RgnCount, 1)
Flag = 1
Shape1.Visible = True
End Sub
Private Sub Form_Load()
ScaleMode = 3
P1.ScaleMode = 3
End Sub
Module代码:
Type POINTAPI
X As Long
Y As Long
End Type
Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal X As Long, ByVal Y As Long) As Long
'CreatePolygonRgn( )函数是用来创建一个图形热点对象,lpPoint参数为形成图形热点区的第一个边界点坐标,nCount参数指出了边界点的个数(如nCount<3时,将无法形成一个封闭区域),nPolyFillMode参数指定填充模式,该函数返回图形热点对象(Long型)
'DeleteObject( )函数用来删除一个由hObject参数指定的对象
'PtInRegion( )函数用来判断某坐标(由参数x、y指出)是否在hRgn图形热 点对象指出的热点区内,若是返回1,否则返回0。
这个使用VB6编写的,如果改成.net代码可能更简单。
反正给你条思路,多条出路! --------------------编程问答--------------------
Dim RgnPt() As POINTAPI '热点数组
Dim RgnObject As Long: Dim RgnCount As Long: Dim Flag As Integer
Private Sub P1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Flag = -1 Then '必须在按下"创建热点"后才有效,且在按下"使用热点"后失效
If Button = 1 Then '必须按下鼠标左键才有效
RgnCount = RgnCount + 1
ReDim Preserve RgnPt(RgnCount)
RgnPt(RgnCount).X = X
RgnPt(RgnCount).Y = Y
P1.DrawWidth = 5
P1.PSet (X, Y), RGB(255, 0, 0)
End If
End If
End Sub
Private Sub P1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If PtInRegion(RgnObject, X, Y) = 1 Then
Shape1.BorderColor = RGB(0, 255, 0)
Shape1.FillColor = RGB(0, 255, 0)
Else
Shape1.BorderColor = RGB(255, 0, 0)
Shape1.FillColor = RGB(255, 0, 0)
End If
End Sub
Private Sub CrtRgn_Click()
Dim Tmp As Long
RgnCount = 0
P1.Cls
Tmp = DeleteObject(RgnObject)
Flag = -1
Shape1.Visible = False
End Sub
Private Sub DspRgn_Click()
Dim I As Long
P1.DrawWidth = 2
For I = 1 To UBound(RgnPt) - 1
P1.Line (RgnPt(I).X, RgnPt(I).Y)-(RgnPt(I + 1).X, RgnPt(I + 1).Y), RGB(0, 255, 0)
Next
P1.Line (RgnPt(UBound(RgnPt)).X, RgnPt(UBound(RgnPt)).Y)-(RgnPt(1).X, RgnPt(1).Y), RGB(0, 255, 0)
RgnObject = CreatePolygonRgn(RgnPt(1), RgnCount, 1)
Flag = 1
Shape1.Visible = True
End Sub
Private Sub Form_Load()
ScaleMode = 3
P1.ScaleMode = 3
End Sub
Type POINTAPI
X As Long
Y As Long
End Type
Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal X As Long, ByVal Y As Long) As Long
'CreatePolygonRgn( )函数是用来创建一个图形热点对象,lpPoint参数为形成图形热点区的第一个边界点坐标,nCount参数指出了边界点的个数(如nCount<3时,将无法形成一个封闭区域),nPolyFillMode参数指定填充模式,该函数返回图形热点对象(Long型)
'DeleteObject( )函数用来删除一个由hObject参数指定的对象
'PtInRegion( )函数用来判断某坐标(由参数x、y指出)是否在hRgn图形热 点对象指出的热点区内,若是返回1,否则返回0。
这样看得清楚,不知道是不是这样贴代码? --------------------编程问答-------------------- 学习了!
--------------------编程问答-------------------- 谢谢好心人啊,这么多人回复,加强了我的学习热情啊。。。 --------------------编程问答-------------------- 大家一起,反正自己会的,别人想知道的,我们就不遗余力帮助他们
补充:.NET技术 , VB.NET