我们在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()
|