Program Baidu_Thlws
Implicit None
Integer iRes , SelectFile
character( Len = 512 ) :: cF
iRes = SelectFile( cF )
if ( iRes == 0 ) then
write( * , * ) '您没有选择文件'
else
write( * , * ) '您选择了文件:' , Trim( cF )
end if
End Program Baidu_Thlws
Integer Function SelectFile( cFile ) !// 弹出选择文件对话框,等待用户选择输入文件
Use comdlg32
Use User32
Use Kernel32
Implicit None
Character( Len = * ) :: cFile
Type(T_OPENFILENAME) :: ofn !// 定义打开文件对话框派生类型
Character( Len = * ),Parameter:: filter_spec = "ASCII文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0"C !// 打开文件对话框 文件过滤器
Character( Len = 512 ) :: file_spec = ""C
character( Len = 512 ) :: filefilter
Integer :: iLen , iRes
filefilter = "All files (*.txt)\0*.txt\0\0"C
ofn%lStructSize = SIZEOF(ofn)
ofn%hwndOwner = GetForegroundWindow()
ofn%hInstance = NULL
ofn%lpstrFilter = loc(filefilter) !loc(filter_spec) !// 指定文件过滤器
ofn%lpstrCustomFilter = NULL
ofn%nMaxCustFilter = 1
ofn%nFilterIndex = 1 !// 指定初始的 文件过滤器 序号
ofn%lpstrFile = loc(file_spec)
ofn%nMaxFile = sizeof(file_spec)
ofn%nMaxFileTitle = 0
ofn%lpstrInitialDir = Null
ofn%lpstrTitle = loc('请选择一个文件'C) !// 指定打开文件对话框的标题。
ofn%Flags = OFN_FileMustExist
ofn%lpstrDefExt = loc("txt"C)
ofn%lpfnHook = NULL
ofn%lpTemplateName = NULL
iRes = GetOpenFileName( ofn )
If ( iRes == 0) then
SelectFile = 0
Else
SelectFile = 1
iLen = Index( file_spec , CHAR(0) )
cFile = file_spec( 1 : iLen - 1 )
End If
End Function SelectFile