<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8622693766346389305</id><updated>2011-11-28T00:16:52.291+01:00</updated><category term='Dynamic-link library'/><category term='Active Server Pages'/><category term='ActiveX'/><category term='WYSIWYG'/><category term='tips'/><category term='Software development kit'/><category term='browser'/><category term='html'/><category term='programming'/><category term='internet'/><category term='source code'/><category term='JavaScript'/><category term='Borland'/><category term='html editing'/><category term='delphi'/><title type='text'>Frammenti Delphi</title><subtitle type='html'>Siete programmatori? Usate Delphi? Quante volte vi è capitato di dover scrivere delle porzioni di codice "riusabili"? procedure di utilità generale che potrebbero essere tranquillamente utilizzate anche in altri programmi... Beh, a me capita continuamente ed è per questo che mi è venuta voglia di condividere tutto questo con voi così da imparare l'un l'altro e migliorarci.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://frammentidelphi.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8622693766346389305/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://frammentidelphi.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Night Ranger</name><uri>http://www.blogger.com/profile/16833677831261843955</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8622693766346389305.post-7610634104028471927</id><published>2008-11-23T02:59:00.001+01:00</published><updated>2009-01-07T16:27:54.649+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='delphi'/><category scheme='http://www.blogger.com/atom/ns#' term='html editing'/><category scheme='http://www.blogger.com/atom/ns#' term='tips'/><category scheme='http://www.blogger.com/atom/ns#' term='internet'/><category scheme='http://www.blogger.com/atom/ns#' term='source code'/><category scheme='http://www.blogger.com/atom/ns#' term='Borland'/><category scheme='http://www.blogger.com/atom/ns#' term='WYSIWYG'/><category scheme='http://www.blogger.com/atom/ns#' term='Dynamic-link library'/><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Active Server Pages'/><category scheme='http://www.blogger.com/atom/ns#' term='Software development kit'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='ActiveX'/><category scheme='http://www.blogger.com/atom/ns#' term='html'/><category scheme='http://www.blogger.com/atom/ns#' term='browser'/><title type='text'>Quel mistero che si chiama ShDocVw.pas</title><content type='html'>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;strong&gt;&lt;span style="font-family: georgia; font-size: 130%;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style="font-family: georgia;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;&lt;br /&gt;&lt;strong style="color: rgb(0, 0, 0);"&gt;&lt;span style="font-family: georgia;"&gt;Quando non ci sono altre soluzioni&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;In un mio progetto mi sono trovato a dover interagire con alcune pagine HTML che erano online. Inizialmente ho pensato di lavorare con il codice della pagina, scaricata in locale e sulla quale avrei fatto le mie ricerche e le mie modifiche...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Inizialmente sembrava una buona idea e, soprattutto, di semplice realizzazione ma, alla realtà dei fatti, non era affatto così perchè mi ritrovavo di fronte a nuovi e più complicati problemi come il perdere i fogli di stile per strada, alcuni JavaScript risiedevano su file esterni e il loro indirizzo era mascherato/offuscato per impedirne il download, ma se almeno in parte questi problemi sono risolvibili, rimane sempre un caso per cui il mio approccio non poteva proprio essere applicato: le pagine dinamiche con script server-side come ASP, PHP, JSP o ancora altre tecnologie tipiche delle cosiddette Web Applications; in questi casi quello che ottenevo scaricando la pagina non era il codice dello script, ma il risultato di quest'ultimo che viene eseguit sul server prima dell'invio della pagina.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-family: georgia;"&gt;&lt;strong&gt;&lt;span style="font-family: georgia;"&gt;ShDocVw.pas: una miniera di risorse ma senza indicazioni&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Come risolvere allora il problema? Dovevo guardare altrove e la soluzione era nella MSDN tra la documentazione per VB: usare la Shell Doc Object and Control library, ossia la ShDocVw.dll per la quale con Delphi viene fornita una unit con lo stesso nome ShDocVw.pas che funge da wrapper per questa DLL&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Il bello di questa unit è che non si limita alla omonima DLL, ma, come se non bastasse, ci permette di utilizzare anche la MsHTML.dll che, facente parte del HTMLHelp SDK (gli help CHM) permette di editare le pagine in maniera visuale (WYSIWYG).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Sempre la stessa unit ShDocVw.pas, come se già non bastasse, continua a regalarci altre sorprese, dato che è responsabile dell'implementazione del componente TWebBrowser.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Qual'è il problema però comune a questi strumenti? Non sono minimamente documentati. Sebbene mettano a disposizione strumenti potentissimi, sia la Borland prima che la CodeGear/Embarcadero poi non hanno mai voluto fornire un sia pur minimo livello di documentazione. Oltretutto, le funzionalità più interessanti sono totalmente nascoste; prendiamo ad esempio il TWebBrowser: le proprietà e i metodi migliori non sono visibili nell'object inspector e l'help si limita a pochi accenni al resto e questa è la ragione per cui la maggior parte della gente si limita ad usarlo solo tramite il metodo Navigate e quei pochi eventi associati&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;Se ora vi è venuta voglia di studiarvi proprietà, metodi ed eventi di questa Unit (vi consiglio di studiarvi ben bene la documentazione della MSDN sulla MsHTML, prestando particolare cura alle classi IHTMLDocument2 e IHTMLBodyElement. Noi invece cominciamo a scrivere qualche linea di codice.&lt;/p&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;&lt;strong&gt;&lt;span style="font-size: 130%;"&gt;&lt;br /&gt;&lt;small&gt;&lt;span style="font-family: georgia;"&gt;Estrarre il testo di una pagina HTML&lt;/span&gt;&lt;/small&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style="color: rgb(0, 0, 0);"&gt;Prima di appropriarmi di questa tecnologia, per estrarre il testo da una pagina Web, estraevo tutti i tag HTML e così quel che rimaneva era quasi tutto il testo perdendo però le parti risultanti dall'elaborazione degli script. Con questo sistema e in 3 sole linee di codice si ottiene un risultato migliore di prima.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong style="color: rgb(0, 0, 0);"&gt;Esempio 1: &lt;/strong&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Per compilare, creare una form con un TWebBrowser e un TMemo.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Ricordarsi di aggiungere alla clausola Uses la unit MsHTML&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;table style=""&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=""&gt;&lt;br /&gt;&lt;pre style=""&gt;&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;procedure&lt;/span&gt;&lt;/strong&gt; TForm1.FormShow(Sender: TObject);&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;begin&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;   Web1.Navigate(&lt;span style="color: rgb(51, 51, 255);"&gt;'http://www.cryer.co.uk/brian/delphi/twebbrowser/twebbrowser_properties.htm'&lt;/span&gt;);&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;end;&lt;br&gt;&lt;br&gt;procedure &lt;/span&gt;&lt;/strong&gt;TForm1.Web1DocumentComplete(ASender: TObject; &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;const &lt;/span&gt;&lt;/strong&gt;pDisp: IDispatch; &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;var&lt;/span&gt;&lt;/strong&gt; URL: OleVariant);&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;var&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;  Range: IHTMLTxtRange;&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;begin&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;&lt;em&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;   // Inizializzazione della variabile Range come IHTMLTxtRange&lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;   Range &lt;strong&gt;:=&lt;/strong&gt; ((Web1.Document &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;as &lt;/span&gt;&lt;/strong&gt;IHTMLDocument2).body &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;as &lt;/span&gt;&lt;/strong&gt;IHTMLBodyElement).createTextRange;&lt;br&gt;   Range.select; &lt;em&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;// Seleziona Tutto&lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;   Memo1.Lines.Text := Range.Text; &lt;strong&gt;&lt;em&gt;&lt;span style="color: rgb(0, 153, 0);"&gt;// Mostra il risultato nel memo 1&lt;/span&gt;&lt;/em&gt;&lt;br&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;end;&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;&lt;br&gt;Non merita nemmeno di essere spiegato tanto è semplice. Magari rimane un po' ostico il passaggio di inizializzazione di Range ma lavorando con tipi OleVariant e con una doppia associazione è normale :) &lt;p&gt;&lt;/p&gt;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Esempio 2:&lt;/strong&gt;&lt;br&gt;Function per estrarre direttamente il testo, senza dover ricorrere a componenti sulla form.&lt;br&gt;Inserire nella clausola uses le unit MsHTML, ActiveX, ComObj&lt;br&gt;&lt;br&gt;&lt;table style=""&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=""&gt;&lt;br&gt;&lt;pre style=""&gt;&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;function &lt;/span&gt;&lt;/strong&gt;StripHTMLCode(aHTML: &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;string&lt;/span&gt;&lt;/strong&gt;): &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;string&lt;/span&gt;&lt;/strong&gt;;&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;var&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;  IDoc: IHTMLDocument2;&lt;br&gt;  v: Variant;&lt;br&gt;&lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;begin&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;  Idoc &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;:=&lt;/span&gt;&lt;/strong&gt; CreateComObject(Class_HTMLDOcument) &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;as&lt;/span&gt;&lt;/strong&gt; IHTMLDocument2;&lt;br&gt;  &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;try&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;    IDoc.designMode &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;:=&lt;/span&gt;&lt;/strong&gt; &lt;span style="color: rgb(51, 51, 255);"&gt;'on'&lt;/span&gt;;&lt;br&gt;    &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;while &lt;/span&gt;&lt;/strong&gt;IDoc.readyState &amp;lt;&amp;gt; &lt;span style="color: rgb(51, 51, 255);"&gt;'complete'&lt;/span&gt; &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;do&lt;/span&gt;&lt;/strong&gt;      Application.ProcessMessages;&lt;br&gt;    v := VarArrayCreate([&lt;span style="color: rgb(51, 51, 255);"&gt;0&lt;/span&gt;, &lt;span style="color: rgb(51, 51, 255);"&gt;0&lt;/span&gt;], VarVariant);&lt;br&gt;    v[&lt;span style="color: rgb(51, 51, 255);"&gt;0&lt;/span&gt;] := aHTML;&lt;br&gt;    IDoc.Write(PSafeArray(System.TVarData(v).VArray));&lt;br&gt;    IDoc.designMode := &lt;span style="color: rgb(51, 51, 255);"&gt;'off'&lt;/span&gt;;&lt;br&gt;    &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;while &lt;/span&gt;&lt;/strong&gt;IDoc.readyState &amp;lt;&amp;gt; &lt;span color="#3333ff"&gt;'complete'&lt;/span&gt; &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;do&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;      Application.ProcessMessages;&lt;br&gt;    Result := IDoc.body.innerText;&lt;br&gt;  &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;finally&lt;/span&gt;&lt;/strong&gt;    IDoc := &lt;span color="#3333ff"&gt;nil&lt;/span&gt;;&lt;br&gt;  &lt;strong&gt;&lt;span style="color: rgb(0, 0, 153);"&gt;end;&lt;br&gt;end;&lt;/span&gt;&lt;/strong&gt;&lt;br&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;Come si vede facilmente, tutto il lavoro è svolto dalla funzione StripHTMLCode che lavora utilizzando direttamente IHTMLDocument2.&lt;br /&gt;&lt;br /&gt;Per oggi ci fermiamo qui. Nel prossimo post approfondiremo IHTMLDocument2 e vedremo anche altre interessanti curiosità&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"&gt;&lt;a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/8120afea-65f8-4fce-a523-5cff8939076a/" title="Zemified by Zemanta"&gt;&lt;img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=8120afea-65f8-4fce-a523-5cff8939076a" alt="Reblog this post [with Zemanta]"&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8622693766346389305-7610634104028471927?l=frammentidelphi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://frammentidelphi.blogspot.com/feeds/7610634104028471927/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://frammentidelphi.blogspot.com/2008/11/la-misteriosa-e-potentissima-unit.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8622693766346389305/posts/default/7610634104028471927'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8622693766346389305/posts/default/7610634104028471927'/><link rel='alternate' type='text/html' href='http://frammentidelphi.blogspot.com/2008/11/la-misteriosa-e-potentissima-unit.html' title='Quel mistero che si chiama ShDocVw.pas'/><author><name>Night Ranger</name><uri>http://www.blogger.com/profile/16833677831261843955</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8622693766346389305.post-4504231132904512410</id><published>2008-11-23T00:07:00.000+01:00</published><updated>2008-11-23T09:18:58.287+01:00</updated><title type='text'>Una breve introduzione</title><content type='html'>&lt;strong&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-size:130%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-family:georgia;"&gt;&lt;span style="font-size:130%;"&gt;Ok, ci siamo.&lt;/span&gt; &lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;Siamo partiti. Cominciamo con il dire che, almeno nelle mie intenzioni iniziali, questo blog sarà dedicato a chi, anche se non conosce a fondo il Delphi, è quantomeno abituato a lavorare con linguaggi Object-Oriented.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;font-size:130%;"&gt;&lt;strong&gt;La mia idea &lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;La mia idea sarebbe quella di costruire una sorta di libreria di procedure e funzioni che possano servire da base ai nostri programmi... del resto non è nemmeno un'idea tanto campata in aria, visto che anche il &lt;a href="http://sourceforge.net/projects/jcl/"&gt;Project Jedi&lt;/a&gt; è nato allo stesso modo, lavorando tramite una mailing list.&lt;br /&gt;Se qualcuno volesse collaborare, potremmo allargare il discorso anche ad altri linguaggi come C++, C#, VB, ecc. e magari tradurre le stesse procedure in questi linguaggi.&lt;br /&gt;&lt;br /&gt;In ogni caso, la strada è tracciata. Speriamo ci porti tutti molto lontano :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8622693766346389305-4504231132904512410?l=frammentidelphi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://frammentidelphi.blogspot.com/feeds/4504231132904512410/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://frammentidelphi.blogspot.com/2008/11/una-breve-introduzione.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8622693766346389305/posts/default/4504231132904512410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8622693766346389305/posts/default/4504231132904512410'/><link rel='alternate' type='text/html' href='http://frammentidelphi.blogspot.com/2008/11/una-breve-introduzione.html' title='Una breve introduzione'/><author><name>Night Ranger</name><uri>http://www.blogger.com/profile/16833677831261843955</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
