C#调用 GoogleEarth COM API开发.doc

上传人:laozhun 文档编号:3926091 上传时间:2023-03-28 格式:DOC 页数:31 大小:3.64MB
返回 下载 相关 举报
C#调用 GoogleEarth COM API开发.doc_第1页
第1页 / 共31页
C#调用 GoogleEarth COM API开发.doc_第2页
第2页 / 共31页
C#调用 GoogleEarth COM API开发.doc_第3页
第3页 / 共31页
C#调用 GoogleEarth COM API开发.doc_第4页
第4页 / 共31页
C#调用 GoogleEarth COM API开发.doc_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《C#调用 GoogleEarth COM API开发.doc》由会员分享,可在线阅读,更多相关《C#调用 GoogleEarth COM API开发.doc(31页珍藏版)》请在三一办公上搜索。

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:else45: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开发到此为止,请读者继续等待后面的精彩文章不好意思,刚才忘记上传源程序了!http:/files

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号