C#调用GoogleEarthComAPI开发(三)
1)实现GoogleEarth显示画面随窗口大小改变而改变
2)截获GoogleEarth鼠标消息,实现单击、双击功能;鼠标滚轮缩放现在只能放大!O(∩_∩)O~
3)实现GoogleEarth彩色截图(测试环境:Windows2003Server,Vista与Win7中不可用,XP未测)
下面还是继续看代码:
1、GoogleEarth动态改变大小
1:/// 2:///重新改变GoogleEarth视图的大小 3:/// 4:privatevoidResizeGoogleControl() 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(GEHrender,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,newHeight, 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+=newMouseEventHandler(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,MouseEventArgse) 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=((GERenderPanel)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:DoublePointdp=(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=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("xx双击了GoogleEarth..."); 16: 17:DoublePointdp=((GERenderPanel)Control).DetermineScreenCoordinates(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; 32: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(∩_∩)O~
3、截图
程序中有两种截图功能,一种是GoogleEarth自带的截图功能,只能截取黑白图片;另一种为彩色截图,但是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:sfd.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.PointToScreen(view.Control.Location); 7:intnXSrc=pt.X; 8:intnYSrc=pt.Y; 9: 10:IntPtrhRender=view.GeRenderHWnd; 11: 12:if(hRender!=IntPtr.Zero) 13:{ 14://取得RenderDC 15:IntPtrhRenderDC=NativeMethods.GetWindowDC(hRender); 16://创建hBitmap 17:IntPtrhBitmap=NativeMethods.CreateCompatibleBitmap(hRenderDC,nWidth,nHeight); 18://创建MEMDC 19:IntPtrhMemDC=NativeMethods.CreateCompatibleDC(hRenderDC); 20://将Bitmapwww.wang027.comSelect到MemDC 21: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=newSaveFileDialog()) 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://默认选择JPG 39:if(sfd.FilterIndex==0) 40:{ 41:imgFormat=ImageFormat.Jpeg; 42:} 43://选择PNG 44:else 45:{ 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开发到此为止,请读者继续等待后面的精彩文章… |
|