You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.3 KiB
C++

// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include "qmfcapp.h"
#include "qwinwidget.h"
#include <QMessageBox>
#include <windows.h>
#include <QPushButton>
#include <QtWidgets/QHBoxLayout>
#include "mainwidget.h"
#include "viewwidget.h"
#include "ViewOperator.h"
// Global pointers to QApplication and QWinWidget
QApplication* qtApp = nullptr;
QWinWidget *winWidget = nullptr;
ViewOperator* controller = nullptr;
// Original window procedure pointer
WNDPROC originalWndProc = nullptr;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
//static bool ownApplication = FALSE;
//if (ul_reason_for_call == DLL_PROCESS_ATTACH)
// ownApplication = QMfcApp::pluginInstance(hModule);
//if (ul_reason_for_call == DLL_PROCESS_DETACH && ownApplication)
//{
// if (ownApplication && qApp)
// {
// // Added protection to avoid double delete
// if (!QCoreApplication::closingDown())
// {
// if (winWidget != nullptr)
// {
// delete winWidget;
// }
// delete qApp; // Safely delete QApplication
// //qApp = nullptr; // Set to nullptr to avoid future issues
// }
// }
//}
return TRUE;
}
extern "C" __declspec(dllexport) bool showDialog(HWND parent)
{
QWinWidget win(parent);
win.showCentered();
QMessageBox::about(&win, "About GDF Facies", "GDF Facies Version 1.0\nCopyright (C) 2024");
return TRUE;
}
extern "C" __declspec(dllexport) void InitializeQtApplication()
{
if (qtApp == nullptr)
{
// Qt Application initialization
int argc = 0;
qtApp = new QApplication(argc, nullptr);
}
}
// Custom window procedure to handle WM_SIZE
LRESULT CALLBACK CustomWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_SIZE && winWidget != nullptr)
{
// Get new width and height from lParam
int width = LOWORD(lParam);
int height = HIWORD(lParam);
// Resize the QWinWidget to match the parent size
winWidget->setFixedSize(width, height);
// Force a repaint to ensure the widget is redrawn with the new size
winWidget->update(); // or winWidget->repaint();
// Force parent window to update its layout
UpdateWindow(hwnd);
}
// Call the original window procedure for default handling
return CallWindowProc(originalWndProc, hwnd, uMsg, wParam, lParam);
}
extern "C" __declspec(dllexport) void EmbedQtWidget(HWND parentHandle)
{
if (winWidget == nullptr)
{
winWidget = new QWinWidget(parentHandle);
winWidget->resize(1000, 800);
QHBoxLayout *layout = new QHBoxLayout();
winWidget->setLayout(layout);
// Create the QWidget (or custom QWidget)
ViewWidget* viewWidget = new ViewWidget();
//viewWidget->setStyleSheet("border: 1px solid black");
// Embed the QWidget into the QWinWidget
layout->addWidget(viewWidget);
winWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
viewWidget->resize(winWidget->size());
viewWidget->move(0, 0);
// Show the widget inside the WinForms application
winWidget->move(0, 0);
winWidget->show();
controller = new ViewOperator(viewWidget);
}
}
extern "C" __declspec(dllexport) void ResizeQtWidget(int width, int height)
{
if (winWidget != nullptr)
{
winWidget->setFixedSize(width, height); // Resize the QWinWidget to match the WinForms panel size
winWidget->move(0, 0);
}
}
extern "C" __declspec(dllexport) void ShutdownQtApplication()
{
//// Restore the original window procedure
//if (originalWndProc != nullptr && winWidget != nullptr)
//{
// SetWindowLongPtr(winWidget->parentWindow(), GWLP_WNDPROC, (LONG_PTR)originalWndProc);
//}
if (winWidget) {
delete winWidget;
winWidget = nullptr;
}
// Safely delete the QApplication instance
if (qtApp)
{
delete qtApp;
qtApp = nullptr;
}
}
extern "C" __declspec(dllexport)
bool LoadGrid(int numx, int numy, double x0, double y0, double dx, double dy, double* values, double zMin = -1E300, double zMax = 1E300)
{
return controller->LoadGrid(numx, numy, x0, y0, dx, dy, values, zMin, zMax);
}
extern "C" __declspec(dllexport)
bool LoadGridFile(LPCTSTR gridFile) {
QString strFile = QString::fromLocal8Bit(gridFile);
return controller->LoadDfg(strFile);
}
extern "C" __declspec(dllexport)
void ViewBlackWhite() {
controller->ViewBlackWhite();
}
extern "C" __declspec(dllexport)
void ViewColorful()
{
controller->ViewColorFul();
}
extern "C" __declspec(dllexport)
void CreateLines()
{
controller->CreateLines();
}
extern "C" __declspec(dllexport)
double GetFaciesZMin() {
return controller->m_faciesZMin;
}
extern "C" __declspec(dllexport)
void SetFaciesZMin(double zValue) {
controller->m_faciesZMin = zValue;
}
extern "C" __declspec(dllexport)
double GetFaciesZMax() {
return controller->m_faciesZMax;
}
extern "C" __declspec(dllexport)
void SetFaciesZMax(double zValue) {
controller->m_faciesZMax = zValue;
}
extern "C" __declspec(dllexport)
int GetDilate() {
return controller->m_dilate;
}
extern "C" __declspec(dllexport)
void SetDilate(int dilate) {
controller->m_dilate = dilate;
}
extern "C" __declspec(dllexport)
double GetFaciesAreaMin() {
return controller->m_areaMin;
}
extern "C" __declspec(dllexport)
void SetFaciesAreaMin(double area)
{
controller->m_areaMin = area;
}
extern "C" __declspec(dllexport)
int GetFaciesSmoothTimes() {
return controller->m_smoothTimes;
}
extern "C" __declspec(dllexport)
void SetFaciesSmoothTimes(int times)
{
controller->m_smoothTimes = times;
}
extern "C" __declspec(dllexport)
bool GetDilateMax(int& diLateMax)
{
return controller->GetDilateMax(diLateMax);
}
extern "C" __declspec(dllexport)
bool GetSurfaceZRange(double & zMin, double & zMax)
{
return controller->GetSurfaceZRange(zMin, zMax);
}
extern "C" __declspec(dllexport)
bool GetSurfaceParameter(int & xNum, int & yNum, double & deltaX, double & deltaY, double & zMin, double & zMax)
{
return controller->GetSurfaceParameter(xNum, yNum, deltaX, deltaY, zMin, zMax);
}
extern "C" __declspec(dllexport)
const char * GetContourData()
{
return controller->WriteContours();
}
extern "C" __declspec(dllexport)
const void * GetContourDataPtr()
{
CPolygonTreeInterface* pPolygons = controller->GetContourPtr();
return pPolygons;
}
extern "C" __declspec(dllexport)
void DeletePointerArray(char* data) {
delete[] data;
}