主题:getfile()
wanggggg
[专家分:0] 发布于 2010-01-31 12:49:00
请教各位:
vf的getfile()对话框中每次只能选中一个文件,
有什么方法可以选中很多文件,对话框中“文件名”
显示这些被选中文件的文件名,待对话框返回后
程序在分别处理这些文件。
谢谢
回复列表 (共4个回复)
沙发
So_Show [专家分:470] 发布于 2010-01-31 13:42:00
**********************************************************************************************************************************
*--- 函数: GetOpenFileName
*--- 功能: 文件对话框
*=======================================
*--移動指定內存區域的內容至指定地址上
*=======================================
FUNCTION _GetOpenFileName
#define OFN_ALLOWMULTISELECT 0x00000200 &&constant for Flags member
#define OFN_EXPLORER 0x00080000 &&constant for Flags member
DECLARE INTEGER GetOpenFileName IN comdlg32;
STRING @ lpofn
*!* typedef struct tagOFNA {
*!* DWORD lStructSize; 0 //結構大小
*!* HWND hwndOwner; 4 //句柄值
*!* HINSTANCE hInstance; 8 //實例值
*!* LPCSTR lpstrFilter; 12 //過濾器值
*!* LPSTR lpstrCustomFilter; 16 //指向自定義的過濾器緩沖區
*!* DWORD nMaxCustFilter; 20
*!* DWORD nFilterIndex; 24 //顯示過濾器的內容
*!* LPSTR lpstrFile; 28 //指向目錄和文件名的路徑
*!* DWORD nMaxFile; 32 //nMaxFile緩沖區的大小
*!* LPSTR lpstrFileTitle; 36 //指向文件標題欄的標題欄
*!* DWORD nMaxFileTitle; 40 //文件標題欄的最大寬度
*!* LPCSTR lpstrInitialDir; 44 //初始化時的目錄
*!* LPCSTR lpstrTitle; 48 //指針
*!* DWORD Flags; 52
*!* WORD nFileOffset; 54
*!* WORD nFileExtension; 56
*!* LPCSTR lpstrDefExt; 60 //指針默認的擴展名
*!* LPARAM lCustData; 64
*!* LPOFNHOOKPROC lpfnHook; 68 //一個勾子的例程,這裡不需要用到
*!* LPCSTR lpTemplateName; 72 //指針
*!* #ifdef _MAC
*!* LPEDITMENU lpEditInfo;
*!* LPCSTR lpstrPrompt;
*!* #endif
*!* } OPENFILENAMEA, *LPOPENFILENAMEA;
*!* Size=76
*=======================================
*--移動指定內存區域的內容至指定地址上
*=======================================
DECLARE RtlMoveMemory IN kernel32 As CopyMemory;
Integer Destination,;
STring @Source, INTEGER nLength
*=======================================
*--移動指定內存區域的內容至到字符串的指定地址上
*=======================================
DECLARE RtlMoveMemory IN kernel32 As CopySTRMemory;
String @Destination,;
integer Source, INTEGER nLength
*=======================================
*--內存堆操作
*=======================================
#define HEAP_CREATE_ENABLE_EXECUTE 0x00040000 &&constant for flOptions
DECLARE INTEGER HeapCreate IN kernel32 ;
INTEGER flOptions,;
INTEGER dwInitialSize,;
INTEGER dwMaximumSize
DECLARE INTEGER HeapDestroy IN kernel32 ;
INTEGER hHeap
#define HEAP_GENERATE_EXCEPTIONS 0x00000004 && constant for dwFlags paramater
DECLARE INTEGER HeapAlloc IN kernel32 ;
INTEGER hHeap,;
INTEGER dwFlags,;
INTEGER dwBytes
DECLARE INTEGER HeapFree IN kernel32 ;
INTEGER hHeap,;
INTEGER dwFlags,;
INTEGER lpMem
hHeap=HeapCreate(HEAP_CREATE_ENABLE_EXECUTE ,0,0)
IF hHeap<=0
MESSAGEBOX("Cannot create the heap object with HeapCreate()",64,"error")
RETURN .f.
ENDIF
板凳
So_Show [专家分:470] 发布于 2010-01-31 13:43:00
*==實始化結構
LOCAL OPENFILENAMEA
OPENFILENAMEA=REPLICATE(CHR(0),76)
OPENFILENAMEA=STUFF(OPENFILENAMEA,1,4,api_inttostr(76)) &&:lStructSize成員
lpAddressOfFilter=0
lpstrFilter="ALL"+CHR(0)+ "*.*" +REPLICATE(CHR(0),2)
lnLenFilter=LEN(lpstrFilter)
hFilterAddress=HeapAlloc(hHeap,HEAP_GENERATE_EXCEPTIONS,lnLenFilter) &&分配內存堆
IF hFilterAddress<=0
MESSAGEBOX("Cannot allocate the Memory with HeapAlloc()",64,"error")
ENDIF
CopyMemory(hFilterAddress,lpstrFilter,lnLenFilter)
OPENFILENAMEA=STUFF(OPENFILENAMEA,13,4,api_inttostr(hFilterAddress)) &&:lpstrFilter成員
lpstrFile=SPACE(512)
lnLenFile=LEN(lpstrFile)
hFileAddress=HeapAlloc(hHeap,HEAP_GENERATE_EXCEPTIONS,lnLenFile) &&分配內存堆
IF hFileAddress<=0
MESSAGEBOX("Cannot allocate the Memory with HeapAlloc()",64,"error")
ENDIF
CopyMemory(hFileAddress,lpstrFilter,lnLenFile)
OPENFILENAMEA=STUFF(OPENFILENAMEA,29,4,api_inttostr(hFileAddress)) &&lpstrFile
OPENFILENAMEA=STUFF(OPENFILENAMEA,33,4,api_inttostr(lnLenFile)) &&nMaxFile
lpstrInitialDir=""+CHR(0) &&lpstrInitialDir
lnLenInitialDir=LEN(lpstrInitialDir)
hDirAddress=HeapAlloc(hHeap,HEAP_GENERATE_EXCEPTIONS,lnLenInitialDir)
IF hDirAddress<=0
MESSAGEBOX("Cannot allocate the Memory with HeapAlloc()",64,"error")
ENDIF
CopyMemory(hDirAddress,lpstrInitialDir,lnLenInitialDir)
OPENFILENAMEA=STUFF(OPENFILENAMEA,45,4,api_inttostr(hDirAddress))
lnFlag=BITOR(OFN_ALLOWMULTISELECT,OFN_EXPLORER)
OPENFILENAMEA=STUFF(OPENFILENAMEA,53,4,api_inttostr(lnFlag)) &&:Flags成員
IF GetOpenFileName (@OPENFILENAMEA)<>0
CopySTRMemory(@lpstrFile,hFileAddress,lnLenFile) &&取得某地址上內存,然後轉為字符串內容
lpstrFile=STRTRAN(TRIM(lpstrFile),CHR(0),CHR(9))&&getwordnum()無法取以chr分隔的字符,作個轉換
ELSE
*沒有選擇文件
ENDIF
*--釋放內存堆
IF HeapFree (hHeap,0,hFilterAddress)=0
MESSAGEBOX("Cannot frees the allocated Memory with HeapFree()",64,"error")
ENDIF
IF HeapFree (hHeap,0,hFileAddress)=0
MESSAGEBOX("Cannot frees the allocated Memory with HeapFree()",64,"error")
ENDIF
IF HeapFree (hHeap,0,hDirAddress)=0
MESSAGEBOX("Cannot frees the allocated Memory with HeapFree()",64,"error")
ENDIF
IF HeapDestroy(hHeap)=0
MESSAGEBOX("Cannot HeapDestroy the heap object with HeapDestroy()",64,"error")
RETURN .f.
ENDIF
RETURN lpstrFile
FUNCTION api_inttostr (tnValue)
#DEFINE mbase 2^8 -1
ch0=BITAND(mbase, tnValue)
ch1=BITAND(bitrshift(tnValue,8),mbase)
ch2=BITAND(bitrshift(tnValue,16),mbase)
ch3=BITAND(bitrshift(tnValue,24),mbase)
RETURN CHR(ch0)+CHR(ch1)+CHR(ch2)+CHR(ch3)
ENDFUNC
FUNCTION api_strtoint(tcSrcString )
RETURN Asc(SUBSTR(tcSrcString, 1,1)) + ;
BitLShift(Asc(SUBSTR(tcSrcString, 2,1)), 8) +;
BitLShift(Asc(SUBSTR(tcSrcString, 3,1)), 16) +;
BitLShift(Asc(SUBSTR(tcSrcString, 4,1)), 24)
ENDFUNC
FUNCTION isFile(tcstring)
RETURN "."$tcstring
ENDFUNC
************************************************************
把上面2回帖的代码 存个prg 运行后 调用 _GetOpenFileName()
3 楼
So_Show [专家分:470] 发布于 2010-01-31 13:49:00
使用方法。。。说明
*-----------------------------------------------------------------------------------
LOCAL cTmp,cSize,nTmp,lpstrFile,cpath,c原路径
c原路径=Fullpath(Curdir())
lpstrFile=_GetOpenFileName()
IF EMPTY(lpstrFile) OR ISNULL(lpstrFile)
RETURN
ENDIF
FOR n=1 TO GETWORDCOUNT(lpstrFile,CHR(9))
lcSecValue=GETWORDNUM(lpstrFile,n,CHR(9))
*--單選的情況下
IF n=1 AND isFile(lcSecValue)&&用戶只選了一個文件,返回的是文件全稱,中間沒有null值
?lcSecValue
EXIT
ELSE
*--多選的情況下
*--第一個為目錄
*--第二個起為文件
IF n=1
cpath=lcSecValue
ELSE
IF isFile(lcSecValue)
cTmp=cpath+'\'+lcSecValue
?ctmp
ELSE
EXIT &&該顯示的都顯示了
ENDIF
ENDIF
ENDIF
ENDFOR
set default TO &c原路径
4 楼
琅拿度 [专家分:2820] 发布于 2010-02-01 23:18:00
学习,多谢楼上大哥!
我来回复