/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Solutions component.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
// Implementation of the QMfcApp classes
#ifdef QT3_SUPPORT
#undef QT3_SUPPORT
#endif
#ifdef UNICODE
#undef UNICODE
#endif
#include "qmfcapp.h"
#include <QEventLoop>
#include <QAbstractEventDispatcher>
#include <QWidget>
#ifdef QTWINMIGRATE_WITHMFC
#include <afxwin.h>
#else
#include <qt_windows.h>
#endif
#ifdef QTWINMIGRATE_WITHMFC
CWinApp *QMfcApp::mfc_app = 0;
char **QMfcApp::mfc_argv = 0;
int QMfcApp::mfc_argc = 0;
#endif
#if QT_VERSION >= 0x050000
#define QT_WA(unicode, ansi) unicode
QMfcAppEventFilter::QMfcAppEventFilter() : QAbstractNativeEventFilter()
{
}
bool QMfcAppEventFilter::nativeEventFilter(const QByteArray &, void *message, long *result)
{
return static_cast<QMfcApp*>(qApp)->winEventFilter((MSG*)message, result);
}
#endif
/*! \class QMfcApp qmfcapp.h
\brief The QMfcApp class provides merging of the MFC and Qt event loops.
QMfcApp is responsible for driving both the Qt and MFC event loop.
It replaces the standard MFC event loop provided by
CWinApp::Run(), and is used instead of the QApplication parent
class.
To replace the MFC event loop reimplement the CWinApp::Run()
function in the CWinApp subclass usually created by the MFC
Application Wizard, and use either the static run() function, or
an instance of QMfcApp created earlier through the static
instance() function or the constructor.
The QMfcApp class also provides a static API pluginInstance() that
drives the Qt event loop when loaded into an MFC or Win32 application.
This is useful for developing Qt based DLLs or plugins, or if the
MFC application's event handling can not be modified.
*/
static int modalLoopCount = 0;
HHOOK hhook;
LRESULT CALLBACK QtFilterProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (qApp) {
// don't process deferred-deletes while in a modal loop
if (modalLoopCount)
qApp->sendPostedEvents();
else
qApp->sendPostedEvents(0, -1);
}
return CallNextHookEx(hhook, nCode, wParam, lParam);
}
/*!
Inform Qt that a modal loop is about to be entered, and that DeferredDelete
events should not be processed. Call this function before calling Win32
or MFC functions that enter a modal event loop (i.e. MessageBox).
This is only required if the Qt UI code hooks into an existing Win32
event loop using QMfcApp::pluginInstance.
\sa exitModalLoop()
*/
void QMfcApp::enterModalLoop()
{
++modalLoopCount;
}
/*!
Inform Qt that a modal loop has been exited, and that DeferredDelete
events should not be processed. Call this function after the blocking
Win32 or MFC function (i.e. MessageBox) returned.
This is only required if the Qt UI code hooks into an existing Win32
event loop using QMfcApp::pluginInstance.
\sa enterModalLoop()
*/
void QMfcApp::exitModalLoop()
{
--modalLoopCount;
Q_ASSERT(modalLoopCount >= 0);
}
/*!
If there is no global QApplication object (i.e. qApp is null) this
function creates a QApplication instance and returns true;
otherwise it does nothing and returns false.
The application installs an event filter that drives the Qt event
loop while the MFC or Win32 application continues to own the event
loop.
Use this static function if the application event loop code can not be
easily modified, or when developing a plugin or DLL that will be loaded
into an existing Win32 or MFC application. If \a plugin is non-null then
the function loads the respective DLL explicitly to avoid unloading from
memory.
\code
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpvReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
QMfcApp::pluginInstance(hInstance);
return TRUE;
}
\endcode
Set \a plugin to 0 when calling this function from within the same executable
module.
If this function is used, call enterModalLoop and exitModalLoop whenever you
call a Win32 or MFC function that opens a local event loop.
\code
void Dialog::someSlot()
{
QMfcApp::enterModalLoop();
MessageBox(...);
QMfcApp::exitModalLoop();
}
\endcode
*/
bool QMfcApp::pluginInstance(Qt::HANDLE plugin)
{
if (qApp)
return FALSE;
QT_WA({
hhook = SetWindowsHookExW(WH_GETMESSAGE, QtFilterProc, 0, GetCurrentThreadId());
}, {
hhook = SetWindowsHookExA(WH_GETMESSAGE, QtFilterProc, 0, GetCurrentThreadId());
});
int argc = 0;
(void)new QApplication(argc, 0);
if (plugin) {
char filename[256];
if (GetModuleFileNameA((HINSTANCE)plugin, filename, 255))
LoadLibraryA(filename);
}
return TRUE;
}
#if QT_VERSION >= 0x050000
Q_GLOBAL_STATIC(QMfcAppEventFilter, qmfcEventFilter);
#endif
#ifdef QTWINMIGRATE_WITHMFC
/*!
Runs the event loop for both Qt and the MFC application object \a
mfcApp, and returns the result. This function calls \c instance()
if no QApplication object exists and deletes the object it
created.
Calling this static function in a reimplementation of
CWinApp::Run() is the simpliest way to use the QMfcApp class:
\code
int MyMfcApp::Run()
{
return QMfcApp::run(this);
}
\endcode
Since a QApplication object must exist before Qt widgets can be
created you cannot use this function if you want to use Qt-based
user interface elements in, for example, the InitInstance()
function of CWinApp. In such cases, create an instance of
QApplication explicitly using instance() or the constructor.
\sa instance()
*/
int QMfcApp::run(CWinApp *mfcApp)
{
bool ownInstance = !qApp;
if (ownInstance)
instance(mfcApp);
int result = qApp->exec();
if (mfcApp) {
int mfcRes = mfcApp->ExitInstance();
if (mfcRes && !result)
result = mfcRes;
}
if (ownInstance)
delete qApp;
return result;
}
/*!
Creates an instance of QApplication, passing the command line of
\a mfcApp to the QA
qtwinmigrate qt5.7.0+vs2013可用
需积分: 30 9 浏览量
2017-06-12
18:38:54
上传
评论 1
收藏 17KB RAR 举报
My__God
- 粉丝: 19
- 资源: 9
最新资源
- 软件仿真多机串行通信.doc
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