通過調(diào)用Windows API隱藏GoogleEarth界面
作者:wpwen
本文在《C#調(diào)用GoogleEarth Com API》的基礎(chǔ)上展示如何調(diào)用Windows API隱藏GoogleEarth的界面。
繼《C#調(diào)用GoogleEarth Com API》,我又帶給大家第二篇文章。這一篇文章在第一篇的基礎(chǔ)上,展示如何調(diào)用Windows API隱藏GoogleEarth的界面,并將GoogleEarth的地圖顯示在自定義的窗體上。廢話少說,直接上代碼。
1、主窗口代碼:
// 功能:GE實例(二)
// 描述:GE COM API 網(wǎng)址:http://earth.google.com/comapi/index.html
// 作者:溫偉鵬
// 日期:2009-02-08
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 GEDemo
{
public partial class Form2 : Form
{
/// < summary>
/// 用來關(guān)閉GoogleEarth的消息定義
/// < /summary>
static readonly Int32 WM_QUIT = 0x0012;
private IntPtr GEHWnd = (IntPtr)5;
private IntPtr GEHrender = (IntPtr)5;
private IntPtr GEParentHrender = (IntPtr)5;
/// < summary>
/// 定義GE應(yīng)用程序類
/// < /summary>
private ApplicationGEClass GeApp;
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!this.DesignMode)
{
GeApp = new ApplicationGEClass();
GEHWnd = (IntPtr)GeApp.GetMainHwnd();
NativeMethods.SetWindowPos(GEHWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0,
NativeMethods.SWP_NOSIZE + NativeMethods.SWP_HIDEWINDOW);
GEHrender = (IntPtr)GeApp.GetRenderHwnd();
GEParentHrender = (IntPtr)NativeMethods.GetParent(GEHrender);
NativeMethods.MoveWindow(GEHrender, 0, 0, this.Width, this.Height, true);
NativeMethods.SetParent(GEHrender, this.Handle);
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
NativeMethods.PostMessage(GeApp.GetMainHwnd(), WM_QUIT, 0, 0);
}
}
}
2、NativeMethods類定義:
// 功能:Windows API調(diào)用
// 描述:大家可以參照MSDN
// 作者:溫偉鵬
// 日期:2009-02-08
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace GEDemo
{
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 預(yù)定義
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 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;
}
}
3、執(zhí)行效果:
現(xiàn)在,調(diào)用Windows API隱藏GoogleEarth界面的效果就達(dá)到了。
【編輯推薦】
- ??C#程序中的數(shù)據(jù)顯 示:自定義標(biāo)簽和XML、XSL??
- ??C#自定義事件是如何生成的??
- ??C# 自定義控件dll文件的生成步驟??
- ??C#自定義快捷鍵的實現(xiàn)??
- ??C#自定義事件的步驟介紹??
責(zé)任編輯:book05
來源:
cnblogs