在.NET平台使用Google Earth COM API示例(02)

原文链接:http://www.cnblogs.com/fengyhack/p/10603718.html

准备工作与注意事项同前一篇文章,现在我们来关注一些高级的内容。

本文将涉及到两个方面的内容。

1.跨平台调用,在C#中使用Windows API(从dll导入的函数)

2.将GE渲染窗口嵌入到C#的WinForm窗体中

第一部分,跨平台调用的代码

// Windows API调用
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

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

        [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 =                           1U;
        public static readonly UInt32 SWP_NOMOVE =                        2U;
        public static readonly UInt32 SWP_NOZORDER =                    4U;
        public static readonly UInt32 SWP_NOREDRAW =                   8U;
        public static readonly UInt32 SWP_NOACTIVATE =                16U;
        public static readonly UInt32 SWP_FRAMECHANGED =         32U;
        public static readonly UInt32 SWP_SHOWWINDOW =           64U;
        public static readonly UInt32 SWP_HIDEWINDOW =            128U;
        public static readonly UInt32 SWP_NOCOPYBITS =               256U;
        public static readonly UInt32 SWP_NOOWNERZORDER =    512U;
        public static readonly UInt32 SWP_NOSENDCHANGING = 1024U;

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

        [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;
    }
}

这部分代码被称作"NativeMethods",其中的一些method将会在第二部分中被引用


第二部分,窗体嵌入

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EARTHLib;

namespace GEDemo2
{
    public partial class GEDemo2 : Form
    {
        static readonly Int32 WM_QUIT = 0x0012;
        private IntPtr GEHMainWnd = (IntPtr)0;
        private IntPtr GEHRenderWnd = (IntPtr)0;

        private ApplicationGE GEApp;
        public GEDemo2()
        {
            InitializeComponent();
        }

        //重写OnLoad比实现Form_Load更高效
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //启动GoogleEarth实例
            GEApp = new ApplicationGE();

            GEHMainWnd = (IntPtr)GEApp.GetMainHwnd();
            //隐藏GoogleEarth主窗口
            NativeMethods.SetWindowPos(GEHMainWnd, NativeMethods.HWND_BOTTOM,
                0, 0, 0, 0,
                NativeMethods.SWP_NOSIZE | NativeMethods.SWP_HIDEWINDOW);

            GEHRenderWnd = (IntPtr)GEApp.GetRenderHwnd();
            //将渲染窗口嵌入到主窗体
            NativeMethods.MoveWindow(GEHRenderWnd, 0, 0, this.Width, this.Height, true);
            NativeMethods.SetParent(GEHRenderWnd, this.Handle);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            //关闭窗口
            NativeMethods.PostMessage((int)GEHMainWnd, WM_QUIT, 0, 0);
        }
    }
}
这部分就是主窗体的(非设计部分)的全部代码,运行结果如下



现在只是实现了第一部分(窗体嵌入),稍后将会陆续完善。敬请关注本博。

点此领取楼主



转载于:https://www.cnblogs.com/fengyhack/p/10603718.html

原文链接:加载失败,请重新获取