#ifndef MAPINFOFORM_H
#define MAPINFOFORM_H
#include <QWidget>
#include <QTimer>
#include <QProcess>
#include <Windows.h>
#include <TlHelp32.h>
#include <QWindow>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QStringListModel>
namespace Ui {
class MapInfoForm;
}
class MapInfoForm : public QWidget
{
Q_OBJECT
public:
explicit MapInfoForm(QWidget *parent = nullptr);
~MapInfoForm();
void updateMapInfo();
void initWeb();
void sendMessage(QString url);
bool KillProcess(DWORD ProcessId);
bool FindProcess(const char * name, DWORD &pid);
private slots:
void timerMapSlot();
private:
Ui::MapInfoForm *ui;
QWindow * m_window;
QTimer *timermap=new QTimer(this);
const quint16 PORT = 3333;
QUdpSocket qus;
};
#endif // MAPINFOFORM_H
#include "mapinfoform.h"
#include "ui_mapinfoform.h"
MapInfoForm::MapInfoForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MapInfoForm)
{
ui->setupUi(this);
model=NULL;
simulationForm=NULL;
connect(timermap, SIGNAL(timeout()), this, SLOT(timerMapSlot()));
timermap->stop();
timermap->start(50);
initWeb();
}
void MapInfoForm::sendMessage(QString url){
QByteArray msg;
msg.append("MapInfostrart");
msg.append(url);
msg.append("MapInfoend");
qus.writeDatagram(msg, QHostAddress("127.0.0.1"), PORT);
}
void MapInfoForm::timerMapSlot(){
WId wid = (WId)FindWindow(L"Qt5QWindowIcon", L"MapWidget");
if(wid==0){
return;
}
m_window = QWindow::fromWinId(wid);
m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
m_window->show();
QWidget *m_widget;
m_widget = QWidget::createWindowContainer(m_window, this->ui->widgetMap);
QVBoxLayout *mainLayout = new QVBoxLayout(ui->widgetMap);
mainLayout->addWidget(m_widget);
ui->widgetMap->setLayout(mainLayout);
timermap->stop();
}
void MapInfoForm::initWeb(){
DWORD pid;
bool ret = FindProcess("Map.exe", pid);
if (ret)
{
KillProcess(pid);
}
QString cmd = QCoreApplication::applicationDirPath()+"/Mapbin/Map.exe";
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = true;
CreateProcess(
NULL,
(LPWSTR)cmd.toStdWString().c_str(),
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL, &si, &pi);
}
void MapInfoForm::updateMapInfo(){
sendMessage("http://172.16.2.169:8099/map/map.html");
}
bool MapInfoForm::KillProcess(DWORD ProcessId)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, ProcessId);
if (hProcess == NULL)
return FALSE;
if (!TerminateProcess(hProcess, 0))
return FALSE;
return TRUE;
}
bool MapInfoForm::FindProcess(const char * name, DWORD &pid)
{
int i = 0;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
i += 0;
}
bool bMore = ::Process32First(hProcessSnap, &pe32);
while (bMore)
{
LPTSTR pf = (LPTSTR)(LPCTSTR)pe32.szExeFile;
char *pFileName = (char *)malloc(2 * wcslen(pf) + 1);
wcstombs(pFileName, pf, 2 * wcslen(pf) + 1);
if (stricmp(name, pFileName) == 0)
{
i += 1;
pid = pe32.th32ProcessID;
return true;
}
bMore = ::Process32Next(hProcessSnap, &pe32);
}
if (i > 0){
return true;
}
else{
return false;
}
}
MapInfoForm::~MapInfoForm()
{
DWORD pid;
bool ret = FindProcess("Map.exe", pid);
if (ret)
{
KillProcess(pid);
}
qDebug()<<" pid "<<pid;
if(m_window!=NULL){
delete m_window;
}
if(timermap!=NULL){
delete timermap;
}
delete ui;
}