C++實例 — 從外部拖拉一群檔案至視窗 (C++ an actual example Dragdrop a group of files onto the form)

本文展示C++從外部拖拉一群檔案至視窗的實例。

在PDM系統之業務洽辦單之各單有其專屬的文件資料,於是設計成可由使用者以拖拉檔案的方式建立相關文件檔案。

未命名

.cpp程式碼

//從外部拖拉一個以上檔案至DBGrid7
void __fastcall TForm_Req::Newdbg7WP(TMessage &Msg)
{
  if (Msg.Msg == WM_DROPFILES){

    // 建立洽辦單號專屬資料夾
    if(!chk_mak_dir(req_doc_dir3)){
        ShowMessage("! [ "+req_doc_dir3+" ]資料夾不存在,作業無法執行,請洽系統管理者建立該資料夾 !");
        return;
    }

    // 確認執行
    char q;
    String msg="確定將拖拉的檔案匯入成為洽辦單號 "+req_id+" 的所屬文件?"; //\n\n(若文件中有同名將被覆蓋)";
    q=MessageDlg(msg , mtConfirmation , TMsgDlgButtons() << mbYes << mbNo ,0);
    if(q!=mrYes){
        return;
    }

    // find the number of files dropped
    int num_files = DragQueryFile((HDROP)Msg.WParam, 0xFFFFFFFF, (LPSTR)NULL, NULL);
    String files;
    int n_files=0;

    Screen->Cursor=crSQLWait;

    for(int i=0; i<num_files ; i++){
        Application->ProcessMessages();
        int NameLength = DragQueryFile((HDROP)Msg.WParam, i, NULL, NULL) + 1;
        char *FileName = new char[NameLength];
        DragQueryFile((HDROP)Msg.WParam, i, FileName, NameLength);

        if(DirectoryExists((String)FileName)){
            ShowMessage("! [ "+(String)FileName+" ]是一個資料夾 !");
        }
        else{                               //如果是檔案
            String ori_FileName=FileName;
            String cpy_fl;
            String ext=ExtractFileExt(FileName).UpperCase();
            if(ext==".PNG"){  //尚不接受png檔
                ShowMessage("! 不能匯入 "+(String)FileName+" ! \n\n尚無法接受pgn格式的圖片檔案,請轉成jpg或bmp");
                continue;
            }                 //若是bmp jpg jpeg檔則會進行比例縮圖成為IMG_W寬度
            else if(ext==".JPG" || ext==".BMP" || ext==".JPEG"){ // || ext==".PNG"){
                trans_bmp_to_jpg(FileName,IMG_W,temp_jpg_fil,this);
                cpy_fl=req_doc_dir3+"\\"+mainflnm_nopath(FileName)+".jpg";
                FileName=temp_jpg_fil.c_str();
            }
            else{             //其餘檔案格式
                cpy_fl=req_doc_dir3+"\\"+ExtractFileName(FileName);
            }

            FileSetAttr(cpy_fl,faArchive); //取消原檔之唯讀屬性

            // add the file,複製檔案至洽辦單之專屬資料夾
            if(CopyFile(FileName,cpy_fl.c_str(),false)==true){ //false:覆蓋
                ++n_files;
                // 處理資料庫 ----
                //先刪同名檔紀錄
                String s;
                qu_req_doc->SQL->Clear();
                qu_req_doc->SQL->Add(" DELETE req_doc WHERE req_id="+QuotedStr(req_id)+" AND filename="+QuotedStr(ExtractFileName(cpy_fl)));
                qu_req_doc->SQL->Add(s);
                qu_req_doc->ExecSQL();

                //填寫此檔紀錄
                qu_req_doc->SQL->Clear();
                s=" INSERT req_doc(req_id,filename,ent_no,ent_nm,ent_dt) VALUES ( "+
                              QuotedStr(req_id) +","+
                              QuotedStr(ExtractFileName(cpy_fl)) +","+
                              QuotedStr(ACCOUNT) +","+
                              QuotedStr(ACCOUNTC) +", GETDATE() )";
                qu_req_doc->SQL->Add(s);
                qu_req_doc->ExecSQL();
            }
            else{
                ShowMessage("! "+ori_FileName+"複製時出問題,無法匯入 !");
            }
        } 
        delete [] FileName;
    } // end for
    Screen->Cursor=crDefault;
    req_doc_open->Execute();  //重展資料
    ShowMessage("已加入 "+(String)n_files+" 個檔案:\n"+files);
  }
  Olddbg7WP(Msg);
}

 

Tagged: ,

1 thoughts on “C++實例 — 從外部拖拉一群檔案至視窗 (C++ an actual example Dragdrop a group of files onto the form)

  1. […] 程式码所在 […]

發表留言