项目中用到了Google earth二次开发,现在把搜集的资料进行了一下整理。供大家参考
1、安装google earth,我用的是4.2的版本
2、制作控件,封装google earth API,同时加入windows API,来将google earth的窗口嵌入在 控件中。
代码如下:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using EARTHLib;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace GoogleEarthCOMWrapper
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public struct RECT
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int X1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Y1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int X2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Y2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public partial class EarthControl : UserControl
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private RECT windowRect = new RECT();
IntPtr GEHWnd = IntPtr.Zero;
IntPtr GEHrender = IntPtr.Zero;
IntPtr GEParentHrender = IntPtr.Zero;
int IntGEHrender = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int WM_CLOSE = 0x10;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
预定义#region 预定义
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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;
public static readonly int WM_MOUSEWHEEL = 0x020A;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static int GW_CHILD = 5;
public static int GW_HWNDNEXT = 2;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
API#region API
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static IntPtr GetParent(IntPtr hWnd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, ref RECT lpRect);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
#endregion
public EarthControl()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Members#region Private Members
private ApplicationGEClass _applicationGEClass;
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Member Functions#region Public Member Functions
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Init()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = 0;
this._applicationGEClass = new ApplicationGEClass();
while (i < 3 && (GEHWnd == IntPtr.Zero))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GEHWnd = (IntPtr)_applicationGEClass.GetMainHwnd();
i++;
}
catch
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Threading.Thread.Sleep(2000);
}
}
while (_applicationGEClass.IsInitialized() == 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Give the app a couple seconds to Load
Console.WriteLine("Waiting for Google Earth to initialize
");
Thread.Sleep(500);
}
}
public CameraInfoGEClass GetCamera(bool considerTerrain)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetCamera(considerTerrain ? 1 : 0) as CameraInfoGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void SetCamera(CameraInfoGEClass cameraInfo, double speed)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.SetCamera(cameraInfo, speed);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void SetCameraParams(double lat, double lon, double alt, AltitudeModeGE altMode, double range,
double tilt, double azimuth, double speed)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.SetCameraParams(lat, lon, alt, altMode, range, tilt, azimuth, speed);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void SaveScreenShot(string fileName, int quality)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.SaveScreenShot(fileName, quality);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
public void OpenKmlFile(string fileName, bool suppressMessages)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.OpenKmlFile(fileName, suppressMessages ? 1 : 0);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void OpenKmlFileAndCloseOthers(string fileName, bool suppressMessages)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_applicationGEClass.GetTemporaryPlaces() != null && _applicationGEClass.GetTemporaryPlaces().Visibility > 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.GetTemporaryPlaces().Visibility = 0;
}
_applicationGEClass.OpenKmlFile(fileName, suppressMessages ? 1 : 0);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
public void LoadKmlData(string data)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.LoadKmlData(ref data);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FeatureGEClass GetFeatureByName(string name)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetFeatureByName(name) as FeatureGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FeatureGEClass GetFeatureByHref(string href)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetFeatureByHref(href) as FeatureGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void SetFeatureView(FeatureGEClass feature, double speed)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.SetFeatureView(feature, speed);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public GeoPoint GetPointOnTerrainFromScreenCoords(double screen_x, double screen_y)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PointOnTerrainGE ret = _applicationGEClass.GetPointOnTerrainFromScreenCoords(screen_x, screen_y);
//GeoPoint gp = new GeoPoint((double)ret.GetValue(0), (double)ret.GetValue(1), (double)ret.GetValue(2));
GeoPoint gp = new GeoPoint((double)ret.Latitude, (double)ret.Longitude, (double)ret.Altitude);
return gp;
}
catch(Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return new GeoPoint(0, 0, 0);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsInitialized()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (_applicationGEClass.IsInitialized() == 0) ? false : true;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsOnline()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (_applicationGEClass.IsOnline() == 0) ? false : true;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Login()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.Login();
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Logout()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.Logout();
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void ShowDescriptionBalloon(FeatureGEClass feature)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.ShowDescriptionBalloon(feature);
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void HideDescriptionBalloons()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_applicationGEClass.HideDescriptionBalloons();
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FeatureGEClass GetHighlightedFeature()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetHighlightedFeature() as FeatureGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FeatureGEClass GetMyPlaces()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetMyPlaces() as FeatureGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FeatureGEClass GetTemporaryPlaces()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetTemporaryPlaces() as FeatureGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public FeatureCollectionGEClass GetLayersDatabases()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetLayersDatabases() as FeatureCollectionGEClass;
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int GetMainHwnd()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _applicationGEClass.GetMainHwnd();
}
catch (Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ThrowException(e);
}
return 0;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Control#region Windows Control
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void TakeIn()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//GoogleEarthCOMWrapper.NativeMethods.SetParent(GEHrender, this.tabControl1.TabPages[0].Handle);
//GoogleEarthCOMWrapper.NativeMethods.MoveWindow(GEHrender, 0, 0, this.tabControl1.TabPages[0].Width, this.tabControl1.TabPages[0].Height, true);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//GoogleEarthCOMWrapper.NativeMethods.GetClientRect(GEParentHrender, ref windowRect);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//GoogleEarthCOMWrapper.NativeMethods.SetWindowPos(GEHWnd, GoogleEarthCOMWrapper.NativeMethods.HWND_BOTTOM, 0, 0, 0, 0,
//GoogleEarthCOMWrapper.NativeMethods.SWP_NOSIZE + GoogleEarthCOMWrapper.NativeMethods.SWP_HIDEWINDOW);
if (_applicationGEClass != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntGEHrender = _applicationGEClass.GetRenderHwnd();
GEHWnd = (IntPtr)_applicationGEClass.GetMainHwnd();
GEHrender = (IntPtr)IntGEHrender;
GEParentHrender = (IntPtr)GetParent(GEHrender);
SetParent(GEHrender, this.Handle);
MoveWindow(GEHrender, 0, 0, this.Width, this.Height, true);
GetClientRect(GEParentHrender, ref windowRect);
SetWindowPos(GEHWnd,HWND_BOTTOM, 0, 0, 0, 0,SWP_NOSIZE +SWP_HIDEWINDOW);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Properties#region Public Properties
public int StreamingProgressPercentage
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.StreamingProgressPercentage; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public double AutoPilotSpeed
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.AutoPilotSpeed; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _applicationGEClass.AutoPilotSpeed = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public ViewExtentsGEClass ViewExtents
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.ViewExtents as ViewExtentsGEClass; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int VersionMajor
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.VersionMajor; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int VersionMinor
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.VersionMinor; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int VersionBuild
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.VersionBuild; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public AppTypeGE VersionAppType
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.VersionAppType; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public double ElevationExaggeration
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _applicationGEClass.ElevationExaggeration; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _applicationGEClass.ElevationExaggeration = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Helpers#region Private Helpers
private void ThrowException(Exception e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string message = "Unknown Exception";
Exception ret = new Exception(message);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (e is System.Runtime.InteropServices.COMException)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
uint errorCode = (uint)((System.Runtime.InteropServices.COMException)e).ErrorCode;
//switch (errorCode)
//{
// case 0x80048002:
// message = "GoogleEarth not initilaized";
// ret = new ApplicationUninitializedException(message, e);
// break;
// case 0x80048004:
// message = "Feature has no view";
// ret = new FeatureHasNoViewException(message, e);
// break;
// case 0x80048001:
// message = "Invalid or deleted feature";
// ret = new InvalidOrDeletedFeatureException(message, e);
// break;
//}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
throw ret;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void SendMouseWheel(MouseEventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (IntGEHrender != 0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PostMessage(IntGEHrender, WM_MOUSEWHEEL, e.Delta, e.Delta);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void DestroyHandle()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (GEHWnd != IntPtr.Zero)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Post a colse message
PostMessage(GEHWnd.ToInt32(), WM_CLOSE, 0, 0);
// Delay for it to get the message
System.Threading.Thread.Sleep(1000);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Clear internal handle
GEHWnd = IntPtr.Zero;
GEHrender = IntPtr.Zero;
}
base.DestroyHandle();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void OnResize(EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnResize(e);
if (GEHrender != IntPtr.Zero)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MoveWindow(GEHrender, 0, 0, this.Width, this.Height, true);
}
}
}
}
4、使用控件
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Test
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class Form1 : Form
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Form1()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//启动google earth
private void button1_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
earthControl1.Init();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//将google earth嵌入到控件中
private void button2_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
earthControl1.TakeIn();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//传递滚轮事件到google earth 中
protected override void OnMouseWheel(MouseEventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnMouseWheel(e);
if (earthControl1.Bounds.Contains(e.X, e.Y))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
earthControl1.SendMouseWheel(e);
}
}
}
}
这个控件还是个雏形,还有很多方面没有考虑,比如说google earth的销毁等。