资料收集站

SDL

Tuesday
Jan 06th
Text size
  • Increase font size
  • Default font size
  • Decrease font size

    作者:刘 学 平

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

一、 代 码 与 编 译 选 项

  1. 在C + +Builder 中 创 建 一 个 空 白 的DLL 项 目。
  2. 创 建 一 个 空 白 的Form, 修 改 它 的 属 性 为:

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

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

           Align=alTop

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

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

----5. 下 面 给 出 编 码 及 编 码 原 理:

----(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;
       }
    }

v6. 设 置 编 译 选 项: 打 开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()

转载自中国程序员网站

Comments (0)Add Comment

Write comment

busy
 

Google 搜索

在线用户

We have 40 guests online