• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#调用GoogleEarthComAPI开发(三)(转载)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

原文地址: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 DC
  15:         IntPtr hRenderDC = NativeMethods.GetWindowDC(hRender);
// 创建hBitmap
  17:         IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hRenderDC, nWidth, nHeight);
// 创建MEM DC
  19:         IntPtr hMemDC = NativeMethods.CreateCompatibleDC(hRenderDC);
// 将Bitmap Select到MemDC
  21:         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;
// 默认选择JPG
if (sfd.FilterIndex == 0)
  40:                     {
  41:                         imgFormat = ImageFormat.Jpeg;
  42:                     }
// 选择PNG
else
  45:                     {
  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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#获取日期的星期名称发布时间:2022-07-13
下一篇:
C#ThreadPool类(线程池)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap