赛迪网 > IT技术 C/C++ > 文章
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

编程实用篇—C++Builder开发动画DLL

发布时间:2006.04.07 01:28     来源:赛迪网社区    作者:single

我们在Windows98环境下执行拷贝文件、查找文件等计算机耗时较长的操作时,Windows会显示一个小 的动画,指示正在进行的操作,与死板的静止图像相比增色不少。那么我们自己开发软件时,能否也 显示一个这样的动画提示呢?笔者开发了一个能够在PB下调用的动画DLL,由于采用多线程编程,PB调用的DLL函数能够及时将控制权交还给PB,不影响应用系统的运转。

一、代码与编译选项

在C++Builder中创建一个空白的DLL项目。

创建一个空白的Form,修改它的属性为:

BorderStyle=bsDialog
BorderIcons 的 子 属 性 均 为False
FormStyle=fsStayOnTop
Position= poScreenCenter
Name=StatusForm

在Form上添加一个Win32下的Animate控件Animate1,修改它的属性为Align=alTop

在Form上添加一个Standard 下的Button 控件Button_Cancel,再添加System下的Timer控件Timer1, 设置定时Interval时间位250,较快响应用户的取消请求。

因为PB应用系统与动画窗体代码分别属于两个线程,不能采用PB线程直接关闭动画窗体线程的窗口, 否则会引起系统运行不正常,因此采用PB线程设置关闭标志,而动画线程采用Timer控件定时检查标 志,一旦检测到关闭标志,就关闭窗口,清除线程标志,结束动画线程。

下面给出编码及编码原理:

(1)DLL主体代码:

/ *DLL 主 体 代 码
  * 定 义DLL 公 用 变 量
*g_CommonAVI 对Animate 控 件
动 画 类 型 索 引
 *gi_Canceled Button_Cancel 
按 钮 是 否 被 选 择 过
 *gi_AVIType 要 显 示 的 动 画 类 型,
由DLL 输 出 函 数 做 为 参 数 输 入
 *gi_RequestClose 请 求 动 画 线 程 关 闭 标 志
 *gi_WindowActive 动 画 窗 口 所 处 的 状 态
 *lpsWinTitle 动 画 窗 体 的 标 题,
由DLL 输 出 函 数 做 为 参 数 输 入
 */

   TCommonAVI g_CommonAVI[]={
    aviNone, aviFindFolder,
    aviFindFile, aviFindComputer,
   aviCopyFiles, aviCopyFile,
    aviRecycleFile, aviEmptyRecycle,
   aviDeleteFile
   };
   int gi_Canceled=0,gi_AVIType=0;
   int gi_RequestClose=0,gi_WindowActive=0;
   char lpsWinTitle[256];
   HWND hWndParent=NULL;

   / * 定 义DLL 输 出 函 数 */
   extern “C" __declspec(dllexport) int pascal Dll 
 EntryPoint(HINSTANCE hinst, unsigned 
long reason, void *);
   extern “C" __declspec(dllexport) int pascal
ShowStatus Window
(int AVIType,LPSTR WinTitle,long hWnd);
extern “C" __declspec(dllexport) 
int pascal GetStatus(int ai_CloseWin);
extern “C" __declspec(dllexport)
int pascal CloseStatusWindow();

    / * 定 义 线 程TformThread: */
   class TFormThread : public TThread{
   public: // User declarations
   __fastcall TFormThread(bool CreateSuspended);
   void __fastcall Execute(void);
   };
   __fastcall TFormThread::
TFormThread(bool CreateSuspended):
TThread(CreateSuspended){
   }
/ * 动 画 线 程 执 行 代 码,
动 画 窗 体 的 定 时 器 控 件 会 关 闭 它,
清 除 窗 体 存 在 标 志 后 结 束 线 程 的 运 行
*/
   void __fastcall TFormThread::Execute(void){
   gi_WindowActive=1;
    StatusForm=new TStatusForm(NULL);

    StatusForm ->Caption=lpsWinTitle;
    StatusForm ->ShowModal();
    gi_WindowActive=0;
    delete StatusForm;
    gi_RequestClose=0;
   }
   / * 定 义 一 个 线 程 实 例 指 针 */
   TFormThread *FormThread;
    / * 输 出 函 数 代 码 实 现 部 分
    * DllEntryPoint 32 位DLL 入 口
    * ShowStatusWindow 显 示 动 画 窗 口,
它 通 过 创 建 一 个 线 程 来 创 建 窗 口,
避 免 由 于 窗 口 的MODAL 属 性 而 使
控 制 权 不 能 及 时 的 返 还 给 调 用 者
    * GetStatus 取 得“ 取 消” 状 态,
即 用 户 有 没 有 选 择“ 取 消” 按 钮
    * CloseStatusWindow 关 闭 动 画 窗 口,
    */
   __declspec(dllexport) int WINAPI DllEntryPoint 
(HINSTANCE hinst, unsigned long reason, void *)
   {
   return 1;
   }

 __declspec(dllexport) int pascal ShowStatusWindow
