好久沒有更新《C#調用Google Earth Com API開發》系列文章了,今天帶給大家的是第三篇,本篇相 對於第二篇主要改進了三個方面。
1) 實現GoogleEarth顯示畫面隨窗口大小改變而改變
2) 截獲GoogleEarth鼠標消息,實現單擊、雙擊功能;鼠標滾輪縮放現在只能放大!O(∩_∩)O~
3) 實現GoogleEarth彩色截圖(測試環境:Windows 2003 Server ,Vista與Win7中不可用,XP未測)
下面還是繼續看代碼:
1、GoogleEarth動態改變大小
1: /// <summary>
2: /// 重新改變GoogleEarth視圖的大小
3: /// </summary>
4: private void ResizeGoogleControl()
5: {
6: NativeMethods.SendMessage(GEHWnd, (uint)NativeMethods.WM_COMMAND, NativeMethods.WM_PAINT, 0);
7: NativeMethods.PostMessage(GEHWnd, NativeMethods.WM_QT_PAINT, 0, 0);
8:
9: RECT mainRect = new RECT();
10: NativeMethods.GetWindowRect(GEHWnd, out mainRect);
11: clientRect = new RECT();
12: NativeMethods.GetClientRect(GEHrender, out clientRect);
13:
14: int offsetW = mainRect.Width - clientRect.Width;
15: int offsetH = mainRect.Height - clientRect.Height;
16:
17: int newWidth = this.Control.Width + (int)offsetW;
18: int newHeight = this.Control.Height + (int)offsetH;
19:
20: NativeMethods.SetWindowPos(GEHWnd, NativeMethods.HWND_TOP,
21: 0, 0, newWidth, newHeight,
22: NativeMethods.SWP_FRAMECHANGED);
23:
24: NativeMethods.SendMessage(GEHWnd, (uint)NativeMethods.WM_COMMAND, NativeMethods.WM_SIZE, 0);
25: }
2、鼠標消息
此例子中對於鼠標消息到處理使用了鉤子,調用HookAPI.dll實現。
1: /// <summary>
2: /// 鼠標鉤子
3: /// </summary>
4: private MouseHook mouseHook;
5:
6: // 設置鼠標鉤子
7: mouseHook = new MouseHook();
8: mouseHook.MouseClick += new MouseEventHandler (mouseHook_MouseClick);
9: mouseHook.MouseDbClick += new MouseEventHandler (mouseHook_MouseDbClick);
10: mouseHook.MouseWheel += new MouseEventHandler (mouseHook_MouseWheel);
11: // 啟動鼠標鉤子
12: mouseHook.StartHook(HookType.WH_MOUSE_LL, 0);
單擊事件:
1: /// <summary>
2: /// 鼠標鉤子。鼠標單擊事件
3: /// </summary>
4: /// <param name="sender"></param>
5: /// <param name="e"></param>
6: void mouseHook_MouseClick(object sender, MouseEventArgs e)
7: {
8: IntPtr hWnd = NativeMethods.WindowFromPoint(e.Location);
9: if (hWnd == this.GeRenderHWnd)
10: {
11: Point point = this.Control.PointToClient(e.Location);
12: // 如果鼠標擊點位置在控件內部,則說明鼠標點擊了GoogleEarth視圖
13: if (this.Control.ClientRectangle.Contains(point))
14: {
15: Console.WriteLine("點擊了GoogleEarth...");
16:
17: DoublePoint dp = ((GERenderPanel)Control).DetermineScreenCoordinates (point.X, point.Y);
18:
19: ParameterizedThreadStart pts = new ParameterizedThreadStart (ShowMouseClickPoint);
20:
21: Thread thread = new Thread(pts);
22: thread.Start(dp);
23:
24: }
25: }
26: }
27:
28: protected void ShowMouseClickPoint(object obj)
29: {
30: //Thread.Sleep(20);
31: DoublePoint dp = (DoublePoint)obj;
32: PointOnTerrainGE pGe = GeApp.GetPointOnTerrainFromScreenCoords(dp.X, dp.Y);
33: Console.WriteLine("鼠標點擊了:Lnt=" + pGe.Longitude.ToString()
34: + ";Lat=" + pGe.Latitude.ToString());
35: }
雙擊事件:
1: /// <summary>
2: /// 鼠標鉤子。鼠標雙擊事件
3: /// </summary>
4: /// <param name="sender"></param>
5: /// <param name="e"></param>
6: void mouseHook_MouseDbClick(object sender, MouseEventArgs e)
7: {
8: IntPtr hWnd = NativeMethods.WindowFromPoint(e.Location);
9: if (hWnd == this.GeRenderHWnd)
10: {
11: Point point = this.Control.PointToClient(e.Location);
12: // 如果鼠標擊點位置在控件內部,則說明鼠標點擊了GoogleEarth視圖
13: if (this.Control.ClientRectangle.Contains(point))
14: {
15: Console.WriteLine("xx雙擊了GoogleEarth...");
16:
17: DoublePoint dp = ((GERenderPanel) Control).DetermineScreenCoordinates(point.X, point.Y);
18:
19: ParameterizedThreadStart pts = new ParameterizedThreadStart (ShowMouseDbClickPoint);
20:
21: Thread thread = new Thread(pts);
22: thread.Start(dp);
23:
24: }
25: }
26: }
27:
28: protected void ShowMouseDbClickPoint(object obj)
29: {
30: //Thread.Sleep(20);
31: DoublePoint dp = (DoublePoint)obj;
32: PointOnTerrainGE pGe = GeApp.GetPointOnTerrainFromScreenCoords(dp.X, dp.Y);
33: Console.WriteLine("xx鼠標雙擊了:Lnt=" + pGe.Longitude.ToString()
34: + ";Lat=" + pGe.Latitude.ToString());
35:
36: MessageBox.Show("我還是出來一下吧!省得你不知道你已經雙擊了鼠標!");
37: }
這兩處代碼還比較簡陋,比如未添加主窗口焦點檢測,相信讀者可以自行添加。O(∩_∩)O~
3、截圖
程序中有兩種截圖功能,一種是GoogleEarth自帶的截圖功能,只能截取黑白圖片;另一種為彩色截圖 ,但是Vista以上操作系統不支持,還未找到合適的方法實現Vista與Win7兼容。
1) GoogleEarth自帶截圖功能:
1: GEViewContent view = GetGEView();
2:
3: if (view != null)
4: {
5: ApplicationGE ge = view.GeApplication;
6: if (ge != null && ge.IsInitialized() > 0)
7: {
8: using (SaveFileDialog sfd = new SaveFileDialog())
9: {
10: sfd.Filter = "jpg圖片|*.jpg";
11: sfd.AddExtension = true;
12: sfd.CheckPathExists = true;
13: sfd.Title = "保存Google Earth截圖";
14:
15: if (sfd.ShowDialog() == DialogResult.OK)
16: {
17: ge.SaveScreenShot(sfd.FileName, 100);
18: }
19: }
20: }
21: }
2) 彩色截圖:
1: GEViewContent view = GetGEView();
2: if (view != null)
3: {
4: int nWidth = view.Control.Width;
5: int nHeight = view.Control.Height;
6: Point pt = view.Control.PointToScreen(view.Control.Location);
7: int nXSrc = pt.X;
8: int nYSrc = pt.Y;
9:
10: IntPtr hRender = view.GeRenderHWnd;
11:
12: if (hRender != IntPtr.Zero)
13: {
14: // 取得Render DC
15: IntPtr hRenderDC = NativeMethods.GetWindowDC(hRender);
16: // 創建hBitmap
17: IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hRenderDC, nWidth, nHeight);
18: // 創建MEM DC
19: IntPtr hMemDC = NativeMethods.CreateCompatibleDC(hRenderDC);
20: // 將Bitmap Select到MemDC
21: NativeMethods.SelectObject(hMemDC, hBitmap);
22: // 直接拷屏
23: NativeMethods.BitBlt(hMemDC, 0, 0, nWidth, nHeight,
24: hRenderDC, 0, 0, 13369376);
25:
26: using(Bitmap bmp = Bitmap.FromHbitmap(hBitmap))
27: {
28: using(SaveFileDialog sfd = new SaveFileDialog())
29: {
30: sfd.Filter = "JPG圖片|*.jpg|PNG圖片|*.png";
31: sfd.AddExtension = true;
32: sfd.CheckPathExists = true;
33: sfd.Title = "保存Google Earth截圖";
34:
35: if (sfd.ShowDialog() == DialogResult.OK)
36: {
37: ImageFormat imgFormat = null;
38: // 默認選擇JPG
39: if (sfd.FilterIndex == 0)
40: {
41: imgFormat = ImageFormat.Jpeg;
42: }
43: // 選擇PNG
44: else
45: {
46: imgFormat = ImageFormat.Png;
47: }
48: bmp.Save(sfd.FileName, imgFormat);
49: }
50: }
51:
52: //銷毀資源
53: NativeMethods.DeleteDC(hRenderDC);
54: NativeMethods.DeleteDC(hMemDC);
55: }
56: }
OK,這篇GE開發到此為止,請讀者繼續等待後面的精彩文章…
不好意思,剛才忘記上傳源程序了!
http://files.cnblogs.com/wpwen/GEDemo_2009-05-22.rar