#include // For std::putenv #include // For std::strdup #include #include #include #include #include #include "windows.h" #include "QWindow.h" #include #include #include #include "T_CreateNewReportWidget.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "MainWindow.h" #include "CreateNewReport.h" #include "GenerateReportBase.h" #include "AutomaticallySaveReport.h" using namespace std; using namespace com::sun::star::uno; using namespace com::sun::star::lang; using namespace com::sun::star::beans; using namespace com::sun::star::bridge; using namespace com::sun::star::frame; using namespace com::sun::star::sheet; using namespace com::sun::star::registry; using namespace com::sun::star::text; using namespace rtl; using com::sun::star::beans::PropertyValue; using com::sun::star::text::XTextDocument; using com::sun::star::text::XText; using com::sun::star::text::XTextCursor; DWORD GetProcessIdByName(const char* processName) { DWORD processId = 0; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 processEntry; processEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(snapshot, &processEntry)) { do { if (strcmp(processEntry.szExeFile, processName) == 0) { processId = processEntry.th32ProcessID; break; } } while (Process32Next(snapshot, &processEntry)); } CloseHandle(snapshot); } return processId; } HWND findWindowByProcessId(DWORD processId) { static DWORD _processId = 0; _processId = processId; HWND _hwnd = NULL; EnumWindows([](HWND hWnd, LPARAM lParam) -> BOOL { DWORD _windowProcessId; GetWindowThreadProcessId(hWnd, &_windowProcessId); if (_windowProcessId == _processId) { // 如果找到匹配的窗口句柄,保存到hwnd HWND* _foundHwnd = reinterpret_cast(lParam); *_foundHwnd = hWnd; TCHAR windowTitle[256]; GetWindowText(*_foundHwnd, windowTitle, sizeof(windowTitle) / sizeof(TCHAR)); TCHAR targetWindowName[] = _T("LibreOffice"); TCHAR* result = _tcsstr(windowTitle, targetWindowName); if (QString(windowTitle).isEmpty() || result == NULL) { return TRUE; } return FALSE; // 找到窗口后停止枚举 } return TRUE; // 继续枚举 }, reinterpret_cast(&_hwnd)); return _hwnd; } namespace GenerateReport { CreateNewReport::CreateNewReport() { } //test1::~Test1() //{ //} void CreateNewReport::actionSlot_CreateNewReport() { _aboutPage = new T_CreateNewReportWidget(); _aboutPage->hide(); _aboutPage->setFixedSize(400, 200); _aboutPage->moveToCenter(); _aboutPage->show(); } void CreateNewReport::accept() { CreateNewDocument(); } void CreateNewReport::CreateNewDocument() { auto _xDesktop = GenerateReportBaseInstance->getDesktop(); // 加载文本文档 //yr-还需替换为设置的报告名称 css::uno::Sequence loadProps; css::uno::Reference xComponent = _xDesktop->loadComponentFromURL( OUString("private:factory/swriter"), OUString("_blank"), 0, loadProps); if (!xComponent.is()) { std::cerr << "无法加载文本文档" << std::endl; return; } // 获取文本文档对象 css::uno::Reference xTextDocument(xComponent, UNO_QUERY); if (!xTextDocument.is()) { std::cerr << "无法获取文本文档对象" << std::endl; return; } GenerateReportBaseInstance->setTextDocument(xTextDocument); GenerateReportBaseInstance->setCurrentReportNumber(1); //auto _xTextDocument = GenerateReportBase::getTextDocument(); // 获取文本对象 css::uno::Reference xText = xTextDocument->getText(); #if 1 //用于测试数据输入 // 创建文本游标 css::uno::Reference xTextCursor = xText->createTextCursor(); // 在文档开头插入数据 xTextCursor->gotoStart(false); OUString ouString = OUString::fromUtf8("English---Hello, World! ; num----123456 中文:这些是中文"); //OUString ouString = OUString::createFromAscii("English---Hello, World! ; num----123456 中文:这些是中文"); xTextCursor->setString(ouString); // 在文档结尾插入数据 xTextCursor->gotoEnd(false); xTextCursor->setString(OUString::fromUtf8("这是插入到文本文档结尾的内容。")); std::cout << "successfully insert" << std::endl; #endif GenerateReport::AutomaticallySaveReport _autoSave; _autoSave.AutomaticallySaveReportExcute(); const char* targetProcessName = "soffice.bin"; DWORD processId = GetProcessIdByName(targetProcessName); auto _hwnd = findWindowByProcessId(processId); GenerateReport::GenerateReportBase::getinstance()->getMainWindow()->libreOfficeProcess((WId)_hwnd); } }