Help with long filenames using COMMDLG
HELP! I've been experimenting with the COMMDLG unit in order to try &
write a long filename dialog box for use in Delphi 1.0, the code of
which is below.
Adding the OFN_LONGFILENAMES flag to the OpenFileName record (required
by GetOpenFileName) forces the use of Windows 95 long filenames but
attempts to implement any of the other new flags (i.e. OFN_EXPLORER
(which I would really like!) or OFN_NODEREFERENCELINKS) are ignored.
The program produces the required result of displaying and returning
long filenames but the file-mask behaves weirdly and I have no idea of
how to retreive the filepath.
I think the file-mask problem is caused by the lack of a Hook
function, but my attempts at writing such a function (ported from
Visual C++ 4.0 code) have failed to solve the problem.
I would really appreciate any help in solving this admittedley trivial
problem (otherwise I may have to write my own dialog - NOT a nice
thought).
r...@sjuhnnu.demon.co.uk
-----------------------------------------------------------------------------------------------
unit Comdlg95;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, CommDlg;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
{Flag to set new explorer-style dialog - this flag doesn't work}
OFN_EXPLORER = $000080000;
{Flag to set dereferencing of .LNK files - this flag doesn't work}
OFN_NODEREFERENCELINKS = $000100000;
{Force long filenames - THIS FLAG WORKS!!!}
OFN_LONGFILENAMES = $000200000;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
OpenFileName: TOpenFilename;
DialogError : LongInt;
InitialDir,
InitialFileName,
ReturnedFileName : array [0..80] of Char;
begin
StrPCopy(InitialDir, 'C:\');
StrPCopy(InitialFileName, '*.*');
StrPCopy(ReturnedFileName, '');
OpenFileName.lStructSize := SizeOf(OpenFileName);
OpenFileName.hWndOwner := Handle;
OpenFileName.hInstance := HInstance;
OpenFileName.lpstrFilter := '*.doc';
OpenFileName.lpstrCustomFilter := NIL;
OpenFileName.nMaxCustFilter := 0;
OpenFileName.nFilterIndex := 0;
OpenFileName.lpstrFile := InitialFileName;
OpenFileName.nMaxFile := SizeOf(InitialFileName);
OpenFileName.lpstrFileTitle := ReturnedFileName;
OpenFileName.nMaxFileTitle := 0;
OpenFileName.lpstrInitialDir := InitialDir;
OpenFileName.lpstrTitle := 'Open sesame'; {Use default
dialog caption}
OpenFileName.Flags := OFN_FILEMUSTEXIST +
OFN_LONGFILENAMES +
OFN_PATHMUSTEXIST +
OFN_SHAREAWARE;
OpenFileName.nFileOffset := 0;
OpenFileName.nFileExtension := 0;
OpenFileName.lpstrDefExt := NIL;
OpenFileName.lCustData := 0;
OpenFileName.lpfnHook := NIL; {***I think this may be
bollocks***}
OpenFileName.lpTemplateName := NIL;
if not GetOpenFileName(OpenFileName) then
begin
DialogError := CommDlgExtendedError;
case DialogError of
0 : Form1.Caption := 'Cancelled dialog';
CDERR_FINDRESFAILURE : Form1.Caption := 'Find resource error';
CDERR_INITIALIZATION : Form1.Caption := 'Initialisation
error';
CDERR_LOADRESFAILURE : Form1.Caption := 'Load resource error';
CDERR_LOADSTRFAILURE : Form1.Caption := 'Load string error';
CDERR_MEMALLOCFAILURE: Form1.Caption := 'Memory allocation
error';
CDERR_MEMLOCKFAILURE : Form1.Caption := 'Memory lock error';
CDERR_NOHINSTANCE : Form1.Caption := 'No instance error';
CDERR_NOHOOK : Form1.Caption := 'No hook error';
CDERR_NOTEMPLATE : Form1.Caption := 'No template error';
CDERR_REGISTERMSGFAIL: Form1.Caption := 'Register window
error';
CDERR_STRUCTSIZE : Form1.Caption := 'Struct size error';
else
Form1.Caption := 'Unknown error ' + IntToStr(DialogError);
end;
end
else
Form1.Caption := 'OK';
Button1.Caption := StrPas(ReturnedFileName);
end;
end.