C#基于google earth的二次开发

首先在C#中新建windows 应用程序。然后再form1中新建panel控件。命名为pnlEarth。

然后双击form1,进入form1.cs.

1、以下是代码:

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;

 

using EARTHLib;
using System.Runtime.InteropServices;
using HookAPI;
using System.Threading;

namespace MYGPS3
{
    public partial class Form1 : Form
    {

 

        private IntPtr _GEHrender;
        private IntPtr _GEParentHrender;
        private IntPtr _GEMainHandler;
        private IApplicationGE _googleEarth;
        private System.Windows.Forms.Control _parentControl;

        IntPtr mapPtr;
        IntPtr mainPtr;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetGEHandlerToControl(pnlEarth, _googleEarth);

        }

        /// <summary>
        /// 将GE的地图控件挂载到指定的控件中去
        /// </summary>
        /// <param name="parentControl">c#控件</param>
        /// <param name="geApplication">ge应用程序</param>
        public void SetGEHandlerToControl(System.Windows.Forms.Control parentControl, IApplicationGE geApplication)
        {
            this._parentControl = parentControl;
            this._googleEarth = geApplication;
            _googleEarth = new ApplicationGEClass();
            //获取GE的主窗体句柄
            this._GEMainHandler = (IntPtr)this._googleEarth.GetMainHwnd();
            //将GE主窗体的高宽设置为0,隐藏掉GE主窗体
            NativeMethods.SetWindowPos(this._GEMainHandler, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE + NativeMethods.SWP_HIDEWINDOW);

            //获取GE地图控件句柄
            this._GEHrender = (IntPtr)_googleEarth.GetRenderHwnd();
            //获取GE地图控件的父窗体句柄
            this._GEParentHrender = NativeMethods.GetParent(this._GEHrender);
            //将GE地图控件的父窗体设置为不可见
            //(考虑到GE地图控件可能被其他程序截获,加上这一句以应万全)
            NativeMethods.PostMessage((int)this._GEParentHrender, NativeMethods.WM_HIDE, 0, 0);

            //设置GE地图控件的父窗体句柄为winform上的控件
            NativeMethods.SetParent(this._GEHrender, parentControl.Handle);
        }

    
         public void ResizeGEControl()
        {
         if (this._parentControl != null)
         {
          //设置GE地图控件的大小等于父窗体大小
          NativeMethods.MoveWindow(this._GEHrender, 0, 0, this._parentControl.Width, this._parentControl.Height, true);
         }
        }
 
        /// <summary>
        /// 释放GE句柄
        /// </summary>
        public void RealseGEHandler()
        {
         try
         {
          if (this._parentControl != null)
          {
           //将GE地图控件的句柄还原到GE主窗体上去
           NativeMethods.SetParent(this._GEHrender, this._GEParentHrender);
           //关闭GE主程序
           NativeMethods.PostMessage(this._googleEarth.GetMainHwnd(), NativeMethods.WM_QUIT, 0, 0);
          }
         }
         finally
         {
          //为防本程序的进程不能成功退出而导致GE出现问题,强制杀掉本程序的进程
          System.Diagnostics.Process geProcess = System.Diagnostics.Process.GetCurrentProcess();
          geProcess.Kill();
         }
        }

 

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            RealseGEHandler();
        }

        private void pnlEarth_SizeChanged(object sender, EventArgs e)
        {
            ResizeGEControl();
        }

 

    }
}

2、然后新建类,进入类向导,命名为NativeMethods.cs

添加以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace MYGPS3
{
    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);

         [DllImport("user32.dll", CharSet = CharSet.Auto)]
         public static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
         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);

         [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
         public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);

         [DllImport("user32")]
         public static extern IntPtr WindowFromPoint(Point point);//根据鼠标指针所在位置,得到窗口句柄

 
       #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;

          #endregion
 
        
 
          public static int GW_CHILD = 5;
         public static int GW_HWNDNEXT = 2;
         public static readonly Int32 WM_QUIT = 0x0012;
         public static readonly Int32 WM_HIDE = 0x0;


    }
}

3、添加google earthapi接口

在菜单栏中选择项目->添加引用->COM,然后选择Google Earth 1.0 Type Library.

 

之后生成即可实现将google earth内嵌到panel控件中,进行二次开发。

你可能感兴趣的:(object,Google,C#,user,Class,WinForm)