首页  编辑  

打开文件对话框API

Tags: /超级猛料/VCL/Common.Dialog.通用对话框和控件/   Date Created:

function SelectFile(handle:hwnd;Filename:pchar;sbsize:dword;initdir:pchar;fileext:pchar;filter:pchar;caption:pchar):integer;stdcall;external 'shell32.dll' index 63;

Ex:

 selectfile(handle,buf,256,'c:\windows','*.exe','*.txt'#0+'*.txt;*.exe'#0+'*.exe'#0+'*.exe'#0#0,'title');

GetOpenFileName

type

 TLPOFNHOOKPROC=function(h:hwnd;uMsg:UINT;wp:wparam;lp:lParam):integer;

type

 TOpenInfo=packed record

    lStructSize:dword;

    hwndOwner:hwnd;

    hInstance:Hwnd;

    lpstrFilter:LPCTSTR;

    lpstrCustomFilter:LPTSTR;

    nMaxCustFilter:dword;

    nFilterIndex:dword;

    lpstrFile:lptstr; //该参数使用来存放对话框返回的文件名(包含路径)的,得是一个至少256个长的Pchar

    nMaxFile:dword; //该参数使用来指定info.lpstrFile长度以及存放对话框返回的文件名的长度的

    lpstrFileTitle:lptstr;

    nMaxFileTitle:dword;

    lpstrInitialDir:lpctstr;

    lpstrTitle:lpctstr;

    Flags:dword;

    nFileOffset:word;

    nFileExtension:word;

    lpstrDefExt:lpctstr;

    lCustData:Lparam;

    lpfnHook:TLPOFNHOOKPROC;

    lpTemplateName:lpctstr;

    pvReserved:integer;

    dwReserved:dword;

    FlagsEx:dword;

 end;

function GetOpenFileName(var info:TOpenInfo):boolean;stdcall; external 'comdlg32.dll'  name 'GetOpenFileNameA';

procedure TForm1.Button1Click(Sender: TObject);

var

 info:TOpenInfo;

 lpstrFile:array[0..1000] of char;

 lpstrFileTitle:array[0..1000] of char;

 lpstrFilter:array[0..50] of char;

 S:String;

begin

 FillChar(lpstrFile,SizeOf(lpstrFile),0);

 FillChar(lpstrFileTitle,SizeOf(lpstrFileTitle),0);

 FillChar(lpstrFilter,SizeOf(lpstrFilter),0);

 S:='文本文件';

 Move(S[1],lpstrFilter,Length(S));

 S:='*.TXT';

 Move(S[1],lpstrFilter[9],Length(S));

 info.lStructSize:=sizeof(info);

 info.hWndOwner:=handle;

 info.hInstance:=hinstance;

 info.lpstrFilter:=lpstrFilter;

 info.lpstrCustomFilter:=nil;

 info.nMaxCustFilter:=0;

 info.nFilterIndex:=1;

 info.lpstrFile:=lpstrFile;

 info.nMaxFile:=SizeOf(lpstrFile);

 info.lpstrFileTitle:=lpstrFileTitle;

 info.nMaxFileTitle:=SizeOf(lpstrFileTitle);

 info.lpstrInitialDir:='c:\';

 info.lpstrTitle:='Open Test';

 info.Flags:=0;//OFN_ENABLESIZING+OFN_EXPLORER;

 info.nFileOffset:=0;

 info.nFileExtension:=0;

 info.lpstrDefExt:='txt';

 info.lCustData:=0;

 info.lpfnHook:=nil;

 info.lpTemplateName:='';

 info.pvReserved:=0;

 info.dwReserved:=0;

 info.FlagsEx:=0;

 try

 if GetOpenFileName(info) then

 ShowMessage(info.lpstrFile);

 except

 caption:=SysErrorMessage(getlasterror);

 end;

end;