我们在Windows98环境下执行拷贝文件、查找文件等计算机耗时较长的操作时,Windows会显示一个小 的动画,指示正在进行的操作,与死板的静止图像相比增色不少。那么我们自己开发软件时,能否也 显示一个这样的动画提示呢?笔者开发了一个能够在PB下调用的动画DLL,由于采用多线程编程,PB调用的DLL函数能够及时将控制权交还给PB,不影响应用系统的运转。 一、代码与编译选项 在C++Builder中创建一个空白的DLL项目。 创建一个空白的Form,修改它的属性为: BorderStyle=bsDialogBorderIcons 的 子 属 性 均 为FalseFormStyle=fsStayOnTopPosition= poScreenCenterName=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 pascalShowStatus 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; }
|