调用Api函数GetOpenFileName来显示一个打开文件对话框时出错 (50分)

  • 主题发起人 主题发起人 631229
  • 开始时间 开始时间
6

631229

Unregistered / Unconfirmed
GUEST, unregistred user!
请看这个程序:(调用Api函数GetOpenFileName来显示一个打开文件对话框)<br>unit Unit1;<br>{$APPTYPE GUI}<br>interface<br>Uses<br>&nbsp; Messages,Sysutils,Commdlg,Windows;<br>Const<br>&nbsp; AppName = 'File Editor';<br>Type<br>&nbsp; TFileName = Array[0..Max_Path] Of Char;<br>Var<br>&nbsp; AMessage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Msg;<br>&nbsp; HWindow,HStatus,HEdit : HWnd;<br>&nbsp; TheLogFont &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TLogFont;<br>&nbsp; TheColor &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; FileName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TFileName;<br><br>implementation<br><br>Function SelectFile(Var FName:TFileName; Open:Boolean): Boolean;<br>Const<br>&nbsp; Filter : PChar = 'Text files (*.txt)'#0'*.txt'#0+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'All files (*.*)'#0'*.*'#0#0;<br>&nbsp; Ext &nbsp; &nbsp;: PChar = 'txt';<br>Var<br>&nbsp; NameRec : OpenFileName;<br>Begin<br>&nbsp; FillChar(NameRec,SizeOf(NameRec),0);<br>&nbsp; FName[0] := #0;<br>&nbsp; With NameRec Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; LStructSize := SizeOf(NameRec);<br>&nbsp; &nbsp; &nbsp; HWndOwner &nbsp; := HWindow;<br>&nbsp; &nbsp; &nbsp; LpStrFilter := Filter;<br>&nbsp; &nbsp; &nbsp; LpStrFile &nbsp; := @FName;<br>&nbsp; &nbsp; &nbsp; NMaxFile &nbsp; &nbsp;:= Max_Path;<br>&nbsp; &nbsp; &nbsp; Flags &nbsp; &nbsp; &nbsp; := OFN_Explorer Or OFN_HideReadOnly;<br>&nbsp; &nbsp; &nbsp; If Open Then<br>&nbsp; &nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Flags := Flags Or OFN_FileMustExist;<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; LpStrDefExt := Ext;<br>&nbsp; &nbsp; End;<br>&nbsp; If Open Then<br>&nbsp; &nbsp; &nbsp; SelectFile := GetOpenFileName(@NameRec)<br>&nbsp; Else<br>&nbsp; &nbsp; &nbsp; SelectFile := GetSaveFileName(@NameRec);<br>End;<br>...<br>...<br>编译时显示这行出错:SelectFile := GetOpenFileName(@NameRec)<br>[Error] Unit1.pas(75): Types of actual and formal var parameters must be identical<br>即:形参与实参类型不匹配,请帮助分析问题出在哪里?以下是Delphi的windows sdk帮助中<br>有关结构OPENFILENAME的定义,恕我愚笨,我看了很多Delphi的帮助,而且浏览了很多讲述Api<br>的英文网站,但还是没有搞明白。<br>typedef struct tagOFN { // ofn &nbsp;<br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; lStructSize; <br>&nbsp; &nbsp; HWND &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hwndOwner; <br>&nbsp; &nbsp; HINSTANCE &nbsp; &nbsp; hInstance; <br>&nbsp; &nbsp; LPCTSTR &nbsp; &nbsp; &nbsp; lpstrFilter; <br>&nbsp; &nbsp; LPTSTR &nbsp; &nbsp; &nbsp; &nbsp;lpstrCustomFilter; <br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; nMaxCustFilter; <br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; nFilterIndex; <br>&nbsp; &nbsp; LPTSTR &nbsp; &nbsp; &nbsp; &nbsp;lpstrFile; <br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; nMaxFile; <br>&nbsp; &nbsp; LPTSTR &nbsp; &nbsp; &nbsp; &nbsp;lpstrFileTitle; <br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; nMaxFileTitle; <br>&nbsp; &nbsp; LPCTSTR &nbsp; &nbsp; &nbsp; lpstrInitialDir; <br>&nbsp; &nbsp; LPCTSTR &nbsp; &nbsp; &nbsp; lpstrTitle; <br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; Flags; <br><br>&nbsp; &nbsp; WORD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nFileOffset; <br>&nbsp; &nbsp; WORD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nFileExtension; <br>&nbsp; &nbsp; LPCTSTR &nbsp; &nbsp; &nbsp; lpstrDefExt; <br>&nbsp; &nbsp; DWORD &nbsp; &nbsp; &nbsp; &nbsp; lCustData; <br>&nbsp; &nbsp; LPOFNHOOKPROC lpfnHook; <br>&nbsp; &nbsp; LPCTSTR &nbsp; &nbsp; &nbsp; lpTemplateName; <br>} OPENFILENAME; <br>
 
而且在这个语句里: &nbsp; &nbsp; &nbsp;LpStrFile &nbsp; := @FName;<br>将@号去掉Delphi后编译时也不提示有问题,而这个错误提示照旧:<br>Types of actual and formal var parameters must be identical<br>
 
我记不太清了,好象 TOpenFileName 和 OFN 中有一个变化,OFN 中的 Flags 在 TOpenFileName 中<br>改为了 TOpenOption ,不知是不是。
 
注意一下使用了var 变量,不能使用字符串,必须对应变量名。<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Commdlg;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>const<br>&nbsp; AppName = 'File Editor';<br>type<br>&nbsp; TFileName = array[0..Max_Path] of Char;<br>var<br>&nbsp; AMessage: Msg;<br>&nbsp; HWindow, HStatus, HEdit: HWnd;<br>&nbsp; TheLogFont: TLogFont;<br>&nbsp; TheColor: DWORD;<br>&nbsp; FileName: TFileName;<br><br>implementation<br><br>{$R *.DFM}<br><br>function SelectFile(var FName: TFileName; Open: Boolean): Boolean;<br>const<br>&nbsp; Filter: PChar = 'Text files (*.txt)'#0'*.txt'#0 +<br>&nbsp; &nbsp; 'All files (*.*)'#0'*.*'#0#0;<br>&nbsp; Ext: PChar = 'txt';<br>var<br>&nbsp; NameRec: OpenFileName;<br>begin<br>&nbsp; FillChar(NameRec, SizeOf(NameRec), 0);<br>&nbsp; FName[0] := #0;<br>&nbsp; with NameRec do<br>&nbsp; begin<br>&nbsp; &nbsp; LStructSize := SizeOf(NameRec);<br>&nbsp; &nbsp; HWndOwner := HWindow;<br>&nbsp; &nbsp; LpStrFilter := Filter;<br>&nbsp; &nbsp; LpStrFile := @FName;<br>&nbsp; &nbsp; NMaxFile := Max_Path;<br>&nbsp; &nbsp; Flags := OFN_Explorer or OFN_HideReadOnly;<br>&nbsp; &nbsp; if Open then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Flags := Flags or OFN_FileMustExist;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; LpStrDefExt := Ext;<br>&nbsp; end;<br>&nbsp; if Open then<br>&nbsp; &nbsp; SelectFile := GetOpenFileName(NameRec)<br>&nbsp; else<br>&nbsp; &nbsp; SelectFile := GetSaveFileName(NameRec);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; a: TFileName;<br>begin<br>&nbsp; a := 'C:/Autoexec.bat';<br>&nbsp; SelectFile(a, False);<br>end;<br><br>end.<br>
 
对了,你的这个程序还有些小问题,输入了文件名,不能定位,但是能够返回文件名。<br>在Button1的Click时间中加入:<br>ShowMessage(a)看看。
 
非常感谢!你说的是对的。但是我暂时还不想结束讨论,因为这个只是程序片段,整个程序本来是在<br>Dev-pascal环境下全部用API函数编写编辑器的(免费pascal和C++:http://www.freepascal.org/<br>和http://www.bloodshed.net/devpascal.html ),Dev-pascal环境下编译通过并能运行,且编译出<br>来的可执行文件只有23k大小。但原来程序用的是Edit,它不能编辑大文件,所以我改用Richedit(先<br>Loadlibrary('Riched32.dll'),再用CreatewindowEx()),能编辑大文件了,后来又发现Dev-pascal不<br>支持中文,所以想转为Delphi(我有6.0个人版)。改动的除了uses部分外就是上述你指教的了,现在别<br>的部分还有些问题(不多了),如果你有兴趣我可以把整个程序发过去(贴出来也行,不算太大)。<br>希望能讲一下这些基本概念:(这些概念在我手上的清华大学的《Delphi4.0入门与提高》里没有或很不详细):<br>符号'@'在变量前何意?符号'^'在变量前何意?在变量后又是何意?<br>Pchar是指针类型吗?如果是,则Const Ext : PChar = 'txt';这样的表达我很不理解。如C语言里<br>char mychara;<br>char * p;<br>p=&amp;mychar;<br>如果愿意分析整个程序,除了送分外,如果你在长沙我还可以为你刻盘:我收集了kylix个人版、Delphi6.0个人版、<br>borland C++5.0编译器、上述Dev-pascal和dev-C++、Fortran for win32、Prolog for win32个人版(人工智能)、<br>图灵机等,除了borland C++5.0只有编译器外,其他都是全套Compiler、IDE、Debugger等。
 
上述我收集的东西全是免费的!不存在盗版问题。
 
多人接受答案了。
 
后退
顶部