Autor Beitrag
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: So 14.12.03 15:58 
Hi,

mit folgendem Code kann ich Open/Savedialogs unabhängig von den dazugehörigen Delphi-Compos aufrufen (mit allen neuen Implementierungen aus den neueren Betriebssystemversionen):

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
{Tipp von SDC: Open/Savedialog ohne VCL}

unit Unit16;

interface

uses Windows; 

function OpenSaveFileDialog(ParentHandle: THandle; const DefExt, Filter, InitialDir, Title: String; var FileName: String; IsOpenDialog: Boolean): Boolean; 

implementation

uses ShlObj, SysUtils; 

type 
  POpenFilenameA = ^TOpenFilenameA; 
  POpenFilename = POpenFilenameA; 
  tagOFNA = packed record 
    lStructSize: DWORD; 
    hWndOwner: HWND; 
    hInstance: HINST; 
    lpstrFilter: PAnsiChar; 
    lpstrCustomFilter: PAnsiChar; 
    nMaxCustFilter: DWORD; 
    nFilterIndex: DWORD; 
    lpstrFile: PAnsiChar; 
    nMaxFile: DWORD; 
    lpstrFileTitle: PAnsiChar; 
    nMaxFileTitle: DWORD; 
    lpstrInitialDir: PAnsiChar; 
    lpstrTitle: PAnsiChar; 
    Flags: DWORD; 
    nFileOffset: Word; 
    nFileExtension: Word; 
    lpstrDefExt: PAnsiChar; 
    lCustData: LPARAM; 
    lpfnHook: function(Wnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): UINT stdcall; 
    lpTemplateName: PAnsiChar; 
  end; 
  TOpenFilenameA = tagOFNA; 
  TOpenFilename = TOpenFilenameA; 

function GetOpenFileName(var OpenFile: TOpenFilename): Bool; stdcall; external 'comdlg32.dll'  name 'GetOpenFileNameA'; 
function GetSaveFileName(var OpenFile: TOpenFilename): Bool; stdcall; external 'comdlg32.dll'  name 'GetSaveFileNameA'; 

const 
  OFN_DONTADDTORECENT = $02000000; 
  OFN_FILEMUSTEXIST = $00001000; 
  OFN_HIDEREADONLY = $00000004; 
  OFN_PATHMUSTEXIST = $00000800; 

function CharReplace(const Source: String; oldChar, newChar: Char): String; 
var 
  i: Integer; 
begin 
  Result := Source; 
  for i := 1 to Length(Result) do 
    if Result[i] = oldChar then 
      Result[i] := newChar; 
end; 

function OpenSaveFileDialog(ParentHandle: THandle; const DefExt, Filter, InitialDir, Title: String; var FileName: String; IsOpenDialog: Boolean): Boolean; 
var 
  ofn: TOpenFileName; 
  szFile: array[0..MAX_PATH] of Char; 
begin 
  Result := false; 
  FillChar(ofn, SizeOf(TOpenFileName), 0); 
  with ofn do begin
    lStructSize := SizeOf(TOpenFileName); 
    hwndOwner := ParentHandle; 
    lpstrFile := szFile; 
    nMaxFile := SizeOf(szFile); 
    if (Title <> '') then 
      lpstrTitle := PChar(Title); 
    if (InitialDir <> '') then 
      lpstrInitialDir := PChar(InitialDir); 
    StrPCopy(lpstrFile, FileName); 
    lpstrFilter := PChar(CharReplace(Filter, '|', #0)+#0#0); 
    if DefExt <> '' then 
      lpstrDefExt := PChar(DefExt); 
    end; 
  if IsOpenDialog then begin 
    if GetOpenFileName(ofn) then begin
      Result := True;
      FileName := StrPas(szFile);
      end; 
  end else begin 
    if GetSaveFileName(ofn) then begin
      Result := True;
      FileName := StrPas(szFile);
      end;
    end; 
end; 

end.


Meine Frage: wie würde die äquivalente Funktion für einen TPrintDialog lauten?

AXMD

Moderiert von user profile iconPeter Lustig: Code- durch Delphi-Tags ersetzt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 15.12.03 11:20 
Titel: Bei Delphi abkucken
Hallo!

Hast Du schon mal in der Implementierung des PrintDialogs nachgesehen, wie das von Delphi gemacht wird?

Vielleicht funktioniert es da ja ähnlich, wie in deinem Quelltext...

MfG,
S.J.