1. the iwebbrowser2_getdocument wrapper function returns a reference to the IHTMLDocument interface of the loaded page.
2. IHTMLDocument implements the IPersistFile interface and we can get a reference to it using WBQueryInterface.
3. using this reference, we will call the save method of the IPersistFile interface to save the page to a file.
If in the minibrowser example we replace the code in the Form1_Toolbar1_FileSave function for the code below, the page will be saved without promting.
Code:
' ======================================================================================== ' procedure: form1_toolbar1_filesave ' purpose: toolbar1 idm_file_save notification handler. ' return: true if message was processed, false if it was not. ' ======================================================================================== function form1_toolbar1_filesave _ ( _ byval hwnd as dword _ ' handle of window that owns the menu ) as long local hctrl as dword local ppvobj as dword local hr as long local ppdoc as dword local pppersistfile as dword local iid_ipersistfile as guid ' get the handle of the window that hosts the webbrowser hctrl = getdlgitem(hwnd, %idc_form1_webbrowser1) if isfalse hctrl then exit function ' get the idispatch of the control ppvobj = wbgetinterfacepointer(hctrl) if isfalse ppvobj then exit function iid_ipersistfile = guid$("{0000010b-0000-0000-c000-000000000046}") ' get a reference to the ihtmldocument interface ppdoc = iwebbrowser2_getdocument(ppvobj) if istrue ppdoc then ' get a reference to the ipersistfile interface pppersistfile = wbqueryinterface(ppdoc, iid_ipersistfile) if istrue pppersistfile then ' save the page calling the save method of the ipersistfile interface hr = ipersistfile_save(pppersistfile, "mysavedfile.htm", %false) if istrue hr then msgbox "error " & hex$(hr) & " saving the page" else msgbox "page saved" end if ' release the ipersistfile interface wbrelease pppersistfile end if ' release the ihtmldocument interface wbrelease ppdoc end if ' release the iwebbrowser2 interface wbrelease ppvobj end function
Code:
' **************************************************************************************** ' ipersistfile_save method ' saves an object to the specified stream. ' **************************************************************************************** ' hresult ( stdmethodcalltype __rpc_far *save )( ' ipersistfile __rpc_far * this, ' /* [unique][in] */ lpcolestr pszfilename, ' /* [in] */ bool fremember); ' **************************************************************************************** ' parameters ' **************************************************************************************** ' pszfilename ' [in] points to a zero-terminated string containing the absolute path of the file to ' which the object should be saved. if pszfilename is null, the object should save its ' data to the current file, if there is one. ' fremember ' [in] indicates whether the pszfilename parameter is to be used as the current working ' file. if true, pszfilename becomes the current file and the object should clear its ' dirty flag after the save. if false, this save operation is a "save a copy as ..." ' operation. in this case, the current file is unchanged and the object should not ' clear its dirty flag. if pszfilename is null, the implementation should ignore the ' fremember flag. ' **************************************************************************************** ' return values ' **************************************************************************************** ' %s_ok ' the object was successfully saved. ' %e_fail ' the file was not saved. ' ipersistfile::save stg_e_* errors. ' %e_pointer ' null pointer. ' **************************************************************************************** declare function proto_ipersistfile_save (byval pthis as dword ptr, byval pszfilename as dword, byval fremember as integer) as long ' **************************************************************************************** function ipersistfile_save (byval pthis as dword ptr, byval strfilename as string, byval fremember as integer) as long local hresult as long if isfalse pthis then function = %e_pointer : exit function if strfilename = " then call dword @@pthis[6] using proto_ipersistfile_save(pthis, %null, fremember) to hresult else strfilename = ucode$(strfilename) call dword @@pthis[6] using proto_ipersistfile_save(pthis, strptr(strfilename), fremember) to hresult end if function = hresult end function ' ****************************************************************************************
Leave a comment: