项目中用到了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;
namespace GoogleEarthCOMWrapper
{
public struct RECT
{
public int X1;
public int Y1;
public int X2;
public int Y2;
}
public partial class EarthControl : UserControl
{
private RECT windowRect = new RECT();
IntPtr GEHWnd = IntPtr.Zero;
IntPtr GEHrender = IntPtr.Zero;
IntPtr GEParentHrender = IntPtr.Zero;
int IntGEHrender = 0;
private int WM_CLOSE = 0x10;
预定义#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;
public static readonly int WM_MOUSEWHEEL = 0x020A;
public static int GW_CHILD = 5;
public static int GW_HWNDNEXT = 2;
#endregion
public delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
API#region API
[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);
[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);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[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);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
#endregion
public EarthControl()
{
InitializeComponent();
}
Private Members#region Private Members
private ApplicationGEClass _applicationGEClass;
#endregion
Public Member Functions#region Public Member Functions
public void Init()
{
int i = 0;
this._applicationGEClass = new ApplicationGEClass();
while (i < 3 && (GEHWnd == IntPtr.Zero))
{
try
{
GEHWnd = (IntPtr)_applicationGEClass.GetMainHwnd();
i++;
}
catch
{
System.Threading.Thread.Sleep(2000);
}
}
while (_applicationGEClass.IsInitialized() == 0)
{
// Give the app a couple seconds to Load
Console.WriteLine("Waiting for Google Earth to initialize");
Thread.Sleep(500);
}
}
public CameraInfoGEClass GetCamera(bool considerTerrain)
{
try
{
return _applicationGEClass.GetCamera(considerTerrain ? 1 : 0) as CameraInfoGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public void SetCamera(CameraInfoGEClass cameraInfo, double speed)
{
try
{
_applicationGEClass.SetCamera(cameraInfo, speed);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void SetCameraParams(double lat, double lon, double alt, AltitudeModeGE altMode, double range,
double tilt, double azimuth, double speed)
{
try
{
_applicationGEClass.SetCameraParams(lat, lon, alt, altMode, range, tilt, azimuth, speed);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void SaveScreenShot(string fileName, int quality)
{
try
{
_applicationGEClass.SaveScreenShot(fileName, quality);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void OpenKmlFile(string fileName, bool suppressMessages)
{
try
{
_applicationGEClass.OpenKmlFile(fileName, suppressMessages ? 1 : 0);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void OpenKmlFileAndCloseOthers(string fileName, bool suppressMessages)
{
try
{
if (_applicationGEClass.GetTemporaryPlaces() != null && _applicationGEClass.GetTemporaryPlaces().Visibility > 0)
{
_applicationGEClass.GetTemporaryPlaces().Visibility = 0;
}
_applicationGEClass.OpenKmlFile(fileName, suppressMessages ? 1 : 0);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void LoadKmlData(string data)
{
try
{
_applicationGEClass.LoadKmlData(ref data);
}
catch (Exception e)
{
ThrowException(e);
}
}
public FeatureGEClass GetFeatureByName(string name)
{
try
{
return _applicationGEClass.GetFeatureByName(name) as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureGEClass GetFeatureByHref(string href)
{
try
{
return _applicationGEClass.GetFeatureByHref(href) as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public void SetFeatureView(FeatureGEClass feature, double speed)
{
try
{
_applicationGEClass.SetFeatureView(feature, speed);
}
catch (Exception e)
{
ThrowException(e);
}
}
public GeoPoint GetPointOnTerrainFromScreenCoords(double screen_x, double screen_y)
{
try
{
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)
{
ThrowException(e);
}
return new GeoPoint(0, 0, 0);
}
public bool IsInitialized()
{
try
{
return (_applicationGEClass.IsInitialized() == 0) ? false : true;
}
catch (Exception e)
{
ThrowException(e);
}
return false;
}
public bool IsOnline()
{
try
{
return (_applicationGEClass.IsOnline() == 0) ? false : true;
}
catch (Exception e)
{
ThrowException(e);
}
return false;
}
public void Login()
{
try
{
_applicationGEClass.Login();
}
catch (Exception e)
{
ThrowException(e);
}
}
public void Logout()
{
try
{
_applicationGEClass.Logout();
}
catch (Exception e)
{
ThrowException(e);
}
}
public void ShowDescriptionBalloon(FeatureGEClass feature)
{
try
{
_applicationGEClass.ShowDescriptionBalloon(feature);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void HideDescriptionBalloons()
{
try
{
_applicationGEClass.HideDescriptionBalloons();
}
catch (Exception e)
{
ThrowException(e);
}
}
public FeatureGEClass GetHighlightedFeature()
{
try
{
return _applicationGEClass.GetHighlightedFeature() as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureGEClass GetMyPlaces()
{
try
{
return _applicationGEClass.GetMyPlaces() as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureGEClass GetTemporaryPlaces()
{
try
{
return _applicationGEClass.GetTemporaryPlaces() as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureCollectionGEClass GetLayersDatabases()
{
try
{
return _applicationGEClass.GetLayersDatabases() as FeatureCollectionGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public int GetMainHwnd()
{
try
{
return _applicationGEClass.GetMainHwnd();
}
catch (Exception e)
{
ThrowException(e);
}
return 0;
}
#endregion
Windows Control#region Windows Control
public void TakeIn()
{
//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);
//GoogleEarthCOMWrapper.NativeMethods.GetClientRect(GEParentHrender, ref windowRect);
//GoogleEarthCOMWrapper.NativeMethods.SetWindowPos(GEHWnd, GoogleEarthCOMWrapper.NativeMethods.HWND_BOTTOM, 0, 0, 0, 0,
//GoogleEarthCOMWrapper.NativeMethods.SWP_NOSIZE + GoogleEarthCOMWrapper.NativeMethods.SWP_HIDEWINDOW);
if (_applicationGEClass != null)
{
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);
}
}
#endregion
Public Properties#region Public Properties
public int StreamingProgressPercentage
{
get { return _applicationGEClass.StreamingProgressPercentage; }
}
public double AutoPilotSpeed
{
get { return _applicationGEClass.AutoPilotSpeed; }
set { _applicationGEClass.AutoPilotSpeed = value; }
}
public ViewExtentsGEClass ViewExtents
{
get { return _applicationGEClass.ViewExtents as ViewExtentsGEClass; }
}
public int VersionMajor
{
get { return _applicationGEClass.VersionMajor; }
}
public int VersionMinor
{
get { return _applicationGEClass.VersionMinor; }
}
public int VersionBuild
{
get { return _applicationGEClass.VersionBuild; }
}
public AppTypeGE VersionAppType
{
get { return _applicationGEClass.VersionAppType; }
}
public double ElevationExaggeration
{
get { return _applicationGEClass.ElevationExaggeration; }
set { _applicationGEClass.ElevationExaggeration = value; }
}
#endregion
Private Helpers#region Private Helpers
private void ThrowException(Exception e)
{
string message = "Unknown Exception";
Exception ret = new Exception(message);
if (e is System.Runtime.InteropServices.COMException)
{
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;
//}
}
throw ret;
}
#endregion
public void SendMouseWheel(MouseEventArgs e)
{
if (IntGEHrender != 0)
{
PostMessage(IntGEHrender, WM_MOUSEWHEEL, e.Delta, e.Delta);
}
}
protected override void DestroyHandle()
{
if (GEHWnd != IntPtr.Zero)
{
// Post a colse message
PostMessage(GEHWnd.ToInt32(), WM_CLOSE, 0, 0);
// Delay for it to get the message
System.Threading.Thread.Sleep(1000);
// Clear internal handle
GEHWnd = IntPtr.Zero;
GEHrender = IntPtr.Zero;
}
base.DestroyHandle();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (GEHrender != IntPtr.Zero)
{
MoveWindow(GEHrender, 0, 0, this.Width, this.Height, true);
}
}
}
}
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;
namespace GoogleEarthCOMWrapper
{
public struct RECT
{
public int X1;
public int Y1;
public int X2;
public int Y2;
}
public partial class EarthControl : UserControl
{
private RECT windowRect = new RECT();
IntPtr GEHWnd = IntPtr.Zero;
IntPtr GEHrender = IntPtr.Zero;
IntPtr GEParentHrender = IntPtr.Zero;
int IntGEHrender = 0;
private int WM_CLOSE = 0x10;
预定义#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;
public static readonly int WM_MOUSEWHEEL = 0x020A;
public static int GW_CHILD = 5;
public static int GW_HWNDNEXT = 2;
#endregion
public delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
API#region API
[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);
[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);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[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);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
#endregion
public EarthControl()
{
InitializeComponent();
}
Private Members#region Private Members
private ApplicationGEClass _applicationGEClass;
#endregion
Public Member Functions#region Public Member Functions
public void Init()
{
int i = 0;
this._applicationGEClass = new ApplicationGEClass();
while (i < 3 && (GEHWnd == IntPtr.Zero))
{
try
{
GEHWnd = (IntPtr)_applicationGEClass.GetMainHwnd();
i++;
}
catch
{
System.Threading.Thread.Sleep(2000);
}
}
while (_applicationGEClass.IsInitialized() == 0)
{
// Give the app a couple seconds to Load
Console.WriteLine("Waiting for Google Earth to initialize");
Thread.Sleep(500);
}
}
public CameraInfoGEClass GetCamera(bool considerTerrain)
{
try
{
return _applicationGEClass.GetCamera(considerTerrain ? 1 : 0) as CameraInfoGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public void SetCamera(CameraInfoGEClass cameraInfo, double speed)
{
try
{
_applicationGEClass.SetCamera(cameraInfo, speed);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void SetCameraParams(double lat, double lon, double alt, AltitudeModeGE altMode, double range,
double tilt, double azimuth, double speed)
{
try
{
_applicationGEClass.SetCameraParams(lat, lon, alt, altMode, range, tilt, azimuth, speed);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void SaveScreenShot(string fileName, int quality)
{
try
{
_applicationGEClass.SaveScreenShot(fileName, quality);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void OpenKmlFile(string fileName, bool suppressMessages)
{
try
{
_applicationGEClass.OpenKmlFile(fileName, suppressMessages ? 1 : 0);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void OpenKmlFileAndCloseOthers(string fileName, bool suppressMessages)
{
try
{
if (_applicationGEClass.GetTemporaryPlaces() != null && _applicationGEClass.GetTemporaryPlaces().Visibility > 0)
{
_applicationGEClass.GetTemporaryPlaces().Visibility = 0;
}
_applicationGEClass.OpenKmlFile(fileName, suppressMessages ? 1 : 0);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void LoadKmlData(string data)
{
try
{
_applicationGEClass.LoadKmlData(ref data);
}
catch (Exception e)
{
ThrowException(e);
}
}
public FeatureGEClass GetFeatureByName(string name)
{
try
{
return _applicationGEClass.GetFeatureByName(name) as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureGEClass GetFeatureByHref(string href)
{
try
{
return _applicationGEClass.GetFeatureByHref(href) as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public void SetFeatureView(FeatureGEClass feature, double speed)
{
try
{
_applicationGEClass.SetFeatureView(feature, speed);
}
catch (Exception e)
{
ThrowException(e);
}
}
public GeoPoint GetPointOnTerrainFromScreenCoords(double screen_x, double screen_y)
{
try
{
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)
{
ThrowException(e);
}
return new GeoPoint(0, 0, 0);
}
public bool IsInitialized()
{
try
{
return (_applicationGEClass.IsInitialized() == 0) ? false : true;
}
catch (Exception e)
{
ThrowException(e);
}
return false;
}
public bool IsOnline()
{
try
{
return (_applicationGEClass.IsOnline() == 0) ? false : true;
}
catch (Exception e)
{
ThrowException(e);
}
return false;
}
public void Login()
{
try
{
_applicationGEClass.Login();
}
catch (Exception e)
{
ThrowException(e);
}
}
public void Logout()
{
try
{
_applicationGEClass.Logout();
}
catch (Exception e)
{
ThrowException(e);
}
}
public void ShowDescriptionBalloon(FeatureGEClass feature)
{
try
{
_applicationGEClass.ShowDescriptionBalloon(feature);
}
catch (Exception e)
{
ThrowException(e);
}
}
public void HideDescriptionBalloons()
{
try
{
_applicationGEClass.HideDescriptionBalloons();
}
catch (Exception e)
{
ThrowException(e);
}
}
public FeatureGEClass GetHighlightedFeature()
{
try
{
return _applicationGEClass.GetHighlightedFeature() as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureGEClass GetMyPlaces()
{
try
{
return _applicationGEClass.GetMyPlaces() as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureGEClass GetTemporaryPlaces()
{
try
{
return _applicationGEClass.GetTemporaryPlaces() as FeatureGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public FeatureCollectionGEClass GetLayersDatabases()
{
try
{
return _applicationGEClass.GetLayersDatabases() as FeatureCollectionGEClass;
}
catch (Exception e)
{
ThrowException(e);
}
return null;
}
public int GetMainHwnd()
{
try
{
return _applicationGEClass.GetMainHwnd();
}
catch (Exception e)
{
ThrowException(e);
}
return 0;
}
#endregion
Windows Control#region Windows Control
public void TakeIn()
{
//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);
//GoogleEarthCOMWrapper.NativeMethods.GetClientRect(GEParentHrender, ref windowRect);
//GoogleEarthCOMWrapper.NativeMethods.SetWindowPos(GEHWnd, GoogleEarthCOMWrapper.NativeMethods.HWND_BOTTOM, 0, 0, 0, 0,
//GoogleEarthCOMWrapper.NativeMethods.SWP_NOSIZE + GoogleEarthCOMWrapper.NativeMethods.SWP_HIDEWINDOW);
if (_applicationGEClass != null)
{
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);
}
}
#endregion
Public Properties#region Public Properties
public int StreamingProgressPercentage
{
get { return _applicationGEClass.StreamingProgressPercentage; }
}
public double AutoPilotSpeed
{
get { return _applicationGEClass.AutoPilotSpeed; }
set { _applicationGEClass.AutoPilotSpeed = value; }
}
public ViewExtentsGEClass ViewExtents
{
get { return _applicationGEClass.ViewExtents as ViewExtentsGEClass; }
}
public int VersionMajor
{
get { return _applicationGEClass.VersionMajor; }
}
public int VersionMinor
{
get { return _applicationGEClass.VersionMinor; }
}
public int VersionBuild
{
get { return _applicationGEClass.VersionBuild; }
}
public AppTypeGE VersionAppType
{
get { return _applicationGEClass.VersionAppType; }
}
public double ElevationExaggeration
{
get { return _applicationGEClass.ElevationExaggeration; }
set { _applicationGEClass.ElevationExaggeration = value; }
}
#endregion
Private Helpers#region Private Helpers
private void ThrowException(Exception e)
{
string message = "Unknown Exception";
Exception ret = new Exception(message);
if (e is System.Runtime.InteropServices.COMException)
{
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;
//}
}
throw ret;
}
#endregion
public void SendMouseWheel(MouseEventArgs e)
{
if (IntGEHrender != 0)
{
PostMessage(IntGEHrender, WM_MOUSEWHEEL, e.Delta, e.Delta);
}
}
protected override void DestroyHandle()
{
if (GEHWnd != IntPtr.Zero)
{
// Post a colse message
PostMessage(GEHWnd.ToInt32(), WM_CLOSE, 0, 0);
// Delay for it to get the message
System.Threading.Thread.Sleep(1000);
// Clear internal handle
GEHWnd = IntPtr.Zero;
GEHrender = IntPtr.Zero;
}
base.DestroyHandle();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (GEHrender != IntPtr.Zero)
{
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;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//启动google earth
private void button1_Click(object sender, EventArgs e)
{
earthControl1.Init();
}
//将google earth嵌入到控件中
private void button2_Click(object sender, EventArgs e)
{
earthControl1.TakeIn();
}
//传递滚轮事件到google earth 中
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if (earthControl1.Bounds.Contains(e.X, e.Y))
{
earthControl1.SendMouseWheel(e);
}
}
}
}
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;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//启动google earth
private void button1_Click(object sender, EventArgs e)
{
earthControl1.Init();
}
//将google earth嵌入到控件中
private void button2_Click(object sender, EventArgs e)
{
earthControl1.TakeIn();
}
//传递滚轮事件到google earth 中
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if (earthControl1.Bounds.Contains(e.X, e.Y))
{
earthControl1.SendMouseWheel(e);
}
}
}
}
这个控件还是个雏形,还有很多方面没有考虑,比如说google earth的销毁等。