⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 多线程文件搜索源程序
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit3.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PBFolderDialog"
#pragma link "PBFolderDialog"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    //打开文件夹选择对话框
    if(PBFolderDialog1->Execute())
    {
        //在文件夹选择对话框选中的子文件夹放入文本框
        Edit1->Text=PBFolderDialog1->Folder;
    }
}
//---------------------------------------------------------------------------
//启动文件搜索
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    //存放匹配文件名的列表框
    ListBox1->Items->Clear();
    //取掉包含文本部分最后的回车、换行符
    AnsiString str=Memo1->Text.SubString(1,Memo1->Text.Length()-2);
    //包含文本部分为空时,不再继续动作
    if(str.IsEmpty())return;
    //调用线程构造函数创建线程,用搜索文件的诸参数初始化现成的成员变量
    pThread=new TSearchFileThread(false,Edit1->Text,ComboBox1->Text,str,CheckBox1->Checked);
    //防止搜索期间线程重复载入,禁止“搜索”按钮使能
    Button2->Enabled=false;
}
//---------------------------------------------------------------------------
//停止搜索
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    //发出线程终止的指令
    pThread->Terminate();
}
//---------------------------------------------------------------------------
//线程终止事件响应函数
void __fastcall TForm1::ThreadOnEnd(TObject *Sender)
{
    //线程终止事件发生,允许线程再次启动
    Button2->Enabled=true;
}
//---------------------------------------------------------------------------
//文件列表框中鼠标双击事件响应函数
void __fastcall TForm1::ListBox1DblClick(TObject *Sender)
{
    AnsiString CmdLine;
	char lpBuffer[64];

    //取当前选中列表项的索引号
    int mIdx=ListBox1->ItemIndex;
    //当前选中列表项有效时...
    if(mIdx!=-1)
    {
        //取选中的文件名
        AnsiString strFile = ListBox1->Items->Strings[mIdx];
        //取当前文件名的扩展名,转为大写方式
        CmdLine=ExtractFileExt(strFile).UpperCase();
        //当前文件名的扩展名为.htm或.html时...
        if((CmdLine==".HTM")||(CmdLine==".HTML"))
        {
            //取Windows目录的全路径名
            GetWindowsDirectory(lpBuffer,sizeof(lpBuffer));
            //建立命令行
            CmdLine=lpBuffer;
        	CmdLine+="\\EXPLORER.EXE /n,/e,"+strFile;
            //创建进程,即调用网页浏览器
            WinExec(CmdLine.c_str(),SW_SHOW);
        }
        else
        {
            //将选中的文件装入表单Form3的Memo1
            Form3->Memo1->Lines->LoadFromFile(strFile);
            //以无模式方式打开窗口(表单Form3)
            Form3->Show();
        }
    }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -