c/c++语言开发共享C++程序操作文件对话框的方法

在c++程序中有时需要通过系统的文件对话框去操作文件或者文件夹,我们有必要熟练掌握操作文件对话框的细节,去更好的为我们软件服务。本文我们就来讲述一下c++在操作文件夹对话框的相关细节,给大家借鉴和参考

在c++程序中有时需要通过系统的文件对话框去操作文件或者文件夹,我们有必要熟练掌握操作文件对话框的细节,去更好的为我们软件服务。c/c++开发分享C++程序操作文件对话框的方法我们就来讲述一下c++在操作文件夹对话框的相关细节,给大家借鉴和参考。

1、调用getopenfilename接口启动打开文件对话框

C++程序操作文件对话框的方法

当我们需要使用文件对话框打开一个文件时,我们可以调用系统api函数getopenfilename来启动系统的打开文件对话框。调用该api函数时需要传入openfilename结构体对象,通过该结构体给打开文件对话框设置一些属性参数。

1.1、openfilename结构体说明

结构体openfilename的定义有两个版本,一个unicode版本的openfilenamew,一个ansi版本的openfilenamea,这个地方我们以unicode版本的openfilenamew来说明,该结构体的定义如下:

typedef struct tagofnw {     dwordlstructsize;     hwnd hwndowner;     hinstancehinstance;     lpcwstr  lpstrfilter;     lpwstr   lpstrcustomfilter;     dwordnmaxcustfilter;     dwordnfilterindex;     lpwstr   lpstrfile;     dwordnmaxfile;     lpwstr   lpstrfiletitle;     dwordnmaxfiletitle;     lpcwstr  lpstrinitialdir;     lpcwstr  lpstrtitle;     dwordflags;     word nfileoffset;     word nfileextension;     lpcwstr  lpstrdefext;     lparam   lcustdata;     lpofnhookproc lpfnhook;     lpcwstr  lptemplatename;  #ifdef _mac     lpeditmenu   lpeditinfo;     lpcstr   lpstrprompt;  #endif  #if (_win32_winnt >= 0x0500)     void *pvreserved;     dworddwreserved;     dwordflagsex;  #endif // (_win32_winnt >= 0x0500)  } openfilenamew, *lpopenfilenamew;

这个地方我们讲一下几个我们使用到的字段:

  • lstructsize字段[in]:用来存放当前结构体的长度。
  • hwndowner字段[in]:用来指定文件对话框的父窗口。
  • lpstrfilter字段[in]:用来设置选择的文件类型,如果要选择所有文件,则设置“all files(*.*)*.*”;如果选择的是常用的图片文件,则可以设置“图片文件(*.bmp;*.jpg;*.jpeg;*.gif;*.png)*.bmp;*.jpg;*.jpeg;*.gif;*.png”。
  • lpstrfile字段[out]:该字段用来传出用户选择的文件信息,存放选择的文件信息的buffer是需要外部申请内存的,然后将buffer地址赋值给该字段,然后在getopenfilename调用完成后通过该字段指定的buffer输出用户选择的文件信息。
  • nmaxfile字段[in]:用来指定设置给lpstrfile字段对应的buffer长度的。
  • lpstrinitialdir字段[in]:用来指定打开文件对话框时的初始显示路径。
  • flags字段[in]:用来设定文件对话框支持的属性,比如支持多选的ofn_allowmultiselect、选择的文件必须存在ofn_filemustexist等。

1.2、设置支持文件多选,控制选择文件的个数上限

要支持文件多选,需要给openfilename结构体的flags字段添加ofn_allowmultiselect选项。如果要控制选择文件的个数上限,比如我们在im聊天框中限制用户插入图片的个数(比如我们此处限制为5个图片文件):

C++程序操作文件对话框的方法

我可以在给openfilename结构体参数设置lpstrfilter字段指定接收用户选择信息时指定buffer的长度最多存放5张图片的路径。

如果选择多个文件时的文件信息超过buffer的长度,则getopenfilename会返回失败,此时可以调用commdlgextendederror获取错误码,如果错误码是fnerr_buffertoosmall,则表示buffer太小存不下选择的多个图片文件信息,此时可以弹出提示框,相关代码如下:

// 设置最多选择5个文件的buffer长度  int nbuflen = 5*(max_path+1) + 1;  tchar* pbuf = new tchar[nbuflen];  memset( pbuf, 0, nbuflen*sizeof(tchar) );     openfilename ofn;  memset( &ofn, 0 ,sizeof(ofn) );  ofn.lstructsize = sizeof(ofn);  ofn.hwndowner = m_hparentwnd;  ofn.lpstrfile = pbuf;  ofn.nmaxfile = nbuflen;  ofn.lpstrfilter = _t("all files(*.*)*.*");  ofn.lpstrfiletitle = null;  ofn.nmaxfiletitle = 0;  ofn.lpstrinitialdir = null;  ofn.flags = ofn_explorer|ofn_allowmultiselect|ofn_filemustexist|ofn_hidereadonly;  // 打开文件打开对话框  bool bret = ::getopenfilename( &ofn );  if ( !bret )  {      // 上面的ofn.lpstrfile buffer的长度设置存放3个图片路径,选择较多文件时      // 会超出给定的buffer长度,所以此处要判断一下这样的情况,并给出提示5      dword dwerr = commdlgextendederror();      if ( dwerr == fnerr_buffertoosmall )       {          // fnerr_buffertoosmall - the buffer pointed to by the lpstrfile member           // of the openfilename structure is too small          ::messagebox( null, _t("最多只允许选择5个文件,请重新选择"), _t("提示"), mb_ok );      }         delete []pbuf;      return;  }     // 再检查一下用户选择的文件个数,检测是否达到上限  int npictotal = 0;  uiposition pos = (uiposition)ofn.lpstrfile;  while ( pos != null )  {      // 获取用户选择的每个文件的路径,此处可以将文件的路径保存下来      strpicpath = getnextpathname( pos, ofn );      ++npictotal;  }     if ( npictotal > 5 )  {      ::messagebox( null, _t("最多只允许选择5个文件,请重新选择"), _t("提示"), mb_ok );      delete []pbuf;      return;  }

当然,我们仅仅通过fnerr_buffertoosmall错误判断文件个数超限了,是不够的。可能用户选择了6个文件时,总的buffer不会超限,所以还要在解析出来用户选择的每个文件后计算一下选择的文件个数。

1.3、从openfilename结构体的lpstrfile字段解析出用户选择的文件的完整路径

上面我们讲了,用户在选择完多个文件后,点击确定,api函数getopenfilename返回,传给该api函数的openfilename结构体对象参数的lpstrfile字段中存放这用户选择的文件信息,比如我选择了3个文件(docker.png、linux.png、running.png),lpstrfile中存放的数据格式会是:

c:usersadministratordesktopcsdn文章图集docker.pnglinux.pngrunning.png

因为这段字串中包含,所以我们不能把lpstrfile当成字符串来处理。我们专门封装了接口getnextpathname,该接口负责将每个文件的完整路径解析:

// 主要用于文件打开对话框选中多个文件时,解析出多个文件名(绝对路径),从mfc库中的cfiledialog类的getnextpathname函数中剥离出来的  cuistring getnextpathname( uiposition& pos, openfilename& ofn )   {      bool bexplorer = ofn.flags & ofn_explorer;          tchar chdelimiter;      if ( bexplorer )      {          chdelimiter = '';      }      else      {          // for old-style dialog boxes, the strings are space separated and the function uses           // short file names for file names with spaces.           chdelimiter = ' ';       }         lptstr lpsz = (lptstr)pos;      if ( lpsz == ofn.lpstrfile ) // first time      {          if ( (ofn.flags & ofn_allowmultiselect) == 0 )          {              pos = null;              return ofn.lpstrfile;          }             // find char pos after first delimiter          while( *lpsz != chdelimiter && *lpsz != '' )          lpsz = _tcsinc( lpsz );          lpsz = _tcsinc( lpsz );             // if single selection then return only selection          if ( *lpsz == 0 )          {              pos = null;              return ofn.lpstrfile;          }      }         cstring strpath = ofn.lpstrfile;      if ( !bexplorer )      {          lptstr lpszpath = ofn.lpstrfile;          while( *lpszpath != chdelimiter )              lpszpath = _tcsinc(lpszpath);          strpath = strpath.left( lpszpath - ofn.lpstrfile );      }         lptstr lpszfilename = lpsz;      cstring strfilename = lpsz;         // find char pos at next delimiter      while( *lpsz != chdelimiter && *lpsz != '' )          lpsz = _tcsinc(lpsz);         if ( !bexplorer && *lpsz == '' )          pos = null;      else      {          if ( !bexplorer )              strfilename = strfilename.left( lpsz - lpszfilename );                 lpsz = _tcsinc( lpsz );          if ( *lpsz == '' ) // if double terminated then done              pos = null;          else              pos = (uiposition)lpsz;       }         // only add '\' if it is needed      if ( !strpath.isempty() )      {          // check for last back-slash or forward slash (handles dbcs)          lpctstr lpsz = _tcsrchr( strpath, '\' );          if ( lpsz == null )              lpsz = _tcsrchr( strpath, '/' );          // if it is also the last character, then we don't need an extra          if ( lpsz != null &&          (lpsz - (lpctstr)strpath) == strpath.getlength()-1 )          {              assert( *lpsz == '\' || *lpsz == '/' );              return strpath + strfilename;          }      }         return strpath + '\' + strfilename;  }

我们在外部循环调用getnextpathname函数即可:

uiposition pos = (uiposition)ofn.lpstrfile;  while ( pos != null )  {      // 获取用户选择的每个文件的路径,此处可以将文件的路径保存下来      strpicpath = getnextpathname( pos, ofn );      ++npictotal;  }

2、调用getsavefilename接口启动保存文件对话框

C++程序操作文件对话框的方法

当我们需要使用保存文件对话框保存一个文件时,我们可以调用系统api函数getsavefilename来启动系统的保存文件对话框。调用该函数时也是传入openfilename结构体对象的,通过该结构体给打开文件对话框设置一些属性参数。

对于getsavefilename接口,openfilename结构体中的lpstrfile字段的含义是不同的,此处用来设置保存文件的默认免费精选名字大全的。调用函数的示例代码如下:

tchar szfile[3*max_path] = { 0 };  _tcscpy( szfile, strfilename );  openfilename ofn;  memset( &ofn, 0 ,sizeof(ofn) );  ofn.lstructsize = sizeof(openfilename);   ofn.hwndowner = null;  ofn.lpstrfilter = _t("all files(*.*)|0*.*||");   ofn.lpstrfile = szfile;   ofn.nmaxfile = sizeof(szfile)/sizeof(tchar);   ofn.lpstrdefext = (lpctstr)strext;  ofn.lpstrinitialdir = null;   ofn.flags = ofn_explorer|ofn_pathmustexist|ofn_hidereadonly|ofn_nochangedir|ofn_overwriteprompt|ofn_enablehook;   ofn.lpfnhook = filesaveas_ofnhookproc; // 设置回调函数,添加额外的处理机制if ( ::getsavefilename( &ofn ) )  {      // 用户最终输入的文件名的完整路径      cstring strpathname = ofn.lpstrfile;  }

在上述代码中,我们添加了ofn_overwriteprompt属性,用来指定覆盖同名文件时给出提示;我们还添加了ofn_enablehook属性,用来指定设置回调函数,在函数中添加额外的处理机制。在本例中,我们的hook回调函数如下:

// 文件对话框回调函数  uint_ptr callback filesaveas_ofnhookproc(  hwnd hdlg,  // handle to child dialog box  uint uimsg, // message identifier  wparam wparam,  // message parameter  lparam lparam   // message parameter  )  {      if( uimsg == wm_notify)       {          lpofnotify pofn = (lpofnotify)lparam;          if( pofn->hdr.code == cdn_fileok )          {              hwnd hfiledlg = ::getparent( hdlg );                 // 获取用户选择保存路径              tchar achcurpath[max_path*3] = { 0 };              int nret = ::sendmessage( hfiledlg, cdm_getfolderpath,                        sizeof(achcurpath)/sizeof(tchar),              (lparam)achcurpath );              if ( nret < 0 )              {              return 0;              }                 // 判断当前程序对选择的目录是否有写权限              bool bcanwrite = canaccessfile( achcurpath, generic_write );              if ( !bcanwrite )              {                  ::messagebox( hfiledlg, string_have_no_right_save_file, string_tip,         mb_ok|mb_iconwarning );                     ::setwindowlong( hdlg, dwl_msgresult, 1 );                  return 1;              }          }      }         return 0;  }

在hook回调函数中我们拦截了点击确定的消息,判断一下用户当前选择的路径有没有权限保存文件。因为在win7及以上系统中,如果程序没有管理员权限,是没有权利向c:windows、c:program files等系统关键路径中写入文件的。

判断程序是否对某个路径有写权限,我们封装了专门的判断函数,如下所示:

// 将要检测的权限generic_xxxxxx传递给dwgenericaccessmask,可检测对  // 文件或者文件夹的权限  bool canaccessfile( cstring strpath, dword dwgenericaccessmask )    {        cstring strlog;      strlog.format( _t("[canaccessfile] strpath: %s, dwgenericaccessmask: %d"), strpath,     dwgenericaccessmask );      writeupdatelog( strlog );         dword dwsize = 0;        psecurity_descriptor psd = null;        security_information si = owner_security_information |       group_security_information | dacl_security_information;         writelog( _t("[canaccessfile] getfilesecurity - null") );         // 获取文件权限信息结构体大小        bool bret = getfilesecurity( strpath, si, psd, 0, &dwsize );        if ( bret || getlasterror() != error_insufficient_buffer )        {            strlog.format( _t("[canaccessfile] getfilesecurity - null failed, getlasterror: %d"), getlasterror() );          writelog( strlog );          return false;        }              char* pbuf = new char[dwsize];      zeromemory( pbuf, dwsize );      psd = (psecurity_descriptor)pbuf;           strlog.format( _t("[canaccessfile] getfilesecurity - dwsize: %d"), dwsize );      writelog( strlog );         // 获取文件权限信息结构体大小        bret = getfilesecurity( strpath, si, psd, dwsize, &dwsize );        if ( !bret )        {            strlog.format( _t("[canaccessfile] getfilesecurity - dwsize failed, getlasterror: %d"), getlasterror() );          writelog( strlog );          delete []pbuf;          return false;        }           writelog( _t("[canaccessfile] openprocesstoken") );         handle htoken = null;        if ( !openprocesstoken( getcurrentprocess(), token_all_access, &htoken ) )        {            strlog.format( _t("[canaccessfile] openprocesstoken failed, getlasterror: %d"),     getlasterror() );          writelog( strlog );          delete []pbuf;          return false;        }           writelog( _t("[canaccessfile] duplicatetoken") );         // 模拟令牌        handle himpersonatedtoken = null;        if( !duplicatetoken( htoken, securityimpersonation, &himpersonatedtoken ) )        {            strlog.format( _t("[canaccessfile] duplicatetoken failed, getlasterror: %d"),     getlasterror() );          writelog( strlog );             delete []pbuf;          closehandle( htoken );          return false;        }           writelog( _t("[canaccessfile] mapgenericmask") );          // 在检测是否有某个权限时,将generic_write等值传给本函数的第二个参数dwgenericaccessmask      // generic_write等参数在调用createfile创建文件时会使用到,下面调用mapgenericmask将      // generic_write等转换成file_generic_write等      // 将generic_xxxxxx转换成file_generic_xxxxxx      generic_mapping genmap;      genmap.genericread = file_generic_read;        genmap.genericwrite = file_generic_write;        genmap.genericexecute = file_generic_execute;        genmap.genericall = file_all_access;        mapgenericmask( &dwgenericaccessmask, &genmap );           writelog( _t("[canaccessfile] accesscheck") );         // 调用accesscheck来检测是否有指定的权限      privilege_set privileges = { 0 };        dword dwgrantedaccess = 0;        dword privlength = sizeof(privileges);        bool bgrantedaccess = false;        if( accesscheck( psd, himpersonatedtoken, dwgenericaccessmask,   &genmap, &privileges, &privlength, &dwgrantedaccess, &bgrantedaccess ) )        {          strlog.format( _t("[canaccessfile] accesscheck succeed, dwgenericaccessmask: %d,     dwgrantedaccess: %d, bgrantedaccess: %d, "),   dwgenericaccessmask, dwgrantedaccess, bgrantedaccess );          writelog( strlog );             if ( bgrantedaccess )          {              if ( dwgenericaccessmask == dwgrantedaccess )              {                  bgrantedaccess = true;              }              else              {                  bgrantedaccess = false;              }          }          else          {              bgrantedaccess = false;          }      }      else      {          strlog.format( _t("[canaccessfile] accesscheck failed, getlasterror: %d"), getlasterror() );          writelog( strlog );             bgrantedaccess = false;      }         strlog.format( _t("[canaccessfile] bgrantedaccess: %d"), bgrantedaccess );      writelog( strlog );         delete []pbuf;      closehandle( himpersonatedtoken );      closehandle( htoken );      return bgrantedaccess;    } 

另外,我们可以在hook回调函数中调整文件对话框相对于父窗口的弹出位置,拦截wm_initdialog消息,代码如下:

// 通过该回调函数移动文件保存对话框的位置,解决win32 文件对话框默认显示在屏幕左上方的问题  uint_ptr callback ofnhookproc(  hwnd hdlg,  // handle to child dialog box  uint uimsg, // message identifier  wparam wparam,  // message parameter  lparam lparam   // message parameter  )  {      if ( uimsg == wm_initdialog )      {          hwnd hfiledlg = ::getparent( hdlg );          hwnd hparent = ::getparent( hfiledlg );             rect rcparent;          ::getwindowrect( hparent, &rcparent );             rect rcfiledlg;          ::getwindowrect( hfiledlg, &rcfiledlg );             int nfiledlgwdith = rcfiledlg.right - rcfiledlg.left;          int nfiledlgheight = rcfiledlg.bottom - rcfiledlg.top;             rect rcdst;          rcdst.left = rcparent.left + ( (rcparent.right - rcparent.left) - nfiledlgwdith )/2;          rcdst.right = rcdst.left + nfiledlgwdith;          rcdst.bottom = rcparent.bottom + 1;          rcdst.top = rcdst.bottom - nfiledlgheight;             // 下面保证文件对话框处于屏幕可视区域内          if ( rcdst.left < 0 )          {              rcdst.left = 0;              rcdst.right = rcdst.left + nfiledlgwdith;          }             if ( rcdst.top < 0 )          {              rcdst.top = 0;              rcdst.bottom = rcdst.top + nfiledlgheight;          }             int nxscreen = ::getsystemmetrics( sm_cxscreen );          int nyscreen = ::getsystemmetrics( sm_cyscreen );             if ( rcdst.right > nxscreen )          {              rcdst.right = nxscreen;              rcdst.left = rcdst.right - nfiledlgwdith;          }             if ( rcdst.bottom > nyscreen )          {              rcdst.bottom = nyscreen;              rcdst.top = rcdst.bottom - nfiledlgheight;          }             // 重新设置文件对话框位置          ::setwindowpos( hfiledlg, null, rcdst.left, rcdst.top, 0, 0, swp_nosize | swp_nozorder );             return 1;      }         return 0;  } 

3、调用shbrowseforfolder接口打开浏览文件夹对话框

C++程序操作文件对话框的方法

当我们在im聊天框中发起文件传输时,我们要发送的是一个文件夹,可能就需要使用到系统的浏览文件夹对话框来选择要发送的文件夹。当我们在接收到多个文件时,可以选择全部另存为,此时也需要使用系统的浏览文件夹对话框来选择要另存为的路径。

我们调用shbrowseforfolder接口即可打开浏览文件夹对话框,相关代码如下所示:

// 打开目录选择对话框  ::coinitialize( null );     lpmalloc lpmalloc = null;  tchar  pchseldir[max_path] = {0};  pchseldir[0] = _t('');  if( e_fail == ::shgetmalloc(&lpmalloc) )  {  ::couninitialize();  return;  }     browseinfo browseinfo;  browseinfo.hwndowner  = m_hparentwnd;  browseinfo.pidlroot   = null;   browseinfo.pszdisplayname = null;  browseinfo.lpsztitle  = string_sel_folder_to_send;     browseinfo.ulflags= bif_returnonlyfsdirs;    browseinfo.lpfn   = null;     browseinfo.lparam = 0;     // 打开浏览文件夹对话框  lpitemidlist lpitemidlist = null;  if ( ( lpitemidlist = ::shbrowseforfolder(&browseinfo) ) != null )  {      // 获取用户选择的文件夹      ::shgetpathfromidlist( lpitemidlist, pchseldir );         lpmalloc->free( lpitemidlist );      lpmalloc->release();    }  else  {      lpmalloc->free( lpitemidlist );      lpmalloc->release();       ::couninitialize();      return;  }     ::couninitialize();

4、最后

到此这篇关于c++如何操作文件对话框的文章就介绍到这了,更多相关c++文件对话框内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++程序操作文件对话框的方法,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1238148.html

(0)
上一篇 2022年9月11日
下一篇 2022年9月11日

精彩推荐