Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a checkbox to TSaveDialog in Delphi 2010

I want to add a checkbox or other VCL component to the TSaveDialog.

Cantu says,

The new Vista Open and Save dialog boxes (implemented by the IFileOpenDialog and
IFileSaveDialog interfaces) are directly mapped by the new FileOpenDialog and
FileSaveDialog components, but also the standard OpenDialog and SaveDialog component uses
the new style when the global UseLatestCommonDialogs is set.

I have no idea what that means (I've never done any Interface programming...)

And I don't want to use third party tools.

I saw it suggested on a web search just now that I look at the TOpenPictureDialog code and copy it.

Before trying any of the paths, I thought I'd ask here for some guidance. Any suggestions on an XP through Win7 solution to adding a checkbox to a modern File, Open dialog box in a Windows-version independent manner?

Tom

like image 641
RobertFrank Avatar asked May 07 '11 01:05

RobertFrank


2 Answers

Robert, you can do that using a dialog template.

First you must store the template as a resource in your app, then load the template using the TOpenFilename structure (don't worry by the name, is the same for open and save dialogs) and finally call the GetSaveFileName function passing the TOpenFilename structure.

check this sample

Create a resource file (calledSaveDialog.rc) with the dialog template (look the MyCheckBox added)

MYSAVEFILE DIALOG -1, 1, 300, 60
STYLE DS_3DLOOK | DS_CONTROL | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS
CAPTION ""
FONT 8, "Tahoma"
{
CONTROL "MyCheckBox", 666, "button", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 84, 19, 60, 12
}

this is the source code

Uses
 CommDlg;

var
  lpofn    : TOpenFilename;
  lpstrFile: Array[0..MAX_PATH-1] of Char;

{$R *.dfm}
{$R SaveDialog.Res}

function _lpfnHook(hdlg: HWND; uiMsg:UINT;wParam:WPARAM;lParam:LPARAM): UINT stdcall;
begin
  Result:=0;
  case uiMsg of
    // Set the initial state of mycheckbox to checked
    WM_INITDIALOG : CheckDlgButton(hdlg,666,BST_CHECKED);
    WM_COMMAND    :
                   case wParam of
                    666:
                         begin
                          if (IsDlgButtonChecked(hdlg,666)=BST_CHECKED) then
                           ShowMessage('MyCheckBox was checked')
                          else
                          if (IsDlgButtonChecked(hdlg,666)=BST_UNCHECKED) then
                            ShowMessage('MyCheckBox was unchecked');
                         end;
                   end;
  end;
end;

procedure TFrmMain.Button1Click(Sender: TObject);
begin
  ZeroMemory(@lpofn,sizeof(lpofn));
  lpofn.lStructSize       := SizeOf(lpofn);
  lpofn.hwndOwner         := Handle;
  lpofn.hInstance         := hInstance;
  //set the filter name
  lpofn.lpstrFilter       := 'All files (*.*)'#0'*.*'#0#0;
  lpofn.lpstrTitle        := 'Save As';
  lpofn.lpstrFile         := lpstrFile;
  lpofn.nMaxFile          := MAX_PATH;
  //Set the template Name
  lpofn.lpTemplateName    :='MYSAVEFILE';
  //set the callback function
  lpofn.lpfnHook          := _lpfnHook;
  lpofn.Flags             := OFN_EXPLORER or OFN_CREATEPROMPT or  OFN_HIDEREADONLY or
                             OFN_PATHMUSTEXIST or OFN_ENABLEHOOK or OFN_ENABLETEMPLATE;
  //execute the dialog
  if GetSaveFileName(lpofn) then ShowMessage(lpofn.lpstrFile);
end;

and this is the output

enter image description here

like image 125
RRUZ Avatar answered Nov 07 '22 10:11

RRUZ


You can do this with a template but this leads to the legacy dialogs in Vista/7. On those platforms you should make use of IFileDialogCustomize. Of course to support XP you need to implement the template approach too.

like image 33
David Heffernan Avatar answered Nov 07 '22 10:11

David Heffernan