1.安裝Google Earth。
2.在解決方案中添加對EARTHLib COM組件的引用。
3.引用EARTHLib 和 System.Runtime.InteropServices。
- using EARTHLib;
- using System.Runtime.InteropServices;
4.添加函數聲明
- 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);
- [DllImport("user32", CharSet = CharSet.Auto)]
- public extern static IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
5.在一個C# 中WINFORM中顯示Google Earth
- ApplicationGEClass application
- private IntPtr GEHrender = (IntPtr)0;//Google Earth的句柄
- private IntPtr GEParentHrender = (IntPtr)0;//Google Earth容器的句柄
- application = new ApplicationGEClass();//實例化一個Google Earth
- GEHrender = (IntPtr)application.GetRenderHwnd();
- GEParentHrender = GetParent(GEHrender);
6.調用Win32的api調整窗口的位置和大小
- MoveWindow(GEHrender,0,0, 800, 600, true);//窗口句柄,窗口的位置,窗口的大小,是否重繪
7.設定Google Earth的容器
- SetParent(GEHrender, this.Handle);//GE的句柄,容器的句柄
8.運行結果
9.也可以綁定到Panel等佈局容器的句柄
- SetParent(GEHrender, this.splitContainer1.Panel2.Handle);
10.運行結果
如果想在WPF中加入Google Earth的話 方式有點不同但是原理都是一樣的,有時間的話我會寫出來和大家分享:)