Qt 开发 MS VC 控件终极篇
1. 使用 MSVC2015 通过项目向导创建 Qt ActiveQt Server
解决方案
项目配置:以下文件需要修改
1. 项目属性页->项目属性->常规->目标文件扩展名
2. 项目属性页->项目属性->链接器->所有选项->输出文件
*.dll 修改为 *.exe 文件
3. 项目属性页->项目属性->常规->配置类型
生成文件 修改为 *.exe
2. 新建 main.cpp
源文件添加以下内容
main.cpp
//# 静态编译需要
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
// End 静态编译需要
#include <QApplication>
#include <QAxFactory>
#include "ActiveQtServer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!QAxFactory::isServer()) {
ActiveQtServer1 w;
w.show();
return app.exec();
}
return app.exec();
}
//导出接口
QAXFACTORY_BEGIN("{ad90301a-849e-4e8b-9a91-0a6dc5f6461f}","{a8f21901-7ff7-4f6a-b939-789620c03d83}")
//导出类
QAXCLASS(ActiveQtServer1)
QAXFACTORY_END()
3. 其他文件如下所示
ActiveQtServer.h
#pragma once
#include <QtWidgets/QWidget>
#include <ActiveQt/QAxBindable>
#include "ui_ActiveQtServer.h"
//# 设置内存执行编码 UTF-8
#ifdef Q_OS_WIN
#pragma execution_character_set("UTF-8")
#endif
//# 控件安全标记类
#include <QAxAggregated>
#include <objsafe.h>
#include <QUuid>
class ObjectSafety : public QAxAggregated, public IObjectSafety
{
public:
ObjectSafety(){
}
QAXAGG_IUNKNOWN;
long queryInterface(const QUuid &iid, void **iface)
{
*iface = NULL;
if (iid == IID_IObjectSafety)
{
*iface = (IObjectSafety*)this;
}
else
{
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
HRESULT WINAPI GetInterfaceSafetyOptions(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
{
*pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
*pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA | INTERFACESAFE_FOR_UNTRUSTED_CALLER;
return S_OK;
}
HRESULT WINAPI SetInterfaceSafetyOptions(REFIID riid, DWORD pdwSupportedOptions, DWORD pdwEnabledOptions)
{
return S_OK;
}
};
//导出控件类
class ActiveQtServer : public QWidget, public QAxBindable
{
Q_OBJECT
//声明类信息
Q_CLASSINFO("ClassID", "{1D9928BD-4453-4bdd-903D-E525ED17FDE5}")
Q_CLASSINFO("InterfaceID", "{99F6860E-2C5A-42ec-87F2-43396F4BE389}")
Q_CLASSINFO("EventsID", "{0A3E9F27-E4F1-45bb-9E47-63099BCCD0E3}")
public:
ActiveQtServer(QWidget *parent = Q_NULLPTR);
public Q_SLOTS:
QString Version();
private:
Ui::ActiveQtServerClass ui;
//重写 QAxBindable 类 createAggregate 方法
QAxAggregated* createAggregate();
};
ActiveQtServer.cpp
#include "ActiveQtServer.h"
#include <ActiveQt/QAxFactory>
ActiveQtServer::ActiveQtServer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
QString ActiveQtServer::Version()
{
return QString("Hello Word!");
}
QAxAggregated * ActiveQtServer::createAggregate()
{
return new ObjectSafety;
}
ActiveQtServer.def
; Declares the module parameters.
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
DumpIDL PRIVATE
4. HTML 文件如下所示
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=8" >
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" >
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ActiveQtServer</title>
</head>
<body>
<div>
<div>
<object id="ActiveQtServer" width="0" height="0" classid="CLSID:1D9928BD-4453-4bdd-903D-E525ED17FDE5"></object>
</div>
<input type="button" onclick="alert(ActiveQtServer.Version())" value="获取版本信息"/>
</div>
</body>
<script>
</script>
</html>