• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#调用GoogleEarthComAPI开发(二)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

原文地址:http://www.cnblogs.com/wpwen/archive/2009/02/22/1395622.html

继《C#调用GoogleEarth Com API开发(一)》,我Neil又带给大家第二篇文章。这一篇文章在第一篇的基础上,展示如何调用Windows API将GoogleEarth的界面隐藏掉,并将GoogleEarth的地图显示在自定义的窗体上。废话少说,直接上代码。

1、主窗口代码:

   1: // 功能:GE实例(二)
   2: // 描述:GE COM API 网址:http://earth.google.com/comapi/index.html
   3: // 作者:温伟鹏
   4: // 日期:2009-02-08
   5:  
   6: using System;
   7: using System.Collections.Generic;
   8: using System.ComponentModel;
   9: using System.Data;
  10: using System.Drawing;
  11: using System.Text;
  12: using System.Windows.Forms;
  13: using EARTHLib;
  14:  
  15: namespace GEDemo
  16: {
  17:     public partial class Form2 : Form
  18:     {
  19:         /// <summary>
  20:         /// 用来关闭GoogleEarth的消息定义
  21:         /// </summary>
  22:         static readonly Int32 WM_QUIT = 0x0012; 
  23:  
  24:         private IntPtr GEHWnd = (IntPtr)5;
  25:         private IntPtr GEHrender = (IntPtr)5;
  26:         private IntPtr GEParentHrender = (IntPtr)5;
  27:         /// <summary>
  28:         /// 定义GE应用程序类
  29:         /// </summary>
  30:         private ApplicationGEClass GeApp;
  31:  
  32:         public Form2()
  33:         {
  34:             InitializeComponent();
  35:         }
  36:  
  37:         protected override void OnLoad(EventArgs e)
  38:         {
  39:             base.OnLoad(e);
  40:  
  41:             if (!this.DesignMode)
  42:             {
  43:                 GeApp = new ApplicationGEClass();
  44:  
  45:                 GEHWnd = (IntPtr)GeApp.GetMainHwnd();
  46:  
  47:                 NativeMethods.SetWindowPos(GEHWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0,
  48:                     NativeMethods.SWP_NOSIZE + NativeMethods.SWP_HIDEWINDOW);
  49:  
  50:                 GEHrender = (IntPtr)GeApp.GetRenderHwnd();
  51:                 GEParentHrender = (IntPtr)NativeMethods.GetParent(GEHrender);
  52:  
  53:                 NativeMethods.MoveWindow(GEHrender, 0, 0, this.Width, this.Height, true);
  54:  
  55:                 NativeMethods.SetParent(GEHrender, this.Handle);
  56:             }
  57:         }
  58:  
  59:         protected override void OnClosing(CancelEventArgs e)
  60:         {
  61:             base.OnClosing(e);
  62:  
  63:             NativeMethods.PostMessage(GeApp.GetMainHwnd(), WM_QUIT, 0, 0);
  64:         }
  65:     }
  66: }

 

2、NativeMethods类定义:

   1: // 功能:Windows API调用
   2: // 描述:大家可以参照MSDN
   3: // 作者:温伟鹏
   4: // 日期:2009-02-08
   5:  
   6: using System;
   7: using System.Collections.Generic;
   8: using System.Text;
   9: using System.Runtime.InteropServices;
  10:  
  11: namespace GEDemo
  12: {
  13:     public class NativeMethods
  14:     {
  15:         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  16:         public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, UInt32 uflags);
  17:  
  18:         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  19:         public static extern IntPtr PostMessage(int hWnd, int msg, int wParam, int lParam);
  20:  
  21:         #region 预定义
  22:  
  23:         public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
  24:         public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
  25:         public static readonly IntPtr HWND_TOP = new IntPtr(0);
  26:         public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
  27:         public static readonly UInt32 SWP_NOSIZE = 1;
  28:         public static readonly UInt32 SWP_NOMOVE = 2;
  29:         public static readonly UInt32 SWP_NOZORDER = 4;
  30:         public static readonly UInt32 SWP_NOREDRAW = 8;
  31:         public static readonly UInt32 SWP_NOACTIVATE = 16;
  32:         public static readonly UInt32 SWP_FRAMECHANGED = 32;
  33:         public static readonly UInt32 SWP_SHOWWINDOW = 64;
  34:         public static readonly UInt32 SWP_HIDEWINDOW = 128;
  35:         public static readonly UInt32 SWP_NOCOPYBITS = 256;
  36:         public static readonly UInt32 SWP_NOOWNERZORDER = 512;
  37:         public static readonly UInt32 SWP_NOSENDCHANGING = 1024;
  38:  
  39:         #endregion
  40:  
  41:         public delegate int EnumWindowsProc(IntPtr hwnd, int lParam);
  42:  
  43:         [DllImport("user32", CharSet = CharSet.Auto)]
  44:         public extern static IntPtr GetParent(IntPtr hWnd);
  45:  
  46:         [DllImport("user32", CharSet = CharSet.Auto)]
  47:         public extern static bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  48:  
  49:         [DllImport("user32", CharSet = CharSet.Auto)]
  50:         public extern static IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
  51:  
  52:         [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
  53:         public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
  54:  
  55:         public static int GW_CHILD = 5;
  56:         public static int GW_HWNDNEXT = 2;
  57:     }
  58: }

3、执行效果:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
(转)C#开源资源大汇总发布时间:2022-07-10
下一篇:
C#为应用程序注册快捷键发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap