DLL弹出窗口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I_can_do_1098/article/details/53607154

在windows上开发应用时候,交互界面是一个很基本的元素。我们经常是一个exe有一个交互界面,而有的时候需要dll弹出一个窗体以供用户去选择或者输入一些信息的。
这种时候怎么办?

有以下四种可行性方案:

1.用MFC或者ATL来编写dll;

2.CreateWindows函数来创建一个窗体,然后show();

3.新建一个类继承MessageBox类;

4.在资源文件里添加窗体控件,这个方法我感觉已经跑偏了。

我最后采取了第五种方法,用QT写一个窗口dll,我是用vs+qt的混合式开发的,至于环境如何配置,请见http://blog.csdn.net/i_can_do_1098/article/details/53606533

第一,我们开发dll的时候直接建立一个Qt的应用工程就可以了,然后把它属性修改成dll;
有的同志就会疑惑,我可以直接建立一个Qt的dll,我为什么还要脱裤子放屁去搞一个应用呢?这个问题等下回答,这是一个很重要的问题。
第二,界面设计和编码过程一毛一样,只不过我们要对main()函数做一下小的修改:

#ifdef __cplusplus

extern "C"
{
#endif

    __declspec(dllexport) int main(int argc, char *argv[])
    {
        QApplication pApp(argc, argv);      
        QWidget w;
        w.show();
        return pApp.exec();
    }

#ifdef __cplusplus
}

看见没有,我们把main函数导出来了,当然你也可以修改成其他名字比如说fuck之类的,毕竟它是一个dll不是exe不需要主入口函数。
第三步:调用Qt dll:
这个简单直接loadLibrary,跟普通的dll一毛一样,至于如何获取界面信息这个你就要自由发挥了,管道、socket、全局事件、返回值等等。

好,我们返回来说一下第一步我们遇到的那个问题,为啥非要建一个exe然后改造成dll呢?一切都是为了一个变量QApplication pApp,至于为啥我们上官方解释:

The QApplication class manages the GUI application's control flow and main settings.
QApplication contains the main event loop, where all events from the window system and other sources are processed and dispatched. It also handles the application's initialization, finalization, and provides session management. In addition, QApplication handles most of the system-wide and application-wide settings.
For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-GUI Qt applications, use QCoreApplication instead, as it does not depend on the QtGui library.
The QApplication object is accessible through the instance() function that returns a pointer equivalent to the global qApp pointer.
QApplication's main areas of responsibility are:
It initializes the application with the user's desktop settings such as palette(), font() and doubleClickInterval(). It keeps track of these properties in case the user changes the desktop globally, for example through some kind of control panel.
It performs event handling, meaning that it receives events from the underlying window system and dispatches them to the relevant widgets. By using sendEvent() and postEvent() you can send your own events to widgets.
It parses common command line arguments and sets its internal state accordingly. See the constructor documentation below for more details.
It defines the application's look and feel, which is encapsulated in a QStyle object. This can be changed at runtime with setStyle().
It specifies how the application is to allocate colors. See setColorSpec() for details.
It provides localization of strings that are visible to the user via translate().
It provides some magical objects like the desktop() and the clipboard().
It knows about the application's windows. You can ask which widget is at a certain position using widgetAt(), get a list of topLevelWidgets() and closeAllWindows(), etc.
It manages the application's mouse cursor handling, see setOverrideCursor()
On the X window system, it provides functions to flush and sync the communication stream, see flushX() and syncX().
It provides support for sophisticated session management. This makes it possible for applications to terminate gracefully when the user logs out, to cancel a shutdown process if termination isn't possible and even to preserve the entire application's state for a future session. See isSessionRestored(), sessionId() and commitData() and saveState() for details.
Since the QApplication object does so much initialization, it must be created before any other objects related to the user interface are created. QApplication also deals with common command line arguments. Hence, it is usually a good idea to create it before any interpretation or modification of argv is done in the application itself.

其实上边都是扯淡的,最后一句话才是最重要的:
Since the QApplication object does so much initialization, it must be created before any other objects related to the user interface are created.

任何带界面的对象创建之前,都要创建QApplication,因为它要做一些必要的初始化,这回同志们明白为啥要从应用转dll了吧,就是一个方便。

猜你喜欢

转载自blog.csdn.net/I_can_do_1098/article/details/53607154