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.
kev/Drawer/TestDrawerDll/TestDrawerDllDlg.cpp

309 lines
7.8 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// TestDrawerDllDlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "TestDrawerDll.h"
#include "TestDrawerDllDlg.h"
#include "afxdialogex.h"
#include "DrawerData.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CTestDrawerDllDlg 对话框
CTestDrawerDllDlg::CTestDrawerDllDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_TESTDRAWERDLL_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDrawerDllDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CTestDrawerDllDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_EXCUTE_TEST, &CTestDrawerDllDlg::OnBnClickedButtonExcuteTest)
ON_BN_CLICKED(IDC_BUTTON_TEST_OPENFILE, &CTestDrawerDllDlg::OnBnClickedButtonTestOpenfile)
ON_BN_CLICKED(IDC_BUTTON_TEST_LICENSE, &CTestDrawerDllDlg::OnBnClickedButtonTestLicense)
ON_BN_CLICKED(IDC_BUTTON_TEST_3D, &CTestDrawerDllDlg::OnBnClickedButtonTest3d)
END_MESSAGE_MAP()
// CTestDrawerDllDlg 消息处理程序
BOOL CTestDrawerDllDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CTestDrawerDllDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CTestDrawerDllDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTestDrawerDllDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void HandleException(const std::string& functionName, int lineNumber, const std::exception& e)
{
std::ostringstream oss;
oss << "❌ 发生异常!\n";
oss << "🔹 函数:" << functionName << "\n";
oss << "🔹 行号:" << lineNumber << "\n";
oss << "🔹 错误信息:" << e.what() << "\n";
// 转换为 CStringMessageBox 需要 CString
CString errorMsg(oss.str().c_str());
// 弹出错误对话框
//AfxMessageBox(nullptr, errorMsg, _T("错误"),"", MB_OK | MB_ICONERROR);
MessageBox(nullptr, errorMsg, _T("错误"), MB_OK | MB_ICONERROR);
}
void CTestDrawerDllDlg::OnBnClickedButtonExcuteTest()
{
CXy* pXy = nullptr;
try
{
pXy = reinterpret_cast<CXy*>(XyCreate()); // 手动管理内存
if (!pXy)
{
throw std::runtime_error("XyCreate() 返回了空指针");
}
pXy->Clear(); // 可能会抛出异常
//::AfxMessageBox("运行正常!");
AfxGetMainWnd()->MessageBox(_T("运行正常!"), _T("提示"), MB_OK | MB_ICONINFORMATION);
}
catch (const std::exception& e)
{
HandleException(__FUNCTION__, __LINE__, e);
}
catch (...)
{
CString errorMsg;
errorMsg.Format(_T("❌ 发生未知异常!(函数:%s行号%d"), _T(__FUNCTION__), __LINE__);
::MessageBox(nullptr, errorMsg, _T("错误"), MB_OK | MB_ICONERROR);
}
// 确保 delete 执行,防止内存泄漏
if (pXy)
{
delete pXy;
pXy = nullptr;
}
}
void CTestDrawerDllDlg::OnBnClickedButtonTestOpenfile()
{
CXy* pXy = nullptr;
//try
//{
pXy = reinterpret_cast<CXy*>(XyCreate()); // 手动管理内存
//if (!pXy)
//{
// throw std::runtime_error("XyCreate() 返回了空指针");
//}
CFileDialog dlg(TRUE, _T("打开文件"), nullptr, OFN_FILEMUSTEXIST
, _T("KEV 文件(*.kev)|*.kev|所有文件(*.*)|*.*||"));
if (dlg.DoModal() == IDOK)
{
CString strFile = dlg.GetPathName();
XyOpenFile(pXy, strFile, false);
XyDeleteLayerElement(pXy, "产能", true);
CString strContent;
strContent.Append("Layer M 产能\\油柱\n");
strContent.Append("Draw\n");
strContent.Append("21615191.33,5030893.10444444\n");
strContent.Append("21615341.33,5031634.10444444\n");
strContent.Append("油柱\n");
strContent.Append("0 2053\n");
strContent.Append("0,0\n");
strContent.Append("油柱\n");
strContent.Append("1,1\n");
strContent.Append("\n");
XyAddBufferData(pXy, strContent);
XySaveAs(pXy, "TestOut.dfd");
}
//pXy->Clear(); // 可能会抛出异常
AfxGetMainWnd()->MessageBox(_T("打开文件功能运行正常!"), _T("提示"), MB_OK | MB_ICONINFORMATION);
//}
//catch (const std::exception& e)
//{
// HandleException(__FUNCTION__, __LINE__, e);
//}
//catch (...)
//{
// CString errorMsg;
// errorMsg.Format(_T("❌ 发生未知异常!(函数:%s行号%d"), _T(__FUNCTION__), __LINE__);
// ::MessageBox(nullptr, errorMsg, _T("错误"), MB_OK | MB_ICONERROR);
//}
// 确保 delete 执行,防止内存泄漏
if (pXy)
{
delete pXy;
pXy = nullptr;
}
}
extern "C" __declspec(dllimport) bool MachineValidate(LPCTSTR licFile);
void CTestDrawerDllDlg::OnBnClickedButtonTestLicense()
{
try
{
CString strMessage("");
TCHAR bufferPath[MAX_PATH] = { 0 };
HMODULE hModule = GetModuleHandle(nullptr); // nullptr 表示当前模块
GetModuleFileName(hModule, bufferPath, MAX_PATH);
PathRemoveFileSpec(bufferPath);
CString strLic;
strLic.Format("%s\\%s", bufferPath, "Constrction.lic");
bool bValidate = MachineValidate(strLic);
if (bValidate == false) {
strMessage.Format("运行错误E001请联系客户服务人员!");
AfxGetMainWnd()->MessageBox(_T("打开文件功能运行正常!\r\n运行错误E001"), _T("提示"), MB_OK | MB_ICONINFORMATION);
}
AfxGetMainWnd()->MessageBox(_T("许可运行正常!"), _T("提示"), MB_OK | MB_ICONINFORMATION);
}
catch (const std::exception& e)
{
HandleException(__FUNCTION__, __LINE__, e);
}
catch (...)
{
CString errorMsg;
errorMsg.Format(_T("❌ 发生未知异常!(函数:%s行号%d"), _T(__FUNCTION__), __LINE__);
::MessageBox(nullptr, errorMsg, _T("错误"), MB_OK | MB_ICONERROR);
}
}
extern "C" __declspec(dllimport) bool InitializeQtApplication();
//__declspec(dllimport) int KVDLN_Init();
void CTestDrawerDllDlg::OnBnClickedButtonTest3d()
{
// Function to initialize QApplication
InitializeQtApplication();
//KVDLN_Init();
}