C#调用 Google Earth COM API 开发(一)
2009-07-18 来自:www.cnblogs.com 字体大小:【大 中 小】
摘要:Google Earth 提供了个人免费版、Plus 版、Pro 版,个人开发只安装个人免费版就
可以了,如果需要更多的功能,那么只有每年上交$400 购买专业版了。
一、准备
Google Earth 提供了个人免费版、Plus 版、Pro 版,个人开发只安装个人免费版就可以了,
如果需要更多的功能,那么只有每年上交$400 购买专业版了
到目前为止,GoogleEarth 的二次开发接口还比较少,功能太弱,仅仅提供了 1.0 的类库。
GoogleEarth COM API 参考文档可以在这里找到:http://earth.google.com/comapi/index.
html
C#调用 COM 的参考资料多如牛毛,大家可以到网上搜一下
二、例子
这里提供一个利用 VS2008 + Google Earth 5.0 开发一个“Hello world”程序
首先,确保已经正确安装 GE,打开 VS2008 ,新建一个 Windows 应用程序项目,在“项目”
菜单中选择“添加引用…”,切换到“COM”选项卡,选择“Google Earth 1.0 Type Libr
ary”,其实就是 Google Earth 的主程序
在项目的引用中你可以看到已经添加了一个 EARTHLib 的引用,然后我们就可以调用其中的
接口进行开发了。
下面就是小例子的代码(功能很简单,只有三个,打开 GE,然后让 GE 保存一张截图,然后
可以打开这个截图看看。呵呵)
1: // 功能:GE 实例
2: // 描述:GE COM API 网址:http://earth.google.com/comapi/index.
html
3: // 作者:温伟鹏
4: // 日期:2008-01-20
5:
6: using System;
7: using System.Collections.Generic;
8: using System.ComponentModel;
9: using System.Data;
public partial class Form1 : Form
{
///
/// 标记 GE 是否已经启动
///
private bool isGeStarted = false;
///
/// 定义 GE 应用程序类
///
private ApplicationGEClass GeApp;
10: using System.Drawing;
11: using System.Text;
12: using System.Windows.Forms;
13: using EARTHLib;
14: using System.Runtime.InteropServices;
15: using System.IO;
16: using System.Diagnostics;
17:
18: namespace GEDemo
19: {
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
///
/// 启动 GE
///
private void StartGE()
{
public Form1()
{
InitializeComponent();
if (isGeStarted)
{
return;
}
try
}
}
private void button1_Click(object sender, EventArgs e)
{
StartGE();
52:
53:
{
GeApp = (ApplicationGEClass)Marshal.GetActiveOb
ject("GoogleEarth.Application");
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
isGeStarted = true;
}
catch
{
GeApp = new ApplicationGEClass();
isGeStarted = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
string ssFile = Path.Combine(Application.StartupPat
h, "ScreenShot.jpg");
68:
69:
70:
71:
lity 越大
72:
73:
74:
75:
76:
77:
78:
Message);
79:
80:
81:
82:
83:
84:
try
{
//quality 的取值范围在(0,100)之间,质量越高,qua
GeApp.SaveScreenShot(ssFile, 100);
MessageBox.Show("成功保存截屏图像:" + ssFile);
}
catch(Exception ex)
{
MessageBox.Show("保存截屏图像时发生错误:" + ex.
}
}
private void button3_Click(object sender, EventArgs e)
{
string ssFile = Path.Combine(Application.StartupPat
h, "ScreenShot.jpg");
85:
86:
87:
88:
89:
90:
if (!File.Exists(ssFile))
{
MessageBox.Show("未能找到保存的截屏图像!");
return;
}
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
Process.Start(ssFile);
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
C# 调用 Google Earth Com API 开发(二)
继《C#调用 GoogleEarth Com API 开发(一)》,我 Neil 又带给大家第二篇文章。这一篇
文章在第一篇的基础上,展示如何调用 Windows API 将 GoogleEarth 的界面隐藏掉,并将
GoogleEarth 的地图显示在自定义的窗体上。废话少说,直接上代码。
1、主窗口代码:
1: // 功能:GE 实例(二)
2: // 描述:GE COM API 网址:http://earth.google.com/comapi/index.html
3: // 作者:温伟鹏
4: // 日期:2009-02-08
5:
6: using System;
7: using System.Collections.Generic;
8: using System.ComponentModel;
9: using System.Data;
10: using System.Drawing;
11: using System.Text;
12: using System.Windows.Forms;
13: using EARTHLib;
14:
15: namespace GEDemo
16: {
17:
18:
19:
20:
21:
///
/// 用来关闭 GoogleEarth 的消息定义
///
public partial class Form2 : Form
{
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
static readonly Int32 WM_QUIT = 0x0012;
private IntPtr GEHWnd = (IntPtr)5;
private IntPtr GEHrender = (IntPtr)5;
private IntPtr GEParentHrender = (IntPtr)5;
///
/// 定义 GE 应用程序类
///
private ApplicationGEClass GeApp;
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!this.DesignMode)
{
GeApp = new ApplicationGEClass();
GEHWnd = (IntPtr)GeApp.GetMainHwnd();
NativeMethods.SetWindowPos(GEHWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0,
NativeMethods.SWP_NOSIZE + NativeMethods.SWP_HIDEWINDOW);
GEHrender = (IntPtr)GeApp.GetRenderHwnd();
GEParentHrender = (IntPtr)NativeMethods.GetParent(GEHrender);
NativeMethods.MoveWindow(GEHrender, 0, 0, this.Width, this.Height, true);
NativeMethods.SetParent(GEHrender, this.Handle);
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
NativeMethods.PostMessage(GeApp.GetMainHwnd(), WM_QUIT, 0, 0);
}
}
66: }
2、NativeMethods 类定义:
1: // 功能:Windows API 调用
2: // 描述:大家可以参照 MSDN
3: // 作者:温伟鹏
4: // 日期:2009-02-08
5:
6: using System;
7: using System.Collections.Generic;
8: using System.Text;
9: using System.Runtime.InteropServices;
10:
11: namespace GEDemo
12: {
13:
14:
15:
16:
public class NativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,
int y, int cx, int cy, UInt32 uflags);
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
#region 预定义
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public static readonly IntPtr HWND_TOP = new IntPtr(0);
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly UInt32 SWP_NOSIZE = 1;
public static readonly UInt32 SWP_NOMOVE = 2;
public static readonly UInt32 SWP_NOZORDER = 4;
public static readonly UInt32 SWP_NOREDRAW = 8;
public static readonly UInt32 SWP_NOACTIVATE = 16;
public static readonly UInt32 SWP_FRAMECHANGED = 32;
public static readonly UInt32 SWP_SHOWWINDOW = 64;
public static readonly UInt32 SWP_HIDEWINDOW = 128;
public static readonly UInt32 SWP_NOCOPYBITS = 256;
public static readonly UInt32 SWP_NOOWNERZORDER = 512;
public static readonly UInt32 SWP_NOSENDCHANGING = 1024;
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
#endregion
public delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static IntPtr GetParent(IntPtr hWnd);
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight,
bool bRepaint);
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58: }
}
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
public static int GW_CHILD = 5;
public static int GW_HWNDNEXT = 2;
3、执行效果:
C# 调用 Google Earth Com API 开发(三)
好久没有更新《C#调用 Google Earth Com API 开发》系列文章了,今天带给大家的是第三
篇,本篇相对于第二篇主要改进了三个方面。
1) 实现 GoogleEarth 显示画面随窗口大小改变而改变
2) 截获 GoogleEarth 鼠标消息,实现单击、双击功能;鼠标滚轮缩放现在只能放大!O(∩_
∩)O~
3) 实现 GoogleEarth 彩色截图(测试环境:Windows 2003 Server ,Vista 与 Win7 中不可
用,XP 未测)
下面还是继续看代码:
1、GoogleEarth 动态改变大小
1: ///
2: /// 重新改变 GoogleEarth 视图的大小
3: ///
4: private void ResizeGoogleControl()
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:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
RECT mainRect = new RECT();
NativeMethods.GetWindowRect(GEHWnd, out mainRect);
clientRect = new RECT();
NativeMethods.GetClientRect(GEHrender, out clientRect);
int offsetW = mainRect.Width - clientRect.Width;
int offsetH = mainRect.Height - clientRect.Height;
int newWidth = this.Control.Width + (int)offsetW;
int newHeight = this.Control.Height + (int)offsetH;
NativeMethods.SetWindowPos(GEHWnd, NativeMethods.HWND_TOP,
0, 0, newWidth, newHeight,
NativeMethods.SWP_FRAMECHANGED);
NativeMethods.SendMessage(GEHWnd,
(uint)NativeMethods.WM_COMMAND, NativeMethods.WM_SIZE, 0);
25: }
2、鼠标消息
此例子中对于鼠标消息到处理使用了钩子,调用 HookAPI.dll 实现。
1:
2:
3:
///
/// 鼠标钩子
///