foxpro的getfile()选中很多文件的方法

***********************************************

*— 函数: 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

 

*==实始化结构

 

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

************************************************************

 

使用方法。。。说明

*———————————————————————————–

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原路径

 

 

 

 » 本站地址:http://www.gomoth.com

标签: