2012-06-04 5 views
2

아래와 같이 FileSaveDialog1.Dialog.QueryInterface를 사용하여 푸시 버튼을 만들 수 있습니다. 버튼 클릭에 응답 할 수 있도록 OnPushButton 클릭 이벤트를 어떻게 설정하고 처리합니까?IFileDialogCustomize PushButton 이벤트 추가

procedure TForm1.FileSaveDialog1Execute(Sender: TObject); 
const 
    dwVisualGroup1ID: DWORD = 1900; 
var 
    c: IFileDialogCustomize; 
    d: IFileDialogControlEvents; 
begin 
    if FileSaveDialog1.Dialog.QueryInterface(IFileDialogCustomize, c) = S_OK then 
    begin 
    // Add a Advanced Button 
    c.AddPushButton(dwVisualGroup1ID, 'Advanced'); 
    c.MakeProminent(dwVisualGroup1ID); 
    // Setup the PushButton Click event? 

    end; 

답변

7

다음 작품 :

type 
    TMyFileDialogEvents = class(TInterfacedObject, IFileDialogEvents, IFileDialogControlEvents) 
    public 
    { IFileDialogEvents } 
    function OnFileOk(const pfd: IFileDialog): HResult; stdcall; 
    function OnFolderChanging(const pfd: IFileDialog; 
     const psiFolder: IShellItem): HResult; stdcall; 
    function OnFolderChange(const pfd: IFileDialog): HResult; stdcall; 
    function OnSelectionChange(const pfd: IFileDialog): HResult; stdcall; 
    function OnShareViolation(const pfd: IFileDialog; const psi: IShellItem; 
     out pResponse: DWORD): HResult; stdcall; 
    function OnTypeChange(const pfd: IFileDialog): HResult; stdcall; 
    function OnOverwrite(const pfd: IFileDialog; const psi: IShellItem; 
     out pResponse: DWORD): HResult; stdcall; 
    { IFileDialogControlEvents } 
    function OnItemSelected(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD; 
     dwIDItem: DWORD): HResult; stdcall; 
    function OnButtonClicked(const pfdc: IFileDialogCustomize; 
     dwIDCtl: DWORD): HResult; stdcall; 
    function OnCheckButtonToggled(const pfdc: IFileDialogCustomize; 
     dwIDCtl: DWORD; bChecked: BOOL): HResult; stdcall; 
    function OnControlActivating(const pfdc: IFileDialogCustomize; 
     dwIDCtl: DWORD): HResult; stdcall; 
    end; 

const 
    dwVisualGroup1ID: DWORD = 1900; 

function TMyFileDialogEvents.OnFileOk(const pfd: IFileDialog): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnFolderChange(const pfd: IFileDialog): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnFolderChanging(const pfd: IFileDialog; 
    const psiFolder: IShellItem): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnOverwrite(const pfd: IFileDialog; 
    const psi: IShellItem; out pResponse: DWORD): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnSelectionChange(const pfd: IFileDialog): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnShareViolation(const pfd: IFileDialog; 
    const psi: IShellItem; out pResponse: DWORD): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnTypeChange(const pfd: IFileDialog): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnItemSelected(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD; dwIDItem: DWORD): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnButtonClicked(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD): HResult; 
begin 
    if dwIDCtl = dwVisualGroup1ID then begin 
    // ... 
    Result := S_OK; 
    end else begin 
    Result := E_NOTIMPL; 
    end; 
end; 

function TMyFileDialogEvents.OnCheckButtonToggled(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD; bChecked: BOOL): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

function TMyFileDialogEvents.OnControlActivating(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD): HResult; 
begin 
    Result := E_NOTIMPL; 
end; 

.

var 
    FileDialog: IFileDialog = nil; 
    MyEvents: IFileDialogEvents = nil; 
    MyEventsCookie: DWORD = 0; 

procedure TForm1.FileSaveDialog1Execute(Sender: TObject); 
var 
    c: IFileDialogCustomize; 
    d: IFileDialogEvents; 
    cookie: DWORD; 
begin 
    if Supports(FileSaveDialog1.Dialog, IFileDialogCustomize, c) then 
    begin 
    // Add a Advanced Button 
    c.AddPushButton(dwVisualGroup1ID, 'Advanced'); 
    c.MakeProminent(dwVisualGroup1ID); 

    // Setup the PushButton Click event 
    d := TMyFileDialogEvents.Create; 
    if Succeeded(FileSaveDialog1.Dialog.Advise(d, cookie)) then 
    begin 
     FileDialog := FileSaveDialog1.Dialog 
     MyEvents := d; 
     MyEventsCookie := cookie; 
    end; 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    Ok: Boolean; 
begin 
    FileDialog := nil; 
    MyEvents := nil; 
    MyEventsCookie := 0; 

    try 
    Ok := FileSaveDialog1.Execute; 
    finally 
    if (FileDialog <> nil) and (MyEventsCookie <> 0) then 
     FileDialog.Unadvise(MyEventsCookie); 
    FileDialog := nil; 
    MyEvents := nil; 
    MyEventsCookie := 0; 
    end; 

    if Ok then ... 
end; 
+0

감사합니다 .. 이벤트가 잘 실행되고 있습니다. FileSaveDialog1.Dialog.Unadvise()를 호출해야합니까? – Bill

+0

Microsoft는 사용자가 Unadvise()를 호출한다고 가정합니다. 그러나'TFileSaveDialog'를 사용하려면'OnExecute' 이벤트에서'TFileSaveDialog.Dialog' 인터페이스에 대한 참조를 저장해야합니다. 그러면 Execute()가 종료 될 때 대화 상자가 해제되지 않습니다. 그런 다음 처리기를 Unadvise() 처리하고 참조를 해제하여 대화 상자를 종료합니다. 나는 그것을 보여주기 위해 나의 대답을 업데이트 할 것이다. –

+0

@MasonWheeler 왜 내가 그것을 썼는지 상상할 수 없다. 설명서가 명확합니다. 나는 코멘트를 삭제했다. –

2

IFileDialogControlEvents를 구현해야합니다. 그런 다음 IFileDialog.Advise를 호출하여 IFileDialogControlEvents 인터페이스를 전달합니다. 단추를 클릭하면 IFileDialogControlEvents.OnButtonClicked 메서드가 호출됩니다. XE2에서 나를 위해 좋은

+1

일부 코드를 표시 할 수 있습니까? – Bill

+2

아니요. 할 수 없습니다. 너는 너 자신을 나머지 할 수있다. 이 시점에서 당신은 필요한 모든 것을 가질 수 있습니다. 이제 모든 관련 인터페이스와 메소드를 알 수 있습니다. MSDN 문서를 사용하여 세부 정보를 입력하십시오. –

+0

어떻게 이벤트 클래스를 설정합니까? 유형 TTestEvent = 클래스 (TInterfacedObject, IFileDialogEvents, IFileDialogControlEvents) 함수 OnButtonClicked (pfdc : cardinal; dwIDCtl : 추기경) : HResult; – Bill