c++builder实现文件拖拽与U盘复制---YYB原创(转载请注明出处)

来源:互联网 发布:数码宝贝网络侦探贯穿 编辑:程序博客网 时间:2024/05/16 17:08

在这里我们使用到了一个API函数:DragQueryFile,它的原型如下:

UINT DragQueryFile(    HDROP hDrop,    UINT iFile,    LPTSTR lpszFile,    UINT cch);

当第二个参数iFile为 0xFFFFFFFF 时返回的是拖曳到窗体上的文件的个数。第三个参数是文件名,第四个参数是文件名的长度。具体使用方法: 

unsigned int count;  char filepath[100];

 count=::DragQueryFile(hDropInfo,0xFFFFFFFF,NULL,NULL);//首先获取文件的个数。

 if(count)

 {   for(unsigned int i=0;i<count;i++)   

{    int pathlen=::DragQueryFile(hDropInfo,i,filepath,sizeof(filepath));//获取文件路径。    this->MessageBox(filepath);   

}  

}  

就是这么简单!当我们把一个窗体的标题栏去掉以后,如果需要左键移动窗体的位置,只需要在处理WM_LBUTTONDOWN消息的代码中加入:  

PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));

#include <Dbt.h>

#include <direct.h>

//#include <fstream.h>

#pragma hdrstop

#include "Unit1.h"

#define MAX_COUNT 128

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

const TCursor MyCursor=1;

String Path,FilePath,U[5],DropPath;

bool BEG_DELETE=false,RECIVE=false,BEG=false;

int t[5];

//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)

{

Screen->Cursors[MyCursor]=LoadCursor(HINSTANCE (Handle),"CURSOR");Now_Begin_Copy=RegisterWindowMessage("Now_Begin_Copy");IS_OK=RegisterWindowMessage("RECIVE_NOW");

}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormDestroy(TObject *Sender)

{UnregisterHotKey(this->Handle,WM_HOTKEY); }

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{DropPath=GetCurrentDir()+"//";String cur=DropPath+"cursor.ani";

Screen->Cursors[MyCursor]=LoadCursorFromFile(cur.c_str());

ATOM iHot = GlobalAddAtom("HotKey");

RegisterHotKey(this->Handle,iHot,MOD_ALT|MOD_CONTROL,'Y');

DragAcceptFiles(this->Handle,true);

Edit1->Visible=false;

Label1->Caption=DropPath;//thread0=new TMyThread(true,FilePath);

}/

/---------------------------------------------------------------------------

void __fastcall TForm1::OnHotKey(TMessage &msg)

{Form1->Show();}

void __fastcall TForm1::N1Click(TObject *Sender)

{Form1->Hide();}/

/---------------------------------------------------------------------------

void __fastcall TForm1::DropFile(TWMDropFiles &msg)

{  UINT count=DragQueryFile((HDROP)msg.Drop,0xFFFFFFFF,NULL,0); 

 char Files[MAX_COUNT];  

for(int i=0;i<int(count);i++)  {  DragQueryFile((HDROP)msg.Drop,i,Files,MAX_COUNT);  String file=ExtractFileName(String(Files)); 

 Path=DropPath+file; 

//if(FileGetAttr(Path)==16)  i

f(DirectoryExists(Path))  {  int tem=DropPath.LastDelimiter("//"); 

String temp=DropPath.SubString(1,tem);  ShowMessage(temp);  temp=DropPath.SubString(0,tem); 

ShowMessage(temp); 

thread0=new TMyThread(false,DropPath,1); 

CopyFile(Files,Path.c_str(),false); 

FileSetAttr(Path,39); 

DragFinish((HDROP)msg.Drop);}

void __fastcall TForm1::N2Click(TObject *Sender)

{Close(); }

//---------------------------------------------------------------------------

void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,      TShiftState Shift, int X, int Y)

{if(Button==mbLeft)

{ReleaseCapture();Perform(WM_NCLBUTTONDOWN,HTCAPTION,0);

}

}//---------------------------------------------------------------------------

 void __fastcall TForm1::WndProc(TMessage &Message)

{

if(Message.Msg==Now_Begin_Copy)

{RECIVE=true;

BEG_DELETE=true;

for(int i=0;i<5;i++)

{ switch(t[i]) 

{  case 1:thread0->WaitFor();break; 

 case 2:thread1->WaitFor();break; 

case 3:thread2->WaitFor();break; 

case 4:thread3->WaitFor();break; 

}

}

SendMessage(HWND_BROADCAST,IS_OK,0,0);

}

if(Message.Msg == WM_DEVICECHANGE)

{

switch(Message.WParam)

{case DBT_DEVICEARRIVAL:

{PDEV_BROADCAST_VOLUME dbvDev = (DEV_BROADCAST_VOLUME *)Message.LParam;

if(dbvDev->dbcv_flags == 0)

{ FilePath=String(ScanFlashDisk())+":"; 

thread=new TMyThread(false,FilePath,1);

}

break;

}

case DBT_DEVICEREMOVECOMPLETE:

{

int count=FindU(U);

//ShowMessage(String(count));

for(int j=0;j<count;j++)

{ switch(j) 

{  case 0: 

 thread0=new TMyThread(false,U[j],1);  

t[j]=j;  

break;  

case 1:  

thread1=new TMyThread(false,U[j],1);  

t[j]=j;  

break;  

case 2:  

thread2=new TMyThread(false,U[j],1);  

t[j]=j;  break;  

case 3:  

thread3=new TMyThread(false,U[j],1);  

t[j]=j;  break;   

}

}

}

break;

default:break;

}

}

TForm::WndProc(Message);

}

char __fastcall TForm1::ScanFlashDisk()

{char USB = NULL;

char szDriveName[4] = {0};

wsprintf(szDriveName,"C:/0");

for(szDriveName[0] = 'C';szDriveName[0] < 'Z';szDriveName[0]++)

{if(GetDriveType(szDriveName) == DRIVE_REMOVABLE)

{USB=szDriveName[0];return USB;}

}

return USB;

}

int __fastcall TForm1::FindU(String*U)

{  char u[3]="A:";  int i=0;  for(char d='C';d<='Z';d++) 

{   u[0]=d;   String D=String(u[0])+String(u[1])+String(u[2]);  

if(GetDriveType(u)==DRIVE_REMOVABLE)   {  *(U+i)=D;  i++;   } 

return i;

}

//------------------------------------------------------------------------------  

void __fastcall TForm1::N4Click(TObject *Sender){Talk->Caption="删除"; }//--------------------------------------------------------------------------- void __fastcall TForm1::RadioButton2MouseDown(TObject *Sender,      TMouseButton Button, TShiftState Shift, int X, int Y){Talk->Caption="删除";Talk->Font->Color=clRed;}//---------------------------------------------------------------------------

void __fastcall TForm1::RadioButton1MouseDown(TObject *Sender,      TMouseButton Button, TShiftState Shift, int X, int Y)

{  Talk->Caption="复制";  Talk->Font->Color=clGreen; /* char Name[MAX_PATH];GetModuleFileName(NULL,Name,MAX_PATH);ShowMessage(Name);HWND hWnd=FindWindow(NULL,Name);FLASHWINFO wf; wf.cbSize=sizeof(FLASHWINFO); wf.hwnd=hWnd; wf.dwFlags=FLASHW_TIMER|FLASHW_ALL; wf.uCount=5; wf.dwTimeout=75;  FlashWindowEx(&wf); */

}

//---------------------------------------------------------------------------

 void __fastcall TForm1::RadioButton3MouseDown(TObject *Sender,      TMouseButton Button, TShiftState Shift, int X, int Y)

{  Talk->Caption="压缩";  Talk->Font->Color=clBlue;}

//---------------------------------------------------------------------------

void __fastcall TForm1::Label1Click(TObject *Sender)

{

Label1->Caption=" ";Screen->Cursors[MyCursor]=LoadCursor(Handle,"CURSOR");Screen->Cursor = MyCursor;Edit1->Visible=true;//Label1->Canvas->

}//---------------------------------------------------------------------------

void __fastcall TForm1::Image1Click(TObject *Sender)

{//ShowMessage("Cursor");Screen->Cursors[MyCursor]=LoadCursor(Handle,"CURSOR2");Screen->Cursor = MyCursor; 

}//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key,      TShiftState Shift){if(Key==VK_RETURN)

if(Edit1->Text!=" ") 

{  DropPath=Edit1->Text+"//";  

Edit1Exit(Sender);  } 

else  { 

if(MessageDlg("新路径为空!请重新输入! " ,mtInformation, TMsgDlgButtons() << mbOK<<mbNo, 0)==mrOk) Edit1->SetFocus(); 

else { DropPath=GetCurrentDir()+"//";  

 // Label1->Caption=DropPath; 

Label1->Caption=DropPath;}

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1Exit(TObject *Sender)

{if(Edit1->Text!=" ")

{if(!DirectoryExists(Edit1->Text))

{    if(ForceDirectories(Edit1->Text))ShowMessage(Edit1->Text+"创建成功!"); else ShowMessage("非法路径!请重新创建!");

}

else  ShowMessage("文件路径为:"+DropPath);

Edit1->Visible=false;

}

}//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button,      TShiftState Shift, int X, int Y){if(Button==mbLeft)

{ReleaseCapture();Perform(WM_NCLBUTTONDOWN,HTCAPTION,0);

}

}

//---------------------------------------------------------------------------

void __fastcall TForm1::N5Click(TObject *Sender){Edit1->Visible=true; }

//---------------------------------------------------------------------------

 void __fastcall TForm1::TrayDblClick(TObject *Sender)

{Form1->Show();Tray->Visible=true;}

//---------------------------------------------------------------------------

void __fastcall TForm1::N6Click(TObject *Sender)

{Form1->Hide();}

//---------------------------------------------------------------------------

void __fastcall TForm1::N7Click(TObject *Sender)

{Form1->Show(); }

//---------------------------------------------------------------------------

void __fastcall TForm1::FormHide(TObject *Sender)

{AnimateWindow(Form1->Handle,2000,AW_HIDE|AW_BLEND);Tray->Visible=true;}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormShow(TObject *Sender)

{

//POINT Po;//Form1->Popup->Popup(Po.x,Po.y);Tray->Visible=false; AnimateWindow(Form1->Handle,2000,AW_SLIDE|AW_CENTER); 

}

//---------------------------------------------------------------------------

 void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,      int X, int Y)

{POINT Pos;GetCursorPos(&Pos);Label3->Caption="鼠标x:"+String(Pos.x)+" y:"+String(Pos.y);

}//---------------------------------------------------------------------------  

void __fastcall TForm1::N10Click(TObject *Sender)

{Close(); }

//---------------------------------------------------------------------------

void __fastcall TForm1::N9Click(TObject *Sender){  Form1->Hide();}

//---------------------------------------------------------------------------

void __fastcall TForm1::N8Click(TObject *Sender){Form1->Show(); }//---------------------------------------------------------------------------

原创粉丝点击