QQ登录

只需一步,快速开始

58上位机VC MFC支持文件拖拽的编辑框

[ 复制链接 ]

58上位机VC MFC支持文件拖拽的编辑框

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()设置些我们想要的编辑框属性;
  1. void CDropEdit::PreSubclassWindow()
  2. {
  3.         ModifyStyle(0,BS_OWNERDRAW,0);
  4.         ModifyStyleEx(0,WS_EX_ACCEPTFILES,0);//let the edit have drag style
  5.         DragAcceptFiles(TRUE);//let our app have drag function
  6.         CEdit::PreSubclassWindow();
  7. }
  8. <div style="text-align: center;"><span style="font-size: large; line-height: 1.5;">4.添加OnDropFiles()函数,实现我们自己想要的功能</span></div>void CDropEdit::OnDropFiles(HDROP hDropInfo)
  9. {
  10.         // get the number of dropped files or pathes
  11.         WORD wNumFilesDropped = DragQueryFile(hDropInfo, -1, NULL, 0);
  12.         CString firstFile="";
  13.         // get all file names. but we'll only need the first one.
  14.         for (WORD x = 0 ; x < wNumFilesDropped; x++) {
复制代码
  1.   // Get the number of bytes required by the file's full pathname
  2.                 WORD wPathnameSize = DragQueryFile(hDropInfo, x, NULL, 0);
  3.                 // Allocate memory to contain full pathname & zero byte
  4.                 char * npszFile = (char *) LocalAlloc(LPTR, wPathnameSize += 1);
  5.                 // If not enough memory, skip this one
  6.                 if (npszFile == NULL) continue;
  7.                 // Copy the pathname into the buffer
  8.                 DragQueryFile(hDropInfo, x, npszFile, wPathnameSize);
  9.                 // we only care about the first
  10.                 if(firstFile=="")firstFile=npszFile;
  11.                 // clean up
  12.                 LocalFree(npszFile);
  13.         }
  14.         // Free the memory block containing the dropped-file information
  15.         DragFinish(hDropInfo);
  16.         // if this was a shortcut, we need to expand it to the target path
  17.         CString expandedFile = ExpandShortcut(firstFile);
  18.         // if that worked, we should have a real file name
  19.         if (expandedFile!="")  firstFile=expandedFile;
  20.         struct _stat buf;
  21.         // get some info about that file
  22.         int result = _stat( firstFile, &buf );
  23.         if( result == 0 ) {
  24.                 if (1==m_nFileFolder)
  25.                 {// verify that we have a dir (if we want dirs)
  26.                         if ((buf.st_mode & _S_IFDIR) == _S_IFDIR)
  27.                         {
  28.                                 SetWindowText(firstFile);
  29.                         }
  30.                 }
  31.                 else if(0==m_nFileFolder)
  32.                 {
  33.                         if((buf.st_mode & _S_IFREG) == _S_IFREG)
  34.                         {// verify that we have a file (if we want files)
  35.                                 SetWindowText(firstFile);
  36.                         }
  37.                 }
  38.                
复制代码
  1. else if(2<=m_nFileFolder)
  2. {
  3. if( (buf.st_mode & _S_IFREG) == _S_IFREG ||(buf.st_mode & _S_IFDIR) == _S_IFDIR )
  4. {
  5. SetWindowText(firstFile);
  6. }
  7. }

  8. }
  9. CEdit::OnDropFiles(hDropInfo);
  10. }
复制代码
上面函数还包含一个我们自定义的函数ExpandShortcut()到例程下去复制下载
最后我们在程序中用到些结构体如struct _stat buf;,我们得添加这些结构体所在的头文件#include <shlobj.h>  #include <sys/types.h>#include<sys/stat.h>


我们来演示下功能实现的整个过程

请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:58.上位机VC MFC支持文件拖拽的编辑框.rar 
文件大小:158.15 KB  售价:10金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我

  

您的支持是我们创作的动力!  

  

您可花点闲钱积分自助任意充值

  

成为VIP会员 全站资源任意下载永久更新!


回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索