常用对话框的API函数

以下是引用片段:
Private   Declare   Function   GetOpenFileName   Lib   "comdlg32.dll"   Alias   "GetOpenFileNameA"   (pOpenfilename   As   OPENFILENAME)   As   Long  
  Private   Declare   Function   GetSaveFileName   Lib   "comdlg32.dll"   Alias   "GetSaveFileNameA"   (pOpenfilename   As   OPENFILENAME)   As   Long  
   
  Private   Type   OPENFILENAME  
            lStructSize   As   Long  
            hwndOwner   As   Long  
            hInstance   As   Long  
            lpstrFilter   As   String  
            lpstrCustomFilter   As   String  
            nMaxCustFilter   As   Long  
            nFilterIndex   As   Long  
            lpstrFile   As   String  
            nMaxFile   As   Long  
            lpstrFileTitle   As   String  
            nMaxFileTitle   As   Long  
            lpstrInitialDir   As   String  
            lpstrTitle   As   String  
            flags   As   Long  
            nFileOffset   As   Integer  
            nFileExtension   As   Integer  
            lpstrDefExt   As   String  
            lCustData   As   Long  
            lpfnHook   As   Long  
            lpTemplateName   As   String  
  End   Type  
   
  '=======================================  
  '打开一个文件  
  '=======================================  
  Function   GetFile(ByVal   Hwnd   As   Long,   Optional   ByVal   dlgFilter   As   String   =   "所有文件(*.*)|*.*",   _  
          Optional   ByVal   dlgTitle   As   String   =   "打开文件",   Optional   ByVal   DefaultDir)   As   String  
          Dim   ofn   As   OPENFILENAME  
          Dim   Rtn   As   String  
          Dim   fn(0)   As   String  
          ofn.lStructSize   =   Len(ofn)  
          ofn.hwndOwner   =   Hwnd  
          ofn.hInstance   =   App.hInstance  
          ofn.lpstrFile   =   String(254,   Chr(0))  
          ofn.nMaxFile   =   255  
          ofn.lpstrFileTitle   =   String(254,   Chr(0))  
          ofn.nMaxFileTitle   =   255  
          ofn.lpstrFilter   =   ChangeStr(dlgFilter)   &   Chr(0)  
          ofn.lpstrTitle   =   dlgTitle  
          If   IsMissing(DefaultDir)   Then  
                  ofn.lpstrInitialDir   =   App.Path  
          Else  
                  ofn.lpstrInitialDir   =   DefaultDir  
          End   If  
          ofn.flags   =   &H80000   'Or   &H20  
          Rtn   =   GetOpenFileName(ofn)  
          If   Rtn   >=   1   Then  
                  GetFile   =   Left(ofn.lpstrFile,   InStr(1,   ofn.lpstrFile,   Chr(0))   -   1)  
          Else  
                  GetFile   =   ""  
          End   If  
  End   Function  
   
  '=======================================  
  '打开多个文件,返回一个字符串数组  
  '=======================================  
   
  Function   GetFiles(ByVal   Hwnd   As   Long,   Optional   ByVal   dlgFilter   As   String   =   "所有文件(*.*)|*.*",   _  
          Optional   ByVal   dlgTitle   As   String   =   "打开文件",   Optional   ByVal   DefaultDir)   As   String()  
          Dim   ofn   As   OPENFILENAME  
          Dim   Rtn   As   Long  
          Dim   fn(0)   As   String  
          ofn.lStructSize   =   Len(ofn)  
          ofn.hwndOwner   =   Hwnd  
          ofn.hInstance   =   App.hInstance  
          ofn.lpstrFile   =   String(2000,   Chr(0))  
          ofn.nMaxFile   =   2000  
          ofn.lpstrFileTitle   =   String(254,   Chr(0))  
          ofn.nMaxFileTitle   =   255  
          ofn.lpstrFilter   =   ChangeStr(dlgFilter)   &   Chr(0)  
          ofn.lpstrTitle   =   dlgTitle  
          If   IsMissing(DefaultDir)   Then  
                  ofn.lpstrInitialDir   =   App.Path  
          Else  
                  ofn.lpstrInitialDir   =   DefaultDir  
          End   If  
          ofn.flags   =   &H80000   or   &H200   'Or   &H20  
          Rtn   =   GetOpenFileName(ofn)  
          If   Rtn   >=   1   Then  
                  GetFiles   =   GetFileNames(ofn.lpstrFile)  
          Else  
                  GetFiles   =   fn  
          End   If  
  End   Function  
   
  '=======================================  
  '保存文件  
  '=======================================  
  Function   SaveFile(ByVal   Hwnd   As   Long,   ByVal   dlgFilterDescribe   As   String,   ByVal   dlgFilter   As   String,   _  
          Optional   ByVal   dlgTitle   As   String   =   "保存文件",   Optional   ByVal   dlgIniDir)   As   String  
          Dim   OFName   As   OPENFILENAME  
          With   OFName  
                  '设置结构的大小  
                  .lStructSize   =   Len(OFName)  
                  '设置父窗口  
                  .hwndOwner   =   Hwnd  
                  '设置程序的实例  
                  .hInstance   =   App.hInstance  
                  '设置过滤属性  
                  .lpstrFilter   =   dlgFilterDescribe   &   "(*"   &   dlgFilter   &   ")"   &   Chr(0)   &   "*"   &   dlgFilter   &   Chr(0)  
                  '设置默认扩展名  
                  .lpstrFile   =   String(254,   Chr(0))  
                  '设置返回的文件(全路径)的最大长度  
                  .nMaxFile   =   255  
                  '为文件名称创建缓冲区  
                  .lpstrFileTitle   =   String(254,   Chr(0))  
                  '设置返回的文件名称的最大长度  
                  .nMaxFileTitle   =   255  
                  '设置初始目录  
                  If   IsMissing(sIniDir)   Then  
                          .lpstrInitialDir   =   App.Path  
                  Else  
                          .lpstrInitialDir   =   sIniDir  
                  End   If  
                  '设置对话框标题  
                  .lpstrTitle   =   sTitle  
                  .flags   =   &H80000   'Or   &H20  
          End   With  
          If   GetSaveFileName(OFName)   Then  
                  SaveFile   =   Left(OFName.lpstrFile,   InStr(1,   OFName.lpstrFile,   Chr(0))   -   1)   &   Trim(dlgFilter)  
          Else  
                  SaveFile   =   ""  
          End   If  
  End   Function  
   
  Private   Function   ChangeStr(ByVal   strIn   As   String)   As   String  
          ChangeStr   =   Replace(strIn,   "|",   Chr(0))  
  End   Function  
   
  Private   Function   GetFileNames(ByVal   strIn   As   String)   As   String()  
          Dim   ln   As   Long,   num   As   Integer,   strPath   As   String,   strName   As   String  
          Dim   pos()   As   Long,   fn()   As   String  
          ReDim   pos(0)  
          pos(0)   =   1  
          strIn   =   Trim(strIn)  
          ln   =   Len(strIn)  
          For   i   =   1   To   ln  
                  If   Asc(Mid(strIn,   i))   =   0   Then  
                          num   =   num   +   1  
                          ReDim   Preserve   pos(num)  
                          pos(num)   =   i  
                          If   Asc(Mid(strIn,   i   +   1))   =   0   Then   Exit   For  
                  End   If  
          Next  
          If   num   =   1   Then  
                  ReDim   fn(0)  
                  fn(0)   =   Left(strIn,   pos(1))  
          Else  
                  num   =   num   -   2  
                  ReDim   fn(num)  
                  strPath   =   Left(strIn,   pos(1)   -   1)  
                  For   i   =   0   To   num  
                          fn(i)   =   strPath   &   "\"   &   Mid(strIn,   pos(i   +   1)   +   1,   pos(i   +   2)   -   pos(i   +   1))  
                  Next  
          End   If  
          GetFileNames   =   fn  
  End   Function  

以下是引用片段:
.Flags属性

.Flags=cdlOFNAllowMultiselect  
   
   
   
   
     
  Flags   属性(“打开”、“另存为”对话框)  
               
   
  为“打开”和“另存为”对话框返回或设置选项。  
   
  语法  
   
  object.Flags   [=   value]  
   
  Flags   属性语法有下列部分:  
   
  部分   描述    
  object   对象表达式,其值是“应用于”列表中的对象。    
  value   如“设置值”中所描述,是为“打开”和“另存为”对话框指定选项的常数或值。    
   
   
  设置值  
   
  Value   的设置值是:  
   
  常数   值   描述    
  cdlOFNAllowMultiselect   &H200   它指定文件名列表框允许多重选择。    
  运行时,通过按   SHIFT   键以及使用   UP   ARROW   和   DOWN   ARROW   键可选择多个文件。作完此操作后,FileName   属性就返回一个包含全部所选文件名的字符串。串中各文件名用空格隔开。  
     
  cdlOFNCreatePrompt   &H2000   当文件不存在时对话框要提示创建文件。该标志自动设置   cdlOFNPathMustExist   和   cdlOFNFileMustExist   标志。    
  cdlOFNExplorer   &H80000   它使用类似资源管理器的打开一个文件的对话框模板。适用于   Windows   95   和   Windows   NT   4.0。    
  CdlOFNExtensionDifferent   &H400   它指示返回的文件扩展名与   DefaultExt   属性指定的扩展名不一致。如果   DefaultExt   属性是   Null,或者扩展相匹配,或者没有扩展时,此标志不设置。当关闭对话框时,可以检查这个标志的值。    
  cdlOFNFileMustExist   &H1000   它指定只能输入文件名文本框已经存在的文件名。如果该标志被设置,则当用户输入非法的文件名时,要显示一个警告。该标志自动设置   cdlOFNPathMustExist   标志。    
  cdlOFNHelpButton   &H10   使对话框显示帮助按钮。    
  cdlOFNHideReadOnly   &H4   隐藏只读复选框。    
  cdlOFNLongNames   &H200000   使用长文件名。    
  cdlOFNNoChangeDir   &H8   强制对话框将对话框打开时的目录置成当前目录。    
  CdlOFNNoDereferenceLinks   &H100000   不要间接引用外壳链接(也称作快捷方式)。缺省时,选取外壳链接会引起它被外壳间接引用。    
  cdlOFNNoLongNames   &H40000   无长文件名。    
  CdlOFNNoReadOnlyReturn   &H8000   它指定返回的文件不能具有只读属性,也不能在写保护目录下面。    
  cdlOFNNoValidate   &H100   它指定公共对话框允许返回的文件名中含有非法字符。    
  cdlOFNOverwritePrompt   &H2   使“另存为”对话框当选择的文件已经存在时应产生一个信息框,用户必须确认是否覆盖该文件。    
  cdlOFNPathMustExist   &H800   它指定只能输入有效路径。如果设置该标志,输入非法路径时,应显示一个警告信息。    
  cdlOFNReadOnly   &H1   建立对话框时,只读复选框初始化为选定。该标志也指示对话框关闭时只读复选框的状态。    
  cdlOFNShareAware   &H4000   它指定忽略共享冲突错误。    
   
   
  说明  
   
  cdlOFNExplorer   和   cdlOFNNoDereferenceLinks   标志适用于   Windows   95   和   Windows   NT   4.0。Windows   95   中   cdlOFNExplorer   的公共对话框使用字符作为分隔符;而在没有   Windows   95   外壳的   Windows   NT   的早期版本中,多重选择是使用空格作为分隔符(固而不能支持长文件名)。  
   
  无论是在   Windows   NT   4.0   还是在   Windows   95   中,如果不选取   cdlOFNAllowMultiselect   标志,cdlOFNExplorer   和   cdlOFNLongNames   标志均没有意义,并且实际上是缺省值。  
   
  无论是在   Windows   NT   4.0   还是在   Windows   95   中,如果   cdlOFNAllowMultiselect   标志被单独使用,都不能支持长文件名。这是因为多重文件名要复现空格分隔符,而长文件名也可能包括空格符。在   Windows   NT   3.5   中,无法避免这种情况。如果使用   cdlOFNAllowMultiselect,就不能看到长文件名。如果在   Windows   95   中添加   cdlOFNExplorer   标志,就可以既能文件多选,又能看到长文件名。但是,这些文件名显现空字符分隔符,而不是空格分隔符隔开。因此,cdlOFNAllowMultiselect   和   cdlOFNExplorer   一起使用时,在   Windows   95   和   Windows   NT   4.0   中需要不同的文件名所得结果的语法分析。  
   
  这些常数在对象浏览器中的   Microsoft   CommonDialog   控件   (MSComDlg)   对象库中列出。  
   
  也可以定义所选择的标志。应使用启动窗体声明部分的   Const   关键字来定义想使用的标志。例如:  
   
  Const   ReadOnly     =   &H00000001&  
  Const   Effects         =   &H00000100&  
  CommonDialog1.Flags   =   &H10&   or   &H200&  
   
  将所需常数值能相加产生同样的结果。下例与上例等效:  
   
  CommonDialog1.Flags   =   &H210&    
   
  数据类型  
   
  Long  

From:http://topic.csdn.net/t/20050811/14/4203000.html

版权声明:
作者:Kiyo
链接:https://www.wkiyo.cn/html/2008-01/i434.html
来源:Kiyo's space
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>