C#调用 GoogleEarth COM API开发_第1页
C#调用 GoogleEarth COM API开发_第2页
C#调用 GoogleEarth COM API开发_第3页
C#调用 GoogleEarth COM API开发_第4页
C#调用 GoogleEarth COM API开发_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c#调用 googleearth com api开发一、准备google earth提供了个人免费版、plus版、pro版,个人开发只安装个人免费版就可以了,如果需要更多的功能,那么只有每年上交$400购买专业版了到目前为止,googleearth的二次开发接口还比较少,功能太弱,仅仅提供了1.0的类库。googleearth com api参考文档可以在这里找到:c#调用com的参考资料多如牛毛,大家可以到网上搜一下二、例子这里提供一个利用vs2008 + google earth 5.0开发一个“hello world”程序首先,确保已经正确安装ge,打开vs2008 ,新建一个windo

2、ws应用程序项目,在“项目”菜单中选择“添加引用”,切换到“com”选项卡,选择“google earth 1.0 type library”,其实就是google earth的主程序在项目的引用中你可以看到已经添加了一个earthlib的引用,然后我们就可以调用其中的接口进行开发了。下面就是小例子的代码(功能很简单,只有三个,打开ge,然后让ge保存一张截图,然后可以打开这个截图看看。呵呵) 1:/功能:ge实例2:/描述:gecomapi网址:3:/作者:温伟鹏4:/日期:2008-01-205:6:usingsystem;7:usingsystem.collections.generic

3、;8:usingsystem.componentmodel;9:usingsystem.data;10:usingsystem.drawing;11:usingsystem.text;12:usingsystem.windows.forms;13:usingearthlib;14:usingsystem.runtime.interopservices;15:usingsystem.io;16:usingsystem.diagnostics;17:18:namespacegedemo19:20:publicpartialclassform1:form21:22:/23:/标记ge是否已经启动24

4、:/25:privateboolisgestarted=false;26:/27:/定义ge应用程序类28:/29:privateapplicationgeclassgeapp;30:31:publicform1()32:33:initializecomponent();34:35:36:privatevoidbutton1_click(objectsender,eventargse)37:38:startge();39:40:41:/42:/启动ge43:/44:privatevoidstartge()45:46:if(isgestarted)47:48:return;49:50:51:tr

5、y52:53:geapp=(applicationgeclass)marshal.getactiveobject(googleearth.application);54:55:isgestarted=true;56:57:catch58:59:geapp=newapplicationgeclass();60:61:isgestarted=true;62:63:64:65:privatevoidbutton2_click(objectsender,eventargse)66:67:stringssfile=path.combine(application.startuppath,screensh

6、ot.jpg);68:69:try70:71:/quality的取值范围在(0,100)之间,质量越高,quality越大72:geapp.savescreenshot(ssfile,100);73:74:messagebox.show(成功保存截屏图像:+ssfile);75:76:catch(exceptionex)77:78:messagebox.show(保存截屏图像时发生错误:+ex.message);79:80:81:82:privatevoidbutton3_click(objectsender,eventargse)83:84:stringssfile=path.combine

7、(application.startuppath,screenshot.jpg);85:86:if(!file.exists(ssfile)87:88:messagebox.show(未能找到保存的截屏图像!);89:return;90:91:92:process.start(ssfile);93:94:95:privatevoidbutton4_click(objectsender,eventargse)96:97:this.close();98:application.exit();99:100:101:102:继c#调用googleearth com api开发(一),我neil又带给大

8、家第二篇文章。这一篇文章在第一篇的基础上,展示如何调用windows api将googleearth的界面隐藏掉,并将googleearth的地图显示在自定义的窗体上。废话少说,直接上代码。1、主窗口代码:1:/功能:ge实例(二)2:/描述:gecomapi网址:3:/作者:温伟鹏4:/日期:2009-02-085:6:usingsystem;7:usingsystem.collections.generic;8:usingsystem.componentmodel;9:usingsystem.data;10:usingsystem.drawing;11:usingsystem.text;1

9、2:usingsystem.windows.forms;13:usingearthlib;14:15:namespacegedemo16:17:publicpartialclassform2:form18:19:/20:/用来关闭googleearth的消息定义21:/22:staticreadonlyint32wm_quit=0x0012;23:24:privateintptrgehwnd=(intptr)5;25:privateintptrgehrender=(intptr)5;26:privateintptrgeparenthrender=(intptr)5;27:/28:/定义ge应用

10、程序类29:/30:privateapplicationgeclassgeapp;31:32:publicform2()33:34:initializecomponent();35:36:37:protectedoverridevoidonload(eventargse)38:39:base.onload(e);40:41:if(!this.designmode)42:43:geapp=newapplicationgeclass();44:45:gehwnd=(intptr)geapp.getmainhwnd();46:47:nativemethods.setwindowpos(gehwnd,

11、nativemethods.hwnd_bottom,0,0,0,0,48:nativemethods.swp_nosize+nativemethods.swp_hidewindow);49:50:gehrender=(intptr)geapp.getrenderhwnd();51:geparenthrender=(intptr)nativemethods.getparent(gehrender);52:53:nativemethods.movewindow(gehrender,0,0,this.width,this.height,true);54:55:nativemethods.setpar

12、ent(gehrender,this.handle);56:57:58:59:protectedoverridevoidonclosing(canceleventargse)60:61:base.onclosing(e);62:63:nativemethods.postmessage(geapp.getmainhwnd(),wm_quit,0,0);64:65:66:2、nativemethods类定义:1:/功能:windowsapi调用2:/描述:大家可以参照msdn3:/作者:温伟鹏4:/日期:2009-02-085:6:usingsystem;7:usingsystem.collect

13、ions.generic;8:usingsystem.text;9:usingsystem.runtime.interopservices;10:11:namespacegedemo12:13:publicclassnativemethods14:15:dllimport(user32.dll,charset=charset.auto,setlasterror=true)16:publicstaticexternboolsetwindowpos(intptrhwnd,intptrhwndinsertafter,intx,inty,intcx,intcy,uint32uflags);17:18:

14、dllimport(user32.dll,charset=charset.auto)19:publicstaticexternintptrpostmessage(inthwnd,intmsg,intwparam,intlparam);20:21:#region预定义22:23:publicstaticreadonlyintptrhwnd_bottom=newintptr(1);24:publicstaticreadonlyintptrhwnd_notopmost=newintptr(-2);25:publicstaticreadonlyintptrhwnd_top=newintptr(0);2

15、6:publicstaticreadonlyintptrhwnd_topmost=newintptr(-1);27:publicstaticreadonlyuint32swp_nosize=1;28:publicstaticreadonlyuint32swp_nomove=2;29:publicstaticreadonlyuint32swp_nozorder=4;30:publicstaticreadonlyuint32swp_noredraw=8;31:publicstaticreadonlyuint32swp_noactivate=16;32:publicstaticreadonlyuin

16、t32swp_framechanged=32;33:publicstaticreadonlyuint32swp_showwindow=64;34:publicstaticreadonlyuint32swp_hidewindow=128;35:publicstaticreadonlyuint32swp_nocopybits=256;36:publicstaticreadonlyuint32swp_noownerzorder=512;37:publicstaticreadonlyuint32swp_nosendchanging=1024;38:39:#endregion40:41:publicde

17、legateintenumwindowsproc(intptrhwnd,intlparam);42:43:dllimport(user32,charset=charset.auto)44:publicexternstaticintptrgetparent(intptrhwnd);45:46:dllimport(user32,charset=charset.auto)47:publicexternstaticboolmovewindow(intptrhwnd,intx,inty,intnwidth,intnheight,boolbrepaint);48:49:dllimport(user32,c

18、harset=charset.auto)50:publicexternstaticintptrsetparent(intptrhwndchild,intptrhwndnewparent);51:52:dllimport(user32.dll,exactspelling=true,charset=charset.auto)53:publicstaticexternintptrgetwindow(intptrhwnd,intucmd);54:55:publicstaticintgw_child=5;56:publicstaticintgw_hwndnext=2;57:58:3、执行效果:好久没有更

19、新c#调用google earth com api开发系列文章了,今天带给大家的是第三篇,本篇相对于第二篇主要改进了三个方面。1) 实现googleearth显示画面随窗口大小改变而改变2) 截获googleearth鼠标消息,实现单击、双击功能;鼠标滚轮缩放现在只能放大!o(_)o3) 实现googleearth彩色截图(测试环境:windows 2003 server ,vista与win7中不可用,xp未测)下面还是继续看代码:1、googleearth动态改变大小1:/2:/重新改变googleearth视图的大小3:/4:privatevoidresizegooglecontrol(

20、)5:6:nativemethods.sendmessage(gehwnd,(uint)nativemethods.wm_command,nativemethods.wm_paint,0);7:nativemethods.postmessage(gehwnd,nativemethods.wm_qt_paint,0,0);8:9:rectmainrect=newrect();10:nativemethods.getwindowrect(gehwnd,outmainrect);11:clientrect=newrect();12:nativemethods.getclientrect(gehren

21、der,outclientrect);13:14:intoffsetw=mainrect.width-clientrect.width;15:intoffseth=mainrect.height-clientrect.height;16:17:intnewwidth=this.control.width+(int)offsetw;18:intnewheight=this.control.height+(int)offseth;19:20:nativemethods.setwindowpos(gehwnd,nativemethods.hwnd_top,21:0,0,newwidth,newhei

22、ght,22:nativemethods.swp_framechanged);23:24:nativemethods.sendmessage(gehwnd,(uint)nativemethods.wm_command,nativemethods.wm_size,0);25:2、鼠标消息此例子中对于鼠标消息到处理使用了钩子,调用hookapi.dll实现。1:/2:/鼠标钩子3:/4:privatemousehookmousehook;5:6:/设置鼠标钩子7:mousehook=newmousehook();8:mousehook.mouseclick+=newmouseeventhandle

23、r(mousehook_mouseclick);9:mousehook.mousedbclick+=newmouseeventhandler(mousehook_mousedbclick);10:mousehook.mousewheel+=newmouseeventhandler(mousehook_mousewheel);11:/启动鼠标钩子12:mousehook.starthook(hooktype.wh_mouse_ll,0);单击事件:1:/2:/鼠标钩子。鼠标单击事件3:/4:/5:/6:voidmousehook_mouseclick(objectsender,mouseeven

24、targse)7:8:intptrhwnd=nativemethods.windowfrompoint(e.location);9:if(hwnd=this.gerenderhwnd)10:11:pointpoint=this.control.pointtoclient(e.location);12:/如果鼠标击点位置在控件内部,则说明鼠标点击了googleearth视图13:if(this.control.clientrectangle.contains(point)14:15:console.writeline(点击了googleearth.);16:17:doublepointdp=(g

25、erenderpanel)control).determinescreencoordinates(point.x,point.y);18:19:parameterizedthreadstartpts=newparameterizedthreadstart(showmouseclickpoint);20:21:threadthread=newthread(pts);22:thread.start(dp);23:24:25:26:27:28:protectedvoidshowmouseclickpoint(objectobj)29:30:/thread.sleep(20);31:doublepoi

26、ntdp=(doublepoint)obj;32:pointonterraingepge=geapp.getpointonterrainfromscreencoords(dp.x,dp.y);33:console.writeline(鼠标点击了:lnt=+pge.longitude.tostring()34:+;lat=+pge.latitude.tostring();35:双击事件:1:/2:/鼠标钩子。鼠标双击事件3:/4:/5:/6:voidmousehook_mousedbclick(objectsender,mouseeventargse)7:8:intptrhwnd=nativem

27、ethods.windowfrompoint(e.location);9:if(hwnd=this.gerenderhwnd)10:11:pointpoint=this.control.pointtoclient(e.location);12:/如果鼠标击点位置在控件内部,则说明鼠标点击了googleearth视图13:if(this.control.clientrectangle.contains(point)14:15:console.writeline(xx双击了googleearth.);16:17:doublepointdp=(gerenderpanel)control).deter

28、minescreencoordinates(point.x,point.y);18:19:parameterizedthreadstartpts=newparameterizedthreadstart(showmousedbclickpoint);20:21:threadthread=newthread(pts);22:thread.start(dp);23:24:25:26:27:28:protectedvoidshowmousedbclickpoint(objectobj)29:30:/thread.sleep(20);31:doublepointdp=(doublepoint)obj;3

29、2:pointonterraingepge=geapp.getpointonterrainfromscreencoords(dp.x,dp.y);33:console.writeline(xx鼠标双击了:lnt=+pge.longitude.tostring()34:+;lat=+pge.latitude.tostring();35:36:messagebox.show(我还是出来一下吧!省得你不知道你已经双击了鼠标!);37:这两处代码还比较简陋,比如未添加主窗口焦点检测,相信读者可以自行添加。o(_)o3、截图程序中有两种截图功能,一种是googleearth自带的截图功能,只能截取黑白图

30、片;另一种为彩色截图,但是vista以上操作系统不支持,还未找到合适的方法实现vista与win7兼容。1) googleearth自带截图功能:1:geviewcontentview=getgeview();2:3:if(view!=null)4:5:applicationgege=view.geapplication;6:if(ge!=null&ge.isinitialized()0)7:8:using(savefiledialogsfd=newsavefiledialog()9:10:sfd.filter=jpg图片|*.jpg;11:sfd.addextension=true;12:s

31、fd.checkpathexists=true;13:sfd.title=保存googleearth截图;14:15:if(sfd.showdialog()=dialogresult.ok)16:17:ge.savescreenshot(sfd.filename,100);18:19:20:21:2) 彩色截图:1:geviewcontentview=getgeview();2:if(view!=null)3:4:intnwidth=view.control.width;5:intnheight=view.control.height;6:pointpt=view.control.pointt

32、oscreen(view.control.location);7:intnxsrc=pt.x;8:intnysrc=pt.y;9:10:intptrhrender=view.gerenderhwnd;11:12:if(hrender!=intptr.zero)13:14:/取得renderdc15:intptrhrenderdc=nativemethods.getwindowdc(hrender);16:/创建hbitmap17:intptrhbitmap=nativemethods.createcompatiblebitmap(hrenderdc,nwidth,nheight);18:/创建

33、memdc19:intptrhmemdc=nativemethods.createcompatibledc(hrenderdc);20:/将bitmapselect到memdc21:nativemethods.selectobject(hmemdc,hbitmap);22:/直接拷屏23:nativemethods.bitblt(hmemdc,0,0,nwidth,nheight,24:hrenderdc,0,0,13369376);25:26:using(bitmapbmp=bitmap.fromhbitmap(hbitmap)27:28:using(savefiledialogsfd=ne

34、wsavefiledialog()29:30:sfd.filter=jpg图片|*.jpg|png图片|*.png;31:sfd.addextension=true;32:sfd.checkpathexists=true;33:sfd.title=保存googleearth截图;34:35:if(sfd.showdialog()=dialogresult.ok)36:37:imageformatimgformat=null;38:/默认选择jpg39:if(sfd.filterindex=0)40:41:imgformat=imageformat.jpeg;42:43:/选择png44:els

35、e45:46:imgformat=imageformat.png;47:48:bmp.save(sfd.filename,imgformat);49:50:51:52:/销毁资源53:nativemethods.deletedc(hrenderdc);54:nativemethods.deletedc(hmemdc);55:56:ok,这篇ge开发到此为止,请读者继续等待后面的精彩文章不好意思,刚才忘记上传源程序了!图片看不清楚?请点击这里查看原图(大图)。图片看不清楚?请点击这里查看原图(大图)。篇继续介绍google earth com api开发的基础知识,相对第三篇的改进如下:1)增加

36、鼠标滚轮支持,可以实现放大、缩小。此功能利用上一篇提供的hookapi.dll实现2)读取placemarks(google earth界面中的位置)并显示、隐藏3)读取所有图层,显示并隐藏下面,继续放代码:1、鼠标滚轮事件,实现放大、缩小1: . 2: / 放大 3: private const long zoomin = 0x00780000; 4: / 缩小 5: private const long zoomout = 0xff880000; 6: . 7: mousehook.mousewheel += new mouseeventhandler(mousehook_mousewhe

37、el); 8: . 9: / 10: / 鼠标钩子。鼠标滚动事件 11: / 12: / 13: / 14: void mousehook_mousewheel(object sender, mouseeventargs e) 15: 16: intptr hwnd = nativemethods.windowfrompoint(e.location); 17: if (hwnd = this.gerenderhwnd) 18: 19: point point = this.control.pointtoclient(e.location); 20: / 如果鼠标位置在控件内部,则说明鼠标在g

38、oogleearth视图范围内进行了滚动 21: if (this.control.clientrectangle.contains(point) 22: 23: nativemethods.postmessage(gerenderhwnd, (int)wm_mouse.wm_mousewheel, e.delta = 120 ? zoomin : zoomout, 0); 24: 25: 待添加的隐藏文字内容2 26: 2、读取placemarksgoogle earth com api中提供了两个读取placemarks的函数。一个是gettemporaryplaces(),用来读取临时位

39、置;另一个是getmyplaces(),用来读取自定义位置,即googleearth中显示的“我的位置”。呵呵 1: . 2: / 3: / 显示位置“placemarks”。位置分为两种,一种时temporaryplaces,另一种为myplaces 4: / 5: protected void showplaces() 6: 7: thread.sleep(500); 8: / 获取myplaces 9: featurege myplace = geapp.getmyplaces(); 10: / 获取临时位置 11: featurege temporaryplace = geapp.ge

40、ttemporaryplaces(); 12: / 13: list places = new list(); 14: places.add(myplace); 15: places.add(temporaryplace); 16: 17: / 获取工具面板 18: getoolpad toolpad = gettoolpad(); 19: / 显示所有位置 20: toolpad.showplaces(places); 21: 22: . 23: / 24: / 显示所有placemarks(位置) 25: / 26: / 27: public void showplaces(list places) 28: 29: if (this.invokerequired) 30: 31: action method = delegate 32: internalshowplaces(places); 33: ; 34: 35: try 36: 37: this.invoke(method); 38: 39: catch 40: 41: else 42: 43: internalshowplaces(places); 44: 45: 46: / 47: / 显示所有pl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论