程序效果:
当点击左边的部件图时,会弹出对话框提示你点击了哪一个区域。
程序思路:
用一个图片区域码图作为点击区域的参照,根据XY取得的颜色识别出属于哪一个区域
程序界面如下:

相关代码如下:
| Visual Basic Code |
| Dim ColorArray ( ) As Long Dim ColorCount As Long Private Sub Form_Load ( ) '初始化颜色数量 ColorCount = Picture3.Width / 10 ReDim ColorArray ( 1 To ColorCount ) For X = 1 To ColorCount ColorArray ( X ) = Picture3.Point ( ( X * 10 ) - 5, 5 ) Next X End Sub Private Sub Picture1_MouseDown ( Button As Integer, Shift As Integer, X As Single, Y As Single ) If Button = 1 Then Dim NowColor As Long NowColor = Picture2.Point ( X, Y ) If NowColor <> RGB ( 255, 255, 255 ) Then Dim i As Long For i = 1 To ColorCount If NowColor = ColorArray ( i ) Then ButtonClick i Exit For End If Next i End If End If End Sub Private Sub ButtonClick ( Index As Long ) MsgBox "你点击了第 " & Index & "个按钮", 64, "提示" End Sub |
本程序的源代码可以到以下地址下载:
本教程介绍了一种使用Visual Basic实现的图片区域点击识别方法。通过为不同区域分配特定颜色,程序能够识别用户点击的具体位置,并给出相应的反馈。

7163

被折叠的 条评论
为什么被折叠?



