1. 程式人生 > >將Google Earth加載彩票平臺搭建入Qt地圖界面

將Google Earth加載彩票平臺搭建入Qt地圖界面

pri define click kml 官網 傳說 slot ogl 能夠

將Google Earth加載彩票平臺搭建論壇:haozbbs.com Q1446595067 入Qt地圖界面

本文采用Qt5.9.3,創建簡單界面,能夠借助Com組件打開Google Earth.exe應用程序,可進行更深層開發,做出地圖顯示界面。雖然Google Earth在新版本中已經將Api接口關閉,但是老版還是可以用的,這個使那個老版安裝包,我上傳說我資源重復,大家自行查找吧,官網應該也有的。世上本沒有路,走到人多了就成了路,大家共同使用谷歌地球,說不定哪天接口又會開啟了呢。
這裏寫圖片描述

Com基礎知識及Google Earth相關書籍
程序效果圖
程序講解
代碼出處

Com基礎知識及面向對象編程思想

一些基本東西我就不多說了,提供幾個關鍵詞,可自行查閱: Com對象和接口,面向對象編程思想,《智慧地球:Google Earth/Maps/Kml核心開發技術揭秘》,《Google API開發詳解:Google Maps與Google Earth雙劍合璧(第2版)》 書中有更加詳細的Google Earth API講解。
程序效果圖

這裏寫圖片描述
點擊按鈕打開谷歌地球
這裏寫圖片描述
程序講解

老套路,不多說了直接上代碼:
google.h文件

#ifndef GOOGLE_H
#define GOOGLE_H

#include <QMainWindow>
#include <guiddef.h>

//#include "windows.h"
#include "googleearth.tlh"

#import "C:\Program Files (x86)\Google\Google Earth\client\googleearth.exe" no_namespace

static const CLSID CLSID_ApplicationGE ={0x8097D7E9,0xDB9E,0x4AEF, {0x9B,0x28,0x61,0xD8,0x2A,0x1D,0xF7,0x84}};
static const CLSID IID_IApplicationGE ={0x2830837B,0xD4E8,0x48C6, {0xB6,0xEE,0x04,0x63,0x33,0x72,0xAB,0xE4}};

namespace Ui {
class Google;
}

class Google : public QMainWindow
{
Q_OBJECT

public:
explicit Google(QWidget *parent = 0);
~Google();

static void KillProcess(const QString& strProcName);

public slots:

void StartGE(void);

private:
IApplicationGE* m_GEApp;

HWND m_GEParentHandle;//GE視圖父窗口句柄
HWND m_GEHandle;    //GE視圖區窗口句柄
HWND m_GeMainHandle;//GE主窗體句柄

Ui::Google *ui;

};

#endif // GOOGLE_H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

google.cpp文件

#include "google.h"
#include "ui_google.h"
#include "windows.h"
#include "windef.h"
#include "tchar.h"

Google::Google(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Google)
{
ui->setupUi(this);

//試圖清除其他事先運行的GE
this->KillProcess("Google Earth");
this->KillProcess("GoogleEarth");
this->KillProcess("GoogleCrashHandler");

connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(StartGE()));

}

Google::~Google()
{
delete ui;
}

void Google::KillProcess(const QString &strProcName)
{
DWORD dwProcID;
HANDLE hProcess;
TCHAR wProcName[260];
int Len = strProcName.toWCharArray(wProcName);
wProcName[Len] = _T(‘\0‘);
HWND h = FindWindow(0,wProcName);
if(h)
{
GetWindowThreadProcessId(h,&dwProcID);
hProcess = OpenProcess(PROCESS_TERMINATE,JOB_OBJECT_ASSIGN_PROCESS,dwProcID);
TerminateProcess(hProcess,0);
}
}

void Google::StartGE()
{
HRESULT hr;
hr = CoCreateInstance(CLSID_ApplicationGE,NULL,CLSCTX_LOCAL_SERVER,IID_IApplicationGE,(void**) &m_GEApp);
if(SUCCEEDED(hr))
{
m_GeMainHandle = (HWND)m_GEApp->GetMainHwnd();//隱藏啟動的GE
::SetWindowPos(m_GeMainHandle, HWND_BOTTOM, 0 , 0, 0, 0, SWP_NOSIZE|SWP_HIDEWINDOW);
m_GEHandle =(HWND) m_GEApp->GetRenderHwnd();//截獲GE的視圖區
RECT rect;
::GetWindowRect((HWND)this->winId(), &rect);//取得當前視圖窗口客戶區大小
m_GEParentHandle = ::GetParent(m_GEHandle);//保存GE視圖區舊的父窗體句柄
::SetParent(m_GEHandle, (HWND)this->winId());//截獲GE視圖

    ::SetWindowPos(m_GeMainHandle,HWND_BOTTOM,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top+50,SWP_FRAMECHANGED);//顯示GE
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

具體說明代碼裏面都有,這裏唯一需要註意以下,編譯器采用VC編譯器,因為MinGW的話,需要用qt自帶的ActiceX什麽類(具體忘記了),比較費事,采用VC的話,可以直接#import “exe”,然後自動生成tlh與tli文件。運行時候需要先將#include “googleearth.tlh”註釋掉,因為還沒有生成tlh文件,編譯之後,將其回復,則可以調用IApplicationGE* m_GEApp類了。剩下的看看書,再做一些二次開發就可以了,順帶提一下結合kml文件也是非常不錯的選擇。
代碼出處

將Google Earth加載彩票平臺搭建入Qt地圖界面