启动界面

启动界面

1 简介

      现在很多软件在开始的时候都设置了启动界面。这样的设置更加体现出UI的友好性。当然还可以在开启启动界面的时候,做一些软件的初始化工作,比如在开启启动界面的时候收集系统信息,或者收集初始化需要的信息等等。

本文主要介绍一种简单的方法,实现精美的启动界面。由于本人知识所限,错误之处还请各位包容,并提醒鄙人。大家多多交流,共同进步。

2 原理(摘自VS2008随机MSDN)

  Representsa thread of execution within an application.

class CWinThread : public CCmdTarget

Remarks:

The main threadof execution is usually provided by an object derived from CWinApp; CWinApp isderived from CWinThread. Additional CWinThread objects allow multiple threadswithin a given application.

There are twogeneral types of threads that CWinThread supports: worker threads anduser-interface threads. Worker threads have no message pump: for example, athread that performs background calculations in a spreadsheet application.User-interface threads have a message pump and process messages received fromthe system. CWinApp and classes derived from it are examples of user-interfacethreads. Other user-interface threads can also be derived directly fromCWinThread.

Objects of classCWinThread typically exist for the duration of the thread. If you wish tomodify this behavior, set m_bAutoDelete to FALSE.

The CWinThreadclass is necessary to make your code and MFC fully thread-safe. Thread-localdata used by the framework to maintain thread-specific information is managedby CWinThread objects. Because of this dependence on CWinThread to handlethread-local data, any thread that uses MFC must be created by MFC. Forexample, a thread created by the run-time function _beginthread, _beginthreadexcannot use any MFC APIs.

To create athread, call AfxBeginThread. There are two forms, depending on whether you wanta worker or user-interface thread. If you want a user-interface thread, pass toAfxBeginThread a pointer to the CRuntimeClass of your CWinThread-derived class.If you want to create a worker thread, pass to AfxBeginThread a pointer to thecontrolling function and the parameter to the controlling function. For bothworker threads and user-interface threads, you can specify optional parametersthat modify priority, stack size, creation flags, and security attributes.AfxBeginThread will return a pointer to your new CWinThread object.

Instead ofcalling AfxBeginThread, you can construct a CWinThread-derived object and thencall CreateThread. This two-stage construction method is useful if you want toreuse the CWinThread object between successive creation and terminations ofthread executions.

以下为本人译文。如有偏颇,敬请谅解。

CWinThread表示一个在程序内可执行的进程。

注意:

继承于CWinApp的对象经常提供一个可执行的主进程,CWinApp继承于CWinThread类。然而,CWinThread的对象允许在程序中运行多线程。

CWinThread类一般支持两种类型的线程:工作(后台)进程和UI进程。工作(后台)进程没有消息泵。例如:在一个电子制表程序中的后台计算线程。UI线程拥有消息泵,并且处理来自于系统的消息。CWinApp以及继承与它的类就是UI进程的例子。其他UI进程也可以直接继承于CWinThread。类CWinThread的对象典型的存在于进程的生存周期内。如果你想修改这行为,可以将m_bAutoDelete设置为FALSE。

对于你的代码和MFC中的线程安全来说,CWinThread是必须的。CWinThread的对象管理着框架为了获得指定线程信息所使用的线程本地的数据。正因为这样依赖CWinThread处理线程本地的数据,所以任何使用MFC的线程必须使用MFC创建。例如:一个使用运行时创建函数(_beginthread)创建的线程是不能使用任何MFCAPI的。

为了创建一个线程,可以调用函数AfxBeginThread。这样创建的线程有两种形式,这取决于你想要UI进程还是后台进程。如果你想要创建UI进程,那么你就传递一个指向继承于CWinThread的CRUNTIME对象。如果你想创建一个后台进程,那么你就传递一个控制函数和相关的参数。对于UI进程和后台进程,你都可以通过参数设置进程优先级,栈大小,创建标志,安全属性等。AfxBeginThread函数将返回一个指向你新建立的CWinthread对象的指针。

你可以构造一个继承与CWinThread的类的对象,并且调用CreateThread函数来替代AfxBeginThread。如果你想在连续的创建和结束进程的过程中重复使用CWinThread对象,那么这种分两阶段的构造方式是有效的。

 

3实验方法

建立一个对话框,或者自己定义一个继承于CWnd类的对象即可。这个是你要显示的启动界面。你可以在这个对话框里面显示你想要显示的信息。当然在我的实验中,我定义了对话框类,我给这个类的对象一个Timer,然后一共存在4s,当调用4次OnTimer后就自动调用DestroyWindow。新建一个类,继承于CWinThread。在该类的内部申明一个先前定义的启动界面的对话框。然后重载函数InitInstance函数。在该函数里实现对话框的显示。附件为实验代码。


点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值