GoogleEarthCOMAPI开发一

 

最近由于项目需要,开始了Google Earth COM API的研究学习。在网上搜集了大量资料,但是对于Google Earth COM API的开发资料相对较少,最后也找到了一些有用的东东。经过整理综合并且加入了自己的一些想法。完成了一些功能。现在成果及代码公布如下,希望同行们批评指正,进一步完善其中的功能。

View Code
 1 #region 调用WindowsAPI
2
3 [DllImport("user32.dll", CharSet = CharSet.Auto)]
4 public extern static bool SetWindowPos(int hWnd,IntPtr hWndInsertAfter,int x,int y,int cx,int cy,uint uFlags);
5
6 [DllImport("user32.dll", CharSet = CharSet.Auto)]
7 public extern static IntPtr PostMessage(int hWnd,int msg,int wParam,int lParam);
8
9 [DllImport("user32.dll", CharSet = CharSet.Auto)]
10 public extern static IntPtr GetParent(IntPtr hWnd);
11
12 [DllImport("user32.dll", CharSet = CharSet.Auto)]
13 public extern static bool MoveWindow(IntPtr hWnd,int x,int y,int nWidth,int nHeight,bool bRepaint);
14
15 [DllImport("user32.dll", CharSet = CharSet.Auto)]
16 public extern static IntPtr SetParent(IntPtr hWndChild,IntPtr hWndNewParent);
17
18 #endregion

 

 

参数常量,可以查阅WINDOWS API,y有详细解释的。

View Code
 1 private IntPtr _GEHrender;
2 private IntPtr _GEParentHrender;
3 private IntPtr _GEMainHandler;
4 private IApplicationGE _googleEarth;
5 private System.Windows.Forms.Control _parentControl;
6
7 private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
8 private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
9 private static readonly IntPtr HWND_TOP = new IntPtr(0);
10 private static readonly IntPtr HWND_POPMOST = new IntPtr(-1);
11 private static readonly UInt32 SWP_NOSIZE = 1;
12 private static readonly UInt32 SWP_HIDEWINDOW = 128;
13 private static readonly Int32 WM_QUIT = 0x0012;
14 private static readonly Int32 WM_HIDE = 0x0;

主程序代码如下:

 

View Code
  1       //定义GE应用程序,用以启动Google Earth
2 ApplicationGEClass ge = new ApplicationGEClass();
3
4
5 public void SetGEHandlerToControl(System.Windows.Forms.Control parentControl, IApplicationGE geApplication)
6 {
7 this._parentControl = parentControl;
8 this._googleEarth = geApplication;
9
10 //获取GE的主窗体句柄
11 this._GEMainHandler = (IntPtr)this._googleEarth.GetMainHwnd();
12 //将GE主窗体的高宽设置为0,隐藏到GE主窗体
13 GEExtern.SetWindowPos((int)this._GEMainHandler, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE + SWP_HIDEWINDOW);
14
15 //获取GE地图控件句柄
16 this._GEHrender = (IntPtr)this._googleEarth.GetRenderHwnd();
17 //获取GE地图控件的父窗口句柄
18 this._GEParentHrender = GEExtern.GetParent(this._GEHrender);
19 //将GE地图控件的父窗体设置为不可见
20 //考虑到GE地图控件可能被其他程序截获,加上这一句以应完全
21 GEExtern.PostMessage((int)this._GEParentHrender, WM_HIDE, 0, 0);
22
23 //设置GE地图控件的父窗体句柄为winform上的控件
24 GEExtern.SetParent(this._GEHrender, parentControl.Handle);
25
26 }
27
28 private void Form1_Load(object sender, EventArgs e)
29 {
30 if (ge.IsOnline() != 0)
31 {
32 MessageBox.Show("已经登录!");
33 }
34 else
35 {
36 MessageBox.Show("尚未登录!");
37 }
38 SetGEHandlerToControl(pnlEarth, ge);
39 }
40
41 public void ResizeGEControl()
42 {
43 if (this._parentControl != null)
44 {
45 //设置GE地图控件的大小等于父窗体大小
46 GEExtern.MoveWindow(this._GEHrender, 0, 0, this._parentControl.Width, this._parentControl.Height, true);
47 }
48 }
49
50 public void RealseGEHandler()
51 {
52 try
53 {
54 if (this._parentControl != null)
55 {
56 //将GE地图控件的句柄还原到GE主窗体上去
57 GEExtern.SetParent(this._GEHrender, this._GEParentHrender);
58 //关闭GE主程序
59 GEExtern.PostMessage(this._googleEarth.GetMainHwnd(), WM_QUIT, 0, 0);
60 }
61 }
62 finally
63 {
64 //为防本程序的进程不能成功退出而导致GE出现问题,强制杀掉本程序的进程
65 System.Diagnostics.Process geProcess = System.Diagnostics.Process.GetCurrentProcess();
66 geProcess.Kill();
67 }
68 }
69
70 private void pnlEarth_SizeChanged(object sender, EventArgs e)
71 {
72 ResizeGEControl();
73 }
74
75 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
76 {
77 RealseGEHandler();
78 }
79 /// <summary>
80 /// 保存当前GE视图
81 /// </summary>
82 /// <param name="sender"></param>
83 /// <param name="e"></param>
84 private void btnSaveScreen_Click(object sender, EventArgs e)
85 {
86 SaveFileDialog sfd = new SaveFileDialog();
87 sfd.Title = "保存屏幕截图";
88 sfd.Filter = "图片(*.jpg)|*.jpg|所有文件|*.*";
89 sfd.FilterIndex = 1;
90 sfd.InitialDirectory = Application.StartupPath;
91 sfd.ShowDialog();
92 //string name = sfd.FileName;
93 string fileName = sfd.FileName + ".jpg"; ;
94 //string fileName = Application.StartupPath +name+ ".jpg";
95
96 try
97 {
98 ge.SaveScreenShot(fileName, 80);
99 MessageBox.Show("saved picture succesfullly!");
100 }
101 catch (Exception ex)
102 {
103 MessageBox.Show("保存图片失败!" + ex.Message);
104 }
105 }
106 /// <summary>
107 /// 打开保存的GE视图图片
108 /// </summary>
109 /// <param name="sender"></param>
110 /// <param name="e"></param>
111 private void btnOpenPicture_Click(object sender, EventArgs e)
112 {
113 OpenFileDialog ofd = new OpenFileDialog();
114
115 ofd.InitialDirectory = Application.StartupPath;
116
117 ofd.Filter = "图片(*.jpg)|*.jpg|所有文件|*.*";
118
119 ofd.FilterIndex = 1;
120
121 ofd.ShowDialog();
122
123 //fileName = ofd.FileName;
124
125 //建立新的系统进程
126 System.Diagnostics.Process process = new System.Diagnostics.Process();
127 //设置文件名,此处为图片的真实路径+文件名
128 process.StartInfo.FileName = ofd.FileName;
129 //此为关键部分,设置进程运行参数,此时为最大化窗口显示图片。
130 process.StartInfo.Arguments = "rundll32.exe C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_Fullscreen";
131 //此项为是否使用shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
132 process.StartInfo.UseShellExecute = true;
133 //此处可以更改进程所打开窗体的显示样式,可以不设
134 process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
135
136 process.Start();
137
138 process.Close();
139 }

 

 

最终运行效果如下:

posted @ 2011-08-31 20:13  gis追梦者  阅读(1264)  评论(1编辑  收藏  举报