- unit uniopendialog;
- interface
- uses
- Windows,
- Unicode; // Lischke-Unicode Lib
- const
- Ofn_allowmultiselect = 512;
- Ofn_createprompt = 8192;
- Ofn_enablehook = 32;
- Ofn_enabletemplate = 64;
- Ofn_enabletemplatehandle = 128;
- Ofn_explorer = 524288;
- Ofn_extensiondifferent = 1024;
- Ofn_filemustexist = 4096;
- Ofn_hidereadonly = 4;
- Ofn_nochangedir = 8;
- Ofn_nolongnames = 262144;
- Ofn_nonetworkbutton = 131072;
- Ofn_noreadonlyreturn = 32768;
- Ofn_notestfilecreate = 65536;
- Ofn_novalidate = 256;
- Ofn_overwriteprompt = 2;
- Ofn_pathmustexist = 2048;
- Ofn_readonly = 1;
- Ofn_shareaware = 16384;
- Ofn_sharefallthrough = 2;
- Ofn_sharenowarn = 1;
- Ofn_sharewarn = 0;
- Ofn_showhelp = 16;
- type
- TUniOpenDialog = class
- private
- fFiles : TWideStrings;
- public
- constructor Create;
- destructor Destroy; override;
- function Execute(_AppHandle, _FormHandle : HWND) : Boolean;
- published
- property Files : TWideStrings read fFiles write fFiles;
- end;
- TOpenFileNameEx = packed record
- lStructSize: DWORD;
- hWndOwner: HWND;
- hInstance: HINST;
- lpstrFilter: PWideChar;
- lpstrCustomFilter: PWideChar;
- nMaxCustFilter: DWORD;
- nFilterIndex: DWORD;
- lpstrFile: PWideChar;
- nMaxFile: DWORD;
- lpstrFileTitle: PWideChar;
- nMaxFileTitle: DWORD;
- lpstrInitialDir: PWideChar;
- lpstrTitle: PWideChar;
- Flags: DWORD;
- nFileOffset: Word;
- nFileExtension: Word;
- lpstrDefExt: PWideChar;
- lCustData: LPARAM;
- lpfnHook: function(Wnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): UINT stdcall;
- lpTemplateName: PWideChar;
- pvReserved : Pointer;
- dwReserved : DWORD;
- FlagsEx : DWORD;
- end;
- TWideStringDynArray = array of WideString;
- function GetOpenFileNameEx(var OpenFile: TOpenFilenameEx): Bool; stdcall;
- function GetSaveFileNameEx(var OpenFile: TOpenFilenameEx): Bool; stdcall;
- implementation
- function GetOpenFileNameEx; external 'comdlg32.dll' name 'GetOpenFileNameW';
- function GetSaveFileNameEx; external 'comdlg32.dll' name 'GetSaveFileNameW';
- function ExtractStringFromStringArray(var P: PWideChar; Separator: WideChar = #0): WideString;
- var
- Start: PWideChar;
- begin
- Start := P;
- P := StrScanW(Start, Separator);
- if P = nil then begin
- Result := Start;
- P := StrEndW(Start);
- end else begin
- SetString(Result, Start, P - Start);
- Inc(P);
- end;
- end;
- function ExtractStringsFromStringArray(P: PWideChar; Separator: WideChar = #0): TWideStringDynArray;
- const
- GROW_COUNT = 1024;
- var
- Count: Integer;
- Item: WideString;
- begin
- Count := 0;
- SetLength(Result, GROW_COUNT);
- Item := ExtractStringFromStringArray(P, Separator);
- While Item <> '' do begin
- if Count > High(Result) then
- SetLength(Result, Length(Result) + GROW_COUNT);
- Result[Count] := Item;
- Inc(Count);
- Item := ExtractStringFromStringArray(P, Separator);
- end;
- SetLength(Result, Count);
- end;
- constructor TUniOpenDialog.Create;
- begin
- fFiles := TWideStringList.Create;
- end;
- destructor TUniOpenDialog.Destroy;
- begin
- fFiles.Free;
- end;
- function TUniOpenDialog.Execute(_AppHandle, _FormHandle : HWND) : Boolean;
- var
- ofn : TOpenFileNameEx;
- szFile : array [0..255] of WideChar;
- i : integer;
- data : TWideStringDynArray;
- P : PWideChar;
- begin
- Result := False;
- ZeroMemory(@ofn, SizeOf(TOpenFileNameEx));
- ofn.hInstance := _AppHandle;
- ofn.lStructSize := sizeof(ofn);
- ofn.hwndOwner := _FormHandle;
- ofn.lpstrFile := szFile;
- ofn.lpstrFile[0] := #0;
- ofn.nMaxFile := SizeOf(szFile);
- ofn.lpstrFilter := '';
- ofn.nFilterIndex := 1;
- ofn.lpstrFileTitle := '';
- ofn.nMaxFileTitle := 0;
- ofn.lpstrInitialDir := '';
- ofn.Flags := Ofn_hidereadonly or OFN_ALLOWMULTISELECT or OFN_EXPLORER;
- if GetOpenFileNameEx(ofn) = true then
- begin
- P := szFile;
- SetLength(data, 0);
- data := ExtractStringsFromStringArray(P);
- for I := 0 to high(data) do
- fFiles.Add(data[I]);
- Result := True;
- end;
- end;