原文地址:http://www.cnblogs.com/wpwen/archive/2009/05/22/1486892.html
好久没有更新《C#调用Google Earth Com API开发》系列文章了,今天带给大家的是第三篇,本篇相对于第二篇主要改进了三个方面。
1) 实现GoogleEarth显示画面随窗口大小改变而改变
2) 截获GoogleEarth鼠标消息,实现单击、双击功能;鼠标滚轮缩放现在只能放大!O(∩_∩)O~
3) 实现GoogleEarth彩色截图(测试环境:Windows 2003 Server ,Vista与Win7中不可用,XP未测)
下面还是继续看代码:
1、GoogleEarth动态改变大小
/// <summary>/// 重新改变GoogleEarth视图的大小/// </summary>void ResizeGoogleControl()5: {uint)NativeMethods.WM_COMMAND, NativeMethods.WM_PAINT, 0);7: NativeMethods.PostMessage(GEHWnd, NativeMethods.WM_QT_PAINT, 0, 0);8:new RECT();out mainRect);new RECT();out clientRect);13:int offsetW = mainRect.Width - clientRect.Width;int offsetH = mainRect.Height - clientRect.Height;16:int)offsetW;int)offsetH;19:20: NativeMethods.SetWindowPos(GEHWnd, NativeMethods.HWND_TOP,21: 0, 0, newWidth, newHeight,22: NativeMethods.SWP_FRAMECHANGED);23:uint)NativeMethods.WM_COMMAND, NativeMethods.WM_SIZE, 0);25: }
2、鼠标消息
此例子中对于鼠标消息到处理使用了钩子,调用HookAPI.dll实现。
1: /// <summary>2: /// 鼠标钩子3: /// </summary>4: private MouseHook mouseHook;5:6: // 设置鼠标钩子7: mouseHook = new MouseHook();8: mouseHook.MouseClick += new MouseEventHandler(mouseHook_MouseClick);9: mouseHook.MouseDbClick += new MouseEventHandler(mouseHook_MouseDbClick);10: mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel);11: // 启动鼠标钩子12: mouseHook.StartHook(HookType.WH_MOUSE_LL, 0);
单击事件:
/// <summary>/// 鼠标钩子。鼠标单击事件/// </summary>object sender, MouseEventArgs e)7: {8: IntPtr hWnd = NativeMethods.WindowFromPoint(e.Location);this.GeRenderHWnd)10: {this.Control.PointToClient(e.Location);// 如果鼠标击点位置在控件内部,则说明鼠标点击了GoogleEarth视图this.Control.ClientRectangle.Contains(point))14: {);16:17: DoublePoint dp = ((GERenderPanel)Control).DetermineScreenCoordinates(point.X, point.Y);18:new ParameterizedThreadStart(ShowMouseClickPoint);20:new Thread(pts);22: thread.Start(dp);23:24: }25: }26: }27:object obj)29: {//Thread.Sleep(20);31: DoublePoint dp = (DoublePoint)obj;32: PointOnTerrainGE pGe = GeApp.GetPointOnTerrainFromScreenCoords(dp.X, dp.Y);+ pGe.Longitude.ToString()+ pGe.Latitude.ToString());35: }
双击事件:
/// <summary>/// 鼠标钩子。鼠标双击事件/// </summary>object sender, MouseEventArgs e)7: {8: IntPtr hWnd = NativeMethods.WindowFromPoint(e.Location);this.GeRenderHWnd)10: {this.Control.PointToClient(e.Location);// 如果鼠标击点位置在控件内部,则说明鼠标点击了GoogleEarth视图this.Control.ClientRectangle.Contains(point))14: {);16:17: DoublePoint dp = ((GERenderPanel)Control).DetermineScreenCoordinates(point.X, point.Y);18:new ParameterizedThreadStart(ShowMouseDbClickPoint);20:new Thread(pts);22: thread.Start(dp);23:24: }25: }26: }27:object obj)29: {//Thread.Sleep(20);31: DoublePoint dp = (DoublePoint)obj;32: PointOnTerrainGE pGe = GeApp.GetPointOnTerrainFromScreenCoords(dp.X, dp.Y);+ pGe.Longitude.ToString()+ pGe.Latitude.ToString());35:);37: }
这两处代码还比较简陋,比如未添加主窗口焦点检测,相信读者可以自行添加。O(∩_∩)O~
3、截图
程序中有两种截图功能,一种是GoogleEarth自带的截图功能,只能截取黑白图片;另一种为彩色截图,但是Vista以上操作系统不支持,还未找到合适的方法实现Vista与Win7兼容。
1) GoogleEarth自带截图功能:
1: GEViewContent view = GetGEView();2:null)4: {5: ApplicationGE ge = view.GeApplication;null && ge.IsInitialized() > 0)7: {new SaveFileDialog())9: {;true;true;;14:if (sfd.ShowDialog() == DialogResult.OK)16: {17: ge.SaveScreenShot(sfd.FileName, 100);18: }19: }20: }21: }2) 彩色截图:
1: GEViewContent view = GetGEView();null)3: {int nWidth = view.Control.Width;int nHeight = view.Control.Height;6: Point pt = view.Control.PointToScreen(view.Control.Location);int nXSrc = pt.X;int nYSrc = pt.Y;9:10: IntPtr hRender = view.GeRenderHWnd;11:if (hRender != IntPtr.Zero)13: {// 取得Render DC15: IntPtr hRenderDC = NativeMethods.GetWindowDC(hRender);// 创建hBitmap17: IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hRenderDC, nWidth, nHeight);// 创建MEM DC19: IntPtr hMemDC = NativeMethods.CreateCompatibleDC(hRenderDC);// 将Bitmap Select到MemDC21: NativeMethods.SelectObject(hMemDC, hBitmap);// 直接拷屏23: NativeMethods.BitBlt(hMemDC, 0, 0, nWidth, nHeight,24: hRenderDC, 0, 0, 13369376);25:using(Bitmap bmp = Bitmap.FromHbitmap(hBitmap))27: {new SaveFileDialog())29: {;true;true;;34:if (sfd.ShowDialog() == DialogResult.OK)36: {null;// 默认选择JPGif (sfd.FilterIndex == 0)40: {41: imgFormat = ImageFormat.Jpeg;42: }// 选择PNGelse45: {46: imgFormat = ImageFormat.Png;47: }48: bmp.Save(sfd.FileName, imgFormat);49: }50: }51://销毁资源53: NativeMethods.DeleteDC(hRenderDC);54: NativeMethods.DeleteDC(hMemDC);55: }56: }
OK,这篇GE开发到此为止,请读者继续等待后面的精彩文章…
不好意思,刚才忘记上传源程序了!
https://files.cnblogs.com/wpwen/GEDemo_2009-05-22.rar