comac_desk_app/GenerateReport/CreateNewReport.cpp

208 lines
6.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <cstdlib> // For std::putenv
#include <cstring> // For std::strdup
#include <tchar.h>
#include <qwidget.h>
#include <QHBoxLayout>
#include <QIcon>
#include <QVBoxLayout>
#include "windows.h"
#include "QWindow.h"
#include <tlhelp32.h>
#include <QProcess>
#include <QAxWidget>
#include "T_CreateNewReportWidget.h"
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/document/IndexedPropertyValues.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <cppuhelper/bootstrap.hxx>
#include <osl/mutex.hxx>
#include <rtl/ustring.hxx>
#include <iostream>
#include <sal/main.h>
#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/process.h>
#include <rtl/ustrbuf.hxx>
#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<HWND*>(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<LPARAM>(&_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<PropertyValue> loadProps;
css::uno::Reference<XComponent> xComponent = _xDesktop->loadComponentFromURL(
OUString("private:factory/swriter"),
OUString("_blank"),
0,
loadProps);
if (!xComponent.is()) {
std::cerr << "无法加载文本文档" << std::endl;
return;
}
// 获取文本文档对象
css::uno::Reference<css::text::XTextDocument> 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<css::text::XText> xText = xTextDocument->getText();
#if 1
//用于测试数据输入
// 创建文本游标
css::uno::Reference<css::text::XTextCursor> 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);
}
}