我正在MapBasic中开发一个应用程序,它在.NET程序集库中调用方法
这就是我如何在我的MapBasice环境中声明该方法。
Declare Method showMainWindow Class "mynamespace.AldebaranInterface" Lib "Aldebaran.dll" ()
Sub ShowWindow
Call showMainWindow()
End Sub
Sub formEventHandler
'Reacting to some button click. I need to call this Sub from somewhere in mynamespace.AldebaranInterface
End Sub我需要的是从我的MapBasic应用程序中从.NET C#代码中回调一些子程序或函数。假设当用户单击我的MapBasic表单中的某个按钮时,执行部分.NET代码。
有办法吗?如果是的话。我怎样才能做到这一点?任何帮助都将不胜感激。
发布于 2018-11-27 13:15:10
我不知道你的.dll应该做什么,我猜它是一个表单,因为你需要你的地图基本代码来对按钮点击做出反应。
为了向mapbasic发送命令,需要创建Mapinfo的COM对象的实例。这里是关于如何做到这一点的链接。我个人使用第二方法。
因此,在创建类之后:
public class Mapinfo
{
private object mapinfoinstance;
public Mapinfo(object mapinfo)
{
this. mapinfoinstance = mapinfo;
}
public static Mapinfo CreateInstance()
{
Type mapinfotype = Type.GetTypeFromProgID("Mapinfo.Application");
object instance = Activator.CreateInstance(mapinfotype);
return new Mapinfo(instance);
}
public void Do(string command)
{
parameter[0] = command;
mapinfoType.InvokeMember("Do",
BindingFlags.InvokeMethod,
null, instance, parameter);
}
public string Eval(string command)
{
parameter[0] = command;
return (string)mapinfoType.InvokeMember("Eval", BindingFlags.InvokeMethod,
null,instance,parameter);
}
},您需要添加按钮单击事件:
appMapInfo = Mapinfo.CreateInstance();
//It's good idea to pass this path string from your MapBasic code
//as method parameter in your .dll
string appMapInfoFilePath = @"D:\YourMBXPath\YourMBX.MBX";
//argumet you want to pass to MapBasic code
string argForMapBasic = ...;
string runCommand;
string subCommand;
subCommand = "dim i_chan_num as integer i_chan_num = DDEInitiate(\"\"MapInfo\"\",\"\"" + appMapInfoFilePath + "\"\")";
subCommand = subCommand + " DDEExecute i_chan_num, \"\"" + argForMapBasic + "\"\" DDETerminate i_chan_num";
runCommand = String.Format("Run Command \"{0}\"", subCommand);
appMapInfo.Do(runCommand);现在,在RemoteMsgHandler端,您应该创建Sub MapBasic (MapBasic引用:在远程应用程序发送执行消息时调用的保留过程名称)。
Sub RemoteMsgHandler
Dim command as String
'command - string passed from your C# code (string argForMapBasic)
command = CommandInfo(CMD_INFO_MSG)
'pass a command to your procedure
Call yourProcedure(command)
End Sub https://stackoverflow.com/questions/53454116
复制相似问题