http://blog.csdn.net/u011619422/article/details/45743049
1.在类向导中添加派生类:
添加CDragEdit类,派生于CEdit类。
2.在CDragEdit类中添加消息:
2.1WM_CREATE消息,在消息相应函数中添加:DragAcceptFiles(TRUE);
2.1WM_DROPFILE消息,响应函数如下:
void CDragEdit::OnDropFiles(HDROP hDropInfo) { // TODO: 拖拽文件的响应函数 WORD wNumFileDropped = DragQueryFile(hDropInfo, -1, NULL, 0); CString firstFile = ""; for (WORD x = 0; x < wNumFileDropped; x++){ WORD wPathnameSize = DragQueryFile(hDropInfo, x, NULL, 0); char* npszFile = (char*)LocalAlloc(LPTR, wPathnameSize += 1); if (npszFile == NULL){ continue; } DragQueryFile(hDropInfo, x, npszFile, wPathnameSize); if (firstFile == ""){ firstFile = npszFile; } LocalFree(npszFile); } DragFinish(hDropInfo); //firstFile为拖拽文件的完整路径 //读取文件的内容: CStdioFile Txtfile_Update; CString TxtData_Updata="",Oneline_data=""; BOOL TxtnotNULL=Txtfile_Update.open(firstFile,CFile::modeRead); if(TxtnotNull ==FALSE) { AfxMessageBox("文件打开失败!");} else { while(Txtfile_Update.ReadString(Oneline_data))//.ReadString遇见回车换行符停止;遇见‘\0’结束 TxtData_Updata +=Oneline_data+"\r\n"; } Txtfile_Updata.Close(); SetWindowText(TxtData_Update); UpdateData(FALSE); //CDialogEx::OnDropFiles(hDropInfo); }
3.在对话框界面中,为Edit控件添加成员变量m_DragEdit(变量类型CDragEdit);修改Edit控件的Accept File属性为TRUE