制作一个小小的游戏

    上周老师布置了一个作业,内容大致如下: 给出中国各省份的地图(为.shp格式),在ArcGIS中用VBA随机生成一个省份,猜此省名并确认。

    用ArcGIS 9.2 加载图层,打开VBA,使用一个textbox 和三个CommandButton ,把三个commandbutton分别命名为“开始”、“确认”、“信息”,分别用于“随机生成省”、“确认省名”、“猜中的信息”。

以下是代码:

Public pav As IActiveView
Dim p1 As Integer
Dim p2 As Integer

Private Sub CommandButton1_Click()

'用于随机生成一个省

Dim pDoc As IMxDocument
Set pDoc = ThisDocument

Dim pMap As IMap
Set pMap = pDoc.FocusMap

Dim i As Integer
i = Int((33 - 0 + 1) * Rnd + 0)

Dim pFeatLyr As IFeatureLayer
Set pFeatLyr = pMap.Layer(0)

Dim pFeatLyrDef As IFeatureLayerDefinition
Set pFeatLyrDef = pFeatLyr
pFeatLyrDef.DefinitionExpression = "FID = " & i
pDoc.ActiveView.Refresh

End Sub

Private Sub CommandButton2_Click()

"在textbox中输入省名,进行确认工作
Dim sText, sAn As String
sText = TextBox1.Text

Dim pDoc As IMxDocument
Set pDoc = ThisDocument

Dim pMap As IMap
Set pMap = pDoc.FocusMap

Dim pFeatureLyr As IFeatureLayer
Set pFeatureLyr = pMap.Layer(0)

Dim pFeatCursor As IFeatureCursor
Dim pFeat As IFeature
Set pFeat = pFeatCursor

Dim Index As Integer
Index = pFeatureLyr.FeatureClass.Fields.FindField("NAME")

Dim pTable As ITable
Set pTable = pFeatureLyr
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)

Dim pRow As IRow
Set pRow = pCursor.NextRow

If sText <> pRow.Value(Index) Then
sAn = pRow.Value(Index)
MsgBox "wrong!  应该是" & sAn
p2 = p2 + 1
Call CommandButton1_Click
Else: MsgBox "right!"
p1 = p1 + 1
p2 = p2 + 1
Call CommandButton1_Click
End If
End Sub

Private Sub CommandButton3_Click()

"进行统计工作

Dim pDoc As IMxDocument
Set pDoc = ThisDocument

Dim pMap As IMap
Set pMap = pDoc.FocusMap


Dim pFeatureLyr As IFeatureLayer
Set pFeatureLyr = pMap.Layer(0)

Dim pFeatCursor As IFeatureCursor
Dim pFeat As IFeature
Set pFeat = pFeatCursor

Dim Index As Integer
Index = pFeatureLyr.FeatureClass.Fields.FindField("NAME")

Dim pTable As ITable
Set pTable = pFeatureLyr
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)

Dim pRow As IRow
Set pRow = pCursor.NextRow


If sText = pRow.Value(Index) Then

 MsgBox "答对了" & p1 & "题.总共作答" & p2 & "题"
 Else
 MsgBox "答对了" & p1 & "题.总共作答" & p2 & "题"
 End If

End Sub

结尾:这个程序很草率,用人工输入省名很麻烦,但技术有限不清楚怎样用一个控件把所有的省份列出进行选择.

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值