Fortran Coder

查看: 9949|回复: 6
打印 上一主题 下一主题

[绘图界面库] 求助!请问,在IVF下如何实现选择文件夹的对话框?

[复制链接]

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
608 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

楼主
发表于 2014-8-19 10:42:14 | 显示全部楼层
QuickWin 很方便的。

以下是一个例子

[Fortran] 纯文本查看 复制代码
program www_fcode_cn
  implicit none
  integer :: i
  character( Len = 512 ) :: c
  i = SelectFile( c )
  if ( i /=0 ) write(*,*) "Your select: " , trim(c)
contains

  Integer Function SelectFile( c )  !// 弹出选择文件对话框,等待用户选择输入文件
    use comdlg32 , only : GetOpenFileName , T_OPENFILENAME , OFN_FileMustExist
    use user32   , only : GetForegroundWindow
    use , intrinsic :: ISO_C_Binding , sz => c_null_char
    Implicit None
    character(Len=*) , Intent( OUT ) :: c
    Type(T_OPENFILENAME) :: ofn !// 定义打开文件对话框派生类型
    Character(Len=*),Parameter:: filter_spec = "ASCII文件" //sz// "*.txt" //sz//&
                                               "所有文件" //sz// "*.*"  //sz// &
                                               sz 
    Character(Len=*),Parameter:: cTitle = '请选择文件'//sz
    Character(Len=512) :: file_spec
    file_spec             = sz
    ofn%lStructSize       = c_sizeof(ofn)
    ofn%hwndOwner         = GetForegroundWindow()
    ofn%hInstance         = 0
    ofn%lpstrFilter       = transfer(c_loc(filter_spec),1)!// 指定文件过滤器
    ofn%lpstrCustomFilter = 0
    ofn%nMaxCustFilter    = 1
    ofn%nFilterIndex      = 1 !// 指定初始的 文件过滤器 序号
    ofn%lpstrFile         = transfer(c_loc(file_spec),1)
    ofn%nMaxFile          = c_sizeof(file_spec)
    ofn%nMaxFileTitle     = 0
    ofn%lpstrInitialDir   = 0
    ofn%lpstrTitle        = transfer(c_loc(cTitle),1) !// 指定打开文件对话框的标题
    ofn%Flags             = OFN_FileMustExist
    ofn%lpstrDefExt       = 0
    ofn%lpfnHook          = 0
    ofn%lpTemplateName    = 0
    SelectFile = GetOpenFileName( ofn )
    If ( SelectFile == 0) return
    c = file_spec( 1 : index( file_spec , sz ) - 1 )
  End Function SelectFile

end program www_fcode_cn

评分

参与人数 1F 币 +9 贡献 +9 收起 理由
珊瑚虫 + 9 + 9 赞一个!

查看全部评分

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
608 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

沙发
发表于 2014-8-19 13:50:44 | 显示全部楼层
你有两个选择。
简单的,先或的一个文件名,然后获得这个文件名所在的路径
[Fortran] 纯文本查看 复制代码
i = index( filename , "\" , back=.true. )
path = filename( 1 : i-1 )

另一个选择,就是用复杂一点的方法。
[Fortran] 纯文本查看 复制代码
Module SHBrowseForFolder_Mod
  use Shell32
  use , intrinsic :: ISO_C_Binding , sz => c_null_char
  Implicit None
  type T_BROWSEINFO
    integer         hwndOwner
    integer         pidlRoot
    integer         pszDisplayName
    type(C_PTR)     lpszTitle
    integer         ulFlags
    integer         lpfn
    integer         lParam
    integer         iImage
  end type
  INTERFACE
    Integer Function SHBrowseForFolder( lpbi )
    !DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'SHBrowseForFolderA' :: SHBrowseForFolder
      import
      type(C_PTR) :: lpbi
    End Function SHBrowseForFolder
    Integer Function SHGetPathFromIDList( pidl , pszPath )
    !DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'SHGetPathFromIDListA' :: SHGetPathFromIDList
    !DEC$ ATTRIBUTES REFERENCE :: pszPath
      INTEGER , value :: pidl 
      Character(len=*) :: pszPath
    End Function SHGetPathFromIDList 
  END INTERFACE 

End Module SHBrowseForFolder_Mod

program www_fcode_cn
  implicit none
  integer :: i
  character( Len = 512 ) :: c
  i = SelectPath( c )
  if ( i /= 0 ) write(*,*) "You Select :" , trim(c)

contains

  Integer Function SelectPath( c )
    use SHBrowseForFolder_Mod
    use user32  , only : GetForegroundWindow
    character( Len = * ) , Intent( OUT ) :: c
    Type(T_BROWSEINFO) :: ofn
    Character(Len=512) :: file_spec 
    Character(Len=64)  :: cTitle
    file_spec           = sz
    cTitle              = "您好,请选择文件夹:"//sz
    SelectPath          = 0
    ofn%hwndOwner       = GetForegroundWindow()
    ofn%pidlRoot        = 0
    ofn%pszDisplayName  = 0
    ofn%lpszTitle       = c_Loc(cTitle)
    ofn%ulFlags         = 0 
    ofn%lpfn            = 0
    ofn%lParam          = 0
    ofn%iImage          = 0
    SelectPath = SHBrowseForFolder( c_loc(ofn) )
    If ( SelectPath == 0 ) return
    SelectPath = SHGetPathFromIDList( SelectPath , file_spec )
    c = file_spec( 1 : index( file_spec , sz ) - 1 )
  End Function SelectPath
end program www_fcode_cn

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-19 11:59

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表