(int AVIType,LPSTR WinTitle,long hWnd){
 hWndParent=(HWND)hWnd;
memset(lpsWinTitle,0,sizeof(lpsWinTitle));
 strncpy(lpsWinTitle,WinTitle,sizeof(lpsWin Title) -1);
if (AVIType>0 & & AVIType<=8) gi_AVIType="AVIType;"    
  FormThread="new" TFormThread(true);       
  FormThread ->Priority = tpNormal;
    FormThread ->Resume();
   }

 __declspec(dllexport) int pascal GetStatus
(int ai_CloseWin){
   if (gi_Canceled)
   if (gi_WindowActive){
   gi_RequestClose=1;
    while(gi_RequestClose);
    }

    return gi_Canceled;
   }

   __declspec(dllexport) int pascal CloseStatusWindow(){
    if (gi_WindowActive){
   gi_RequestClose=1;
    while(gi_RequestClose);
   }

    return gi_Canceled;
   }

(2)窗体StatusForm 的代码:

   TStatusForm *StatusForm;
   extern int gi_Canceled;
   extern int gi_AVIType;
   extern TCommonAVI g_CommonAVI[];
   __fastcall TStatusForm::TStatusForm
(HWND ParentWindow)
    : TForm(ParentWindow)
   {
    gi_Canceled=0;
   }
   // 取 消 按 钮 并 不 直 接 关 闭 窗 体,
而 指 示 设 置 取 消 标 志, 供 调 用 者 查 看
   void __fastcall TStatusForm::Button_CancelClick
(TObject *Sender)
   {
   gi_Canceled=1;
   // ModalResult=mrCancel;
   }
     // 激 活 动 画, 在FORMCREATE 事 件 中
   void __fastcall TStatusForm::FormCreate
(TObject *Sender)
   {
    Animate1 ->CommonAVI=g_CommonAVI[gi_AVI 
Type];
    Animate1 ->Active = true;
   }
 
 extern int gi_RequestClose;
 // 定 时 器 事 件 检 测 到 结 束 标 志 关 闭 窗 体
 void __fastcall TStatusForm::Timer1Timer
(TObject *Sender)
   {
    if (gi_RequestClose){
    ModalResult=mrOk;
    }
   }

设置编译选项:打开Project Options 对话框,清除Linker属性页中的Use Dynamic RTL标志,清除Packages属性页中的Build with runtime packages。 这样只要单个DLL就可以运行了,而不必安装 一些动态连接运行时间库。

二、使用动画DLL

1.定义:

   //Declare -> Global External Functions
   FUNCTION Long ShowStatusWindow
(Long AVIType,String WinTitle,long hWnd)
&LIBRARY “STATWIN.DLL" ALIAS FOR
“Show StatusWindow"

   FUNCTION Long GetCancelStatus
(Long CloseWindow) &LIBRARY
“STATWIN.DLL" ALIAS FOR “GetStatus"

   FUNCTION Long CloseStatusWindow() &
 LIBRARY “STATWIN.DLL" ALIAS FOR
“CloseStatusWindow"

2.调用:

   long ll_EndTime
   // 显 示 查 找 文 件 夹 动 画
   ShowStatusWindow(2)
   setpointer(HourGlass!)

   ll_EndTime = Cpu() + 10 * 1000
   DO
    if GetCancelStatus(0)=1 then
    exit
   end if
    // 做 想 做 的 事 情
   LOOP UNTIL cpu() > ll_EndTime

   CloseStatusWindow()

(T113)


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· 中国未来动画业论坛开幕 “超人之父”支招 (06-20) · 黄金时段禁播外国动画? 此提议尚在讨论中 (06-13)
· 全球动画产值达5000亿美元 中国市场潜力大 (06-10) · 广电总局强力扶持国产动画 播出制作商谋变 (06-03)
· 美国动画师即将到方正育乐北京教学基地讲学 (04-01) · 暴雪娱乐正式推出《魔兽世界》最新CG动画 (11-05)
· 活灵活现的PPT动画 (11-03) · 广电两大领域开放 动画和付费电视可引外资 (08-30)
· [FLASH赏析]爆笑变形金刚——《决战时刻》 (08-07) · 国内游戏动画制作人月薪一万是很普遍的事 (04-30)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
[政务][电信][金融][农业][制造业][中小企业]
[CIO][ERP][协同][IT管理][中间件][电子商务]
[政策][地方][专家][评估][辞典][博客][社区]
· 专题:一路畅通构想曲——让出行不再遭遇堵车
· CIO工作亲历:企业ERP选型不能忽视"选人关"
· 综述:信息化建设给中国监狱带来的各种变化
· 金融业风险管理和法规遵从有五点需考虑的因素
· 保险业CIO关注:该如何建立统一高效的CRM体系
· 调查显示:多数CIO对IT规划仍存在困惑和误解
  博客·论坛 ·曾剑秋·项立刚·Java学习·网管