Qt create 如何构建 ActiveX 控件?

ActiveX.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-01-19T09:37:39
#
#-------------------------------------------------
# Qt shared
QT_CI_JENKINS_HOME=$$(JENKINS_HOME)
!isEmpty(QT_CI_JENKINS_HOME) {
    message("Qt CI environment detected, suppressing example registration")
    CONFIG += qaxserver_no_postlink
}

TEMPLATE = app
TARGET   = ActiveX
CONFIG += warn_off
QT += core widgets axserver
SOURCES += \
        main.cpp \
        activex.cpp \

HEADERS += \
        activex.h

FORMS += \
        activex.ui

RC_FILE  = ActiveX.rc
DEF_FILE = ActiveX.def

activex.h

#ifndef ACTIVEX_H
#define ACTIVEX_H

#include <QWidget>
#include <QtCore>
#include <ActiveQt>


//# 设置内存执行编码 UTF-8
#ifdef Q_OS_WIN
#pragma execution_character_set("UTF-8")
#endif

// ObjectSafety
//# 控件安全标记类
#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;
    }
};
//End ObjectSafety

namespace Ui {
class ActiveX;
}

class ActiveX : public QWidget,public QAxBindable
{
    Q_OBJECT
    Q_CLASSINFO("ClassID",     "{DF16845C-92CD-4AAB-A982-EB9840E74669}")
    Q_CLASSINFO("InterfaceID", "{616F620B-91C5-4410-A74E-6B81C76FFFE0}")
    Q_CLASSINFO("EventsID",    "{E1816BBA-BF5D-4A31-9855-D6BA432055FF}")
public:
    explicit ActiveX(QWidget *parent = 0);
    ~ActiveX();
public Q_SLOTS:
    QString Version();
private:
    Ui::ActiveX *ui;
    QAxAggregated* createAggregate()
    {
        return new ObjectSafety;
    }
};

#endif // ACTIVEX_H

activex.cpp

#include "activex.h"
#include "ui_activex.h"

ActiveX::ActiveX(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ActiveX)
{
    ui->setupUi(this);
}

ActiveX::~ActiveX()
{
    delete ui;
}

QString ActiveX::Version()
{
    return QString("ActiveX 1.0.0");
}

main.cpp

#include "activex.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    if (!QAxFactory::isServer()) {
        ActiveX w;
        w.show();
        return app.exec();
    }
    return app.exec();
}

//! [1]
QAXFACTORY_BEGIN("{EC08F8FC-2754-47AB-8EFE-56A54057F34E}", /* type library ID */ "{A095BA0C-224F-4933-A458-2DD7F6B85D8F}") /* application ID */
    QAXCLASS(ActiveX)
QAXFACTORY_END()
//! [1]

ActiveX.def

EXPORTS
    DllCanUnloadNow      PRIVATE
    DllGetClassObject    PRIVATE
    DllRegisterServer    PRIVATE
    DllUnregisterServer  PRIVATE
    DumpIDL              PRIVATE

`index.html'

<html lang="en">
<head>
<meta name="GENERATOR" content="Microsoft Visual Studio">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ActiveXCameraGui</title>
</head>
<body>
    <object id="ActiveQtServer" style="width:256px;height:256px;" classid="CLSID:DF16845C-92CD-4AAB-A982-EB9840E74669">
        <param name="Version" value="BTC">
    </object>
    <button onclick="alert(ActiveQtServer.Version())">Version</button>

<script>
    window.onload = function () {
        //var ret = ActiveQtServer.Version;
        //alert(ret);//打印 初始化版本信息
        //alert("Hello World!");//打印 Hello World!
    };
</script>
</body>
</html>

posted @ 2018-01-19 12:03  學海無涯  阅读(1989)  评论(1编辑  收藏  举报