C # secondary development based on Google Earth

Source: Internet
Author: User

First, create a Windows Application in C. Then create a panel control in form1. Name it pnlearth.

Double-click form1 to enter form1.cs.

1. the following code:

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

}

/// & Lt; summary & gt;
/// Mount the map control of GE to the specified control
/// & Lt;/summary & gt;
/// & Lt; param name = "parentControl" & gt; c # Control & lt;/param & gt;
/// & Lt; param name = "geApplication" & gt; ge Application & lt;/param & gt;
Public void SetGEHandlerToControl (System. Windows. Forms. Control parentControl, IApplicationGE geApplication)
{
This. _ parentControl = parentControl;
This. _ googleEarth = geApplication;
_ GoogleEarth = new ApplicationGEClass ();
// Obtain the main form handle of GE
This. _ GEMainHandler = (IntPtr) this. _ googleEarth. GetMainHwnd ();
// Set the height and width of the GE main form to 0 to hide the GE main form
NativeMethods. SetWindowPos (this. _ GEMainHandler, NativeMethods. HWND_BOTTOM, 0, 0, 0, NativeMethods. SWP_NOSIZE + NativeMethods. SWP_HIDEWINDOW );

// Get the GE map control handle
This. _ GEHrender = (IntPtr) _ googleEarth. GetRenderHwnd ();
// Obtain the parent form handle of the GE map control
This. _ GEParentHrender = NativeMethods. GetParent (this. _ GEHrender );
// Set the parent form of the GE map control to invisible.
// (Considering that the GE map control may be intercepted by other programs, add this sentence to be)
NativeMethods. PostMessage (int) this. _ GEParentHrender, NativeMethods. WM_HIDE, 0, 0 );

// Set the parent form handle of the GE map control to the control on winform.
NativeMethods. SetParent (this. _ GEHrender, parentControl. Handle );
}


Public void ResizeGEControl ()
{
If (this. _ parentControl! = Null)
{
// Set the size of the GE map control to the size of the parent form.
NativeMethods. MoveWindow (this. _ GEHrender, 0, 0, this. _ parentControl. Width, this. _ parentControl. Height, true );
}
}
 
/// & Lt; summary & gt;
/// Release the GE handle
/// & Lt;/summary & gt;
Public void RealseGEHandler ()
{
Try
{
If (this. _ parentControl! = Null)
{
// Restore the handle of the GE map control to the GE main form
NativeMethods. SetParent (this. _ GEHrender, this. _ GEParentHrender );
// Close the GE main program
Nativemethods. postmessage (this. _ googleearth. getmainhwnd (), nativemethods. wm_quit, 0, 0 );
}
}
Finally
{
// In order to prevent the process of this program from successfully exiting and cause Ge problems, force kill the process of this program
System. Diagnostics. Process geprocess = system. Diagnostics. process. getcurrentprocess ();
Geprocess. Kill ();
}
}

 

Private void form=formclosing (object sender, FormClosingEventArgs e)
{
RealseGEHandler ();
}

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

 

}
}

2. Create a new class and enter the class wizard named NativeMethods. cs.

Add the following code:

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); // obtain the window Handle Based on the cursor position


# Region pre-defined
 
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. added the google earthapi

In the menu bar, select project> Add reference> COM, and then select Google Earth 1.0 Type Library.

 

After that, google earth is embedded into the panel control for secondary development.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.