问题描述
PrivateDeclareFunctionGetForegroundWindowLib"user32"()AsIntegerPrivateDeclareFunctionGetWindowLongLib"user32"Alias"GetWindowLongA"(ByValhWndAsInteger,ByValnIndexAsInteger)AsInteger'''<summary>'''对话框返回错误信息'''</summary>'''<returns></returns>'''<remarks></remarks><DllImport("comdlg32.dll",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="CommDlgExtendedError")>_PrivateFunctionCommDlgExtendedError()AsIntegerEndFunction'''<summary>'''获取文件标题'''</summary>'''<paramname="lpszFile"></param>'''<paramname="lpszTitle"></param>'''<paramname="cbBuf"></param>'''<returns></returns>'''<remarks></remarks><DllImport("comdlg32.dll",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="GetFileTitleA")>_PrivateFunctionGetFileTitle(ByVallpszFileAsString,ByVallpszTitleAsString,ByValcbBufAsInteger)AsIntegerEndFunction<DllImport("comdlg32.dll",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="GetOpenFileNameA")>_PrivateFunctionGetOpenFileName(<[In],Out>ByValofnAsOPENFILENAME)AsBooleanEndFunction'''<summary>'''保存文件对话框API'''</summary>'''<paramname="ofn"></param>'''<returns></returns>'''<remarks></remarks><DllImport("comdlg32.dll",SetLastError:=True,CharSet:=CharSet.Auto,EntryPoint:="GetSaveFileNameA")>_PrivateFunctionGetSaveFileName(<[In],Out>ByValofnAsOPENFILENAME)AsBooleanEndFunctionPrivateStructureOPENFILENAMEDimlStructSizeAsIntegerDimhWndOwnerAsIntegerDimhInstanceAsIntegerDimlpstrFilterAsStringDimlpstrCustomFilterAsStringDimnMaxCustFilterAsIntegerDimnFilterIndexAsIntegerDimlpstrFileAsStringDimnMaxFileAsIntegerDimlpstrFileTitleAsStringDimnMaxFileTitleAsIntegerDimlpstrInitialDirAsStringDimlpstrTitleAsStringDimFlagsAsOFN_STYLEDimnFileOffsetAsShortDimnFileExtensionAsShortDimlpstrDefExtAsStringDimlCustDataAsIntegerDimlpfnHookAsIntegerDimlpTemplateNameAsStringDimpvReservedAsIntegerDimdwReservedAsIntegerDimFlagsExAsIntegerEndStructurePrivateEnumOFN_STYLEAsIntegerOFN_READONLY=&H1OFN_OVERWRITEPROMPT=&H2OFN_HIDEREADONLY=&H4OFN_NOCHANGEDIR=&H8OFN_SHOWHELP=&H10OFN_ENABLEHOOK=&H20OFN_ENABLETEMPLATE=&H40OFN_ENABLETEMPLATEHANDLE=&H80OFN_NOVALIDATE=&H100OFN_ALLOWMULTISELECT=&H200OFN_EXTENSIONDIFFERENT=&H400OFN_PATHMUSTEXIST=&H800OFN_FILEMUSTEXIST=&H1000OFN_CREATEPROMPT=&H2000OFN_SHAREAWARE=&H4000OFN_NOREADONLYRETURN=&H8000&OFN_NOTESTFILECREATE=&H10000OFN_NONETWORKBUTTON=&H20000OFN_NOLONGNAMES=&H40000OFN_EXPLORER=&H80000OFN_NODEREFERENCELINKS=&H100000OFN_LONGNAMES=&H200000OFN_ENABLEINCLUDENOTIFY=&H400000OFN_ENABLESIZING=&H800000OFN_DONTADDTORECENT=&H2000000OFN_FORCESHOWHIDDEN=&H10000000OFN_SHAREWARN=0OFN_SHARENOWARN=1OFN_SHAREFALLTHROUGH=2EndEnumPublicFunctionOpenFileDialog(OptionalFileFilterAsObject="",OptionalFilterIndexAsInteger=1,OptionalInitPathAsString="",OptionalTitleAsString="",OptionalMultiSelectAsBoolean=False,OptionalAllowHookAsBoolean=False)AsStringDimOFNAsOPENFILENAME=NewOPENFILENAME,sFileAsString="",iFilterAsIntegerDimmTypeNameAsString,mFilterAsString,mFilterIndexAsInteger,mInitPathAsString,mTitleAsString,mMultiSelectAsBoolean,mAllowHookAsBoolean'获取模块句柄DimMdlAsSystem.Reflection.Module=System.Reflection.Assembly.GetExecutingAssembly.GetModule(My.Application.Info.AssemblyName&".exe"),hInstanceAsIntPtrIfNotMdlIsNothingThenhInstance=Marshal.GetHINSTANCE(Mdl)Mdl=NothingEndIf'对话框标题IfTitle=""ThenmTitle="选择一个文件:"ElsemTitle=Title'初始路径IfInitPathLike"[C-Z]]:*"ThenmInitPath=InitPathElsemInitPath=CurDir()IfmInitPath="[C-Zc-z]:*[]"ThenmInitPath=Microsoft.VisualBasic.Left(mInitPath,Len(mInitPath)-1)'文件过滤mTypeName=TypeName(FileFilter)IfIsNumeric(FileFilter)=TrueThenIfFileFilter=1ThenmFilter="Excel2003工作薄,*.xls,Excel2007工作薄,*.xlsx,Excel启用宏工作薄,*.xlsm,所有Excel工作薄,*.xl*"ElseIfFileFilter=2ThenmFilter="Word2003文档,*.doc|Word2007文档,*.docx|Word启用宏文档,*.docm|所有文件,*.*"ElseIfFileFilter=3ThenmFilter="JPG图片,*.jpg|PNG图片,*.png|BMP图片,*.bmp|GIF图片,*.gif"ElseIfFileFilter=4ThenmFilter="文本文件,*.txt|所有文件,*.*"ElsemFilter="所有文件(*.*),*.*"EndIfElseIfmTypeName="String"ThenmFilter=CStr(FileFilter)ElsemFilter="AllFiles(*.*),*.*"EndIfmFilter=Replace(Replace(Replace(Replace(mFilter,"|",vbNullChar),",",vbNullChar),">",vbNullChar),"",vbNullChar)iFilter=(UBound(Split(mFilter,vbNullChar))+1)2IfFilterIndex>=1AndFilterIndex<=iFilterThenmFilterIndex=CInt(FilterIndex)ElsemFilterIndex=1mMultiSelect=(MultiSelect=1OrMultiSelect=True)mAllowHook=(AllowHook=1OrAllowHook=True)WithOFN.lStructSize=Len(OFN).Flags=OFN_STYLE.OFN_EXPLOREROrOFN_STYLE.OFN_FILEMUSTEXISTOrOFN_STYLE.OFN_PATHMUSTEXISTOrOFN_STYLE.OFN_NOLONGNAMESOrOFN_STYLE.OFN_HIDEREADONLYOrOFN_STYLE.OFN_DONTADDTORECENTIfmMultiSelect=TrueThen.Flags=.FlagsOrOFN_STYLE.OFN_ALLOWMULTISELECT.lpstrTitle=mTitle.hWndOwner=GetForegroundWindow().hInstance=GetWindowLong(.hWndOwner,-6).lpstrFilter=mFilter.lpstrInitialDir=mInitPath.nFilterIndex=mFilterIndex.nMaxFile=2048.lpstrFile=StrDup(.nMaxFile,vbNullChar).nMaxFileTitle=1024.lpstrFileTitle=StrDup(.nMaxFileTitle,vbNullChar).nFileOffset=0.nFileExtension=0IfmAllowHook=TrueThen.Flags=.FlagsOrOFN_STYLE.OFN_ENABLEHOOKOrOFN_STYLE.OFN_ENABLESIZING.lpfnHook=0.lCustData=0EndIfEndWithIfGetOpenFileName(OFN)=TrueThenDimPutDataAsIntegersFile=OFN.lpstrFilePutData=InStr(sFile,vbNullChar&vbNullChar)IfPutData>0ThensFile=Left(sFile,PutData-1)ElseIfRight(sFile,1)=vbNullCharThensFile=Left(sFile,Len(sFile)-1)EndIfFilterIndex=OFN.nFilterIndexPutData=InStr(sFile,vbNullChar)IfPutData>0ThenmInitPath=Left(sFile,PutData-1)InitPath=mInitPathIfRight(mInitPath,1)<>""ThenmInitPath=mInitPath&""sFile=Mid(sFile,PutData+1)sFile=mInitPath+Replace(sFile,vbNullChar,vbCrLf&mInitPath)ElseInitPath=Left(sFile,Len(sFile)-Len(Dir(sFile)))EndIfEndIfMsgBox(CommDlgExtendedError(),48)ReturnsFileEndFunction
请各位高手帮忙修改一下,谢谢大家!
解决方案
解决方案二:
直接用OpenFileDialog为什么不行呢
解决方案三:
使用OpenFileDialog控件类,不能够自定义