代码名称:Total教学之(API的应用)如何建立一个弹出式菜单

作者/收集者:liuhui_sky from total

开发环境:MapBasic

代码:

前段时间有网友问我怎样建立一个可以多选的Info工具,当时工作太忙只是给他提了一些思路,没能够提供例程,最近我又开始写一个新的程序,刚好也写了一个类似的工具,就贴出来大家一起研究一下。
      这段代码也只是一些核心的代码,并不是代码的全部,因为我的代码是针对一些特定格式的图层写的,所以没有办法贴在这里。但是我可以提供些思路:

1. 建立一个全局的数组(param())了用来存储菜单项
2.建立一个工具
3.当工具在地图上点击的时候调用SearchPoint和SearchInfo两个函数将选中的OBject定位出来
4.将这些定位出来的Object的信息加入前面的全局数组了
5.调用下面的代码显示菜单
6.调用Set Window将选中的信息显示在Info窗口中
Include "mapbasic.def"
Type POINT
    x As Integer
    y As Integer
End Type
'
Define MF_ENABLED &H0
Define MF_SEPARATOR  &H800
Define MF_STRING &H0
Define TPM_RIGHTBUTTON &H2
Define TPM_LEFTALIGN &H0
Define TPM_NONOTIFY &H80
Define TPM_RETURNCMD &H100
Define MF_BYPOSITION &H400
Declare Function CreatePopupMenu Lib "user32" () As Integer
Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal sCaption As String) As Integer
Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Integer, ByVal wFlags As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nReserved As Integer, ByVal hwnd As Integer, nIgnored As Integer) As Integer

Declare Function GetCursorPos Lib "user32" (lpPoint As POINT) As Integer
Declare Function GetForegroundWindow Lib "user32" () As Integer
Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Integer, ByVal wIDItem As Integer, ByVal lpString As String, ByVal nMaxCount As Integer, ByVal wFlag As Integer) As Integer
Dim mSelMenuString As String
Dim param(3) as String

Declare Sub Main
Sub Main
    Dim iMenu As Integer
    Dim hMenu As Integer
    Dim nMenus As Integer
    Dim p As POINT
    Dim Tmp as Integer
    Param(1)="Menu1"
    Param(2)="Menu2"
    Param(3)="Menu3"
    
    Tmp=GetCursorPos(p)
    hMenu=CreatePopupMenu()
    nMenus =3
    For iMenu = 1 To nMenus
    Tmp=AppendMenu(hMenu, MF_STRING + MF_ENABLED, iMenu,param(iMenu))
    Next
    iMenu = TrackPopupMenu(hMenu, TPM_RIGHTBUTTON + TPM_LEFTALIGN + TPM_NONOTIFY + TPM_RETURNCMD, p.x, p.y, 0, GetForegroundWindow(),Tmp)
   Note param(iMenu)
                   
    
End Sub