58上位机VC MFC支持文件拖拽的编辑框
58上位机VC MFC支持文件拖拽的编辑框
功能展示
我们如今的软件都支持直接文件拖拽,进行文件打开等进一步扣件功能,如果不带这功能那就非常的不方便不不人性化,很遗憾的是微软VC默认并不支持这一功能得我们自已一步步实现,我们当前例程就来实现这一功能,效果如图 要点提示 1.要使一个程序具有文件拖拽功能,就得通过函数DragAcceptFiles(TRUE);开户程序的这个功能,这是一个条件。 2.另外接收用户拖拽文件的控件也得具有接收文件的属性,控件的这个属性可以通过控件资源属性进行设置也可通过代码段进行设置,我们当前例程通过ModifyStyleEx(0,WS_EX_ACCEPTFILES,0)实现; 3.最后是通过处理OnDropFiles()函数处理WM_DROPFILES消息,实现我们自己想要的功能
实现功能 1.新建基于对话框的应用程序 2.从Cedit派生自己的编辑框类class CDropEdit : public Cedit;添加变量UINT m_nFileFolder;来过虑只支持文件,文件夹还是两个都行;初始化为m_nFileFolder=3;并添加这个变量的设置与获取函数public: inline void SetFileFolderFilter(UINTnFileFolder){m_nFileFolder=nFileFolder;} inline UINTGetFileFolderFilter(){returnm_nFileFolder;}
3.添加PreSubclassWindow()设置些我们想要的编辑框属性; - void CDropEdit::PreSubclassWindow()
- {
- ModifyStyle(0,BS_OWNERDRAW,0);
- ModifyStyleEx(0,WS_EX_ACCEPTFILES,0);//let the edit have drag style
- DragAcceptFiles(TRUE);//let our app have drag function
- CEdit::PreSubclassWindow();
- }
- <div style="text-align: center;"><span style="font-size: large; line-height: 1.5;">4.添加OnDropFiles()函数,实现我们自己想要的功能</span></div>void CDropEdit::OnDropFiles(HDROP hDropInfo)
- {
- // get the number of dropped files or pathes
- WORD wNumFilesDropped = DragQueryFile(hDropInfo, -1, NULL, 0);
- CString firstFile="";
- // get all file names. but we'll only need the first one.
- for (WORD x = 0 ; x < wNumFilesDropped; x++) {
复制代码- // Get the number of bytes required by the file's full pathname
- WORD wPathnameSize = DragQueryFile(hDropInfo, x, NULL, 0);
- // Allocate memory to contain full pathname & zero byte
- char * npszFile = (char *) LocalAlloc(LPTR, wPathnameSize += 1);
- // If not enough memory, skip this one
- if (npszFile == NULL) continue;
- // Copy the pathname into the buffer
- DragQueryFile(hDropInfo, x, npszFile, wPathnameSize);
- // we only care about the first
- if(firstFile=="")firstFile=npszFile;
- // clean up
- LocalFree(npszFile);
- }
- // Free the memory block containing the dropped-file information
- DragFinish(hDropInfo);
- // if this was a shortcut, we need to expand it to the target path
- CString expandedFile = ExpandShortcut(firstFile);
- // if that worked, we should have a real file name
- if (expandedFile!="") firstFile=expandedFile;
- struct _stat buf;
- // get some info about that file
- int result = _stat( firstFile, &buf );
- if( result == 0 ) {
- if (1==m_nFileFolder)
- {// verify that we have a dir (if we want dirs)
- if ((buf.st_mode & _S_IFDIR) == _S_IFDIR)
- {
- SetWindowText(firstFile);
- }
- }
- else if(0==m_nFileFolder)
- {
- if((buf.st_mode & _S_IFREG) == _S_IFREG)
- {// verify that we have a file (if we want files)
- SetWindowText(firstFile);
- }
- }
-
复制代码- else if(2<=m_nFileFolder)
- {
- if( (buf.st_mode & _S_IFREG) == _S_IFREG ||(buf.st_mode & _S_IFDIR) == _S_IFDIR )
- {
- SetWindowText(firstFile);
- }
- }
- }
- CEdit::OnDropFiles(hDropInfo);
- }
复制代码上面函数还包含一个我们自定义的函数ExpandShortcut()到例程下去复制下载 最后我们在程序中用到些结构体如struct _stat buf;,我们得添加这些结构体所在的头文件#include <shlobj.h> #include <sys/types.h>#include<sys/stat.h>
我们来演示下功能实现的整个过程
|