HTML Paste

I want to paste content from a website to my application, but I want to read the html code from the paste instead of the text only. So I tried this function:

function TForm1.GetHtmlFormatFromClipboard: string;
var
  hClipData       : THandle;
  idxEndFragment  : integer;
  idxStartFragment: integer;
  pClipData       : PChar;
begin
  Result := '';
  if IsClipboardFormatAvailable(RegisterClipboardFormat('HTML Format')) then begin
    Win32Check(OpenClipboard(0));
    try
      hClipData := GetClipboardData(RegisterClipboardFormat('HTML Format'));
      if hClipData <> 0 then begin
        pClipData := GlobalLock(hClipData);
        Win32Check(assigned(pClipData));
        try
          idxStartFragment := Pos('<!--StartFragment-->', pClipData); // len = 20
          idxEndFragment := Pos('<!--EndFragment-->', pClipData);
          if (idxStartFragment >= 0) and (idxEndFragment >= idxStartFragment) then
            Result := Copy(pClipData, idxStartFragment + 20, idxEndFragment - idxStartFragment - 20);
        finally GlobalUnlock(hClipData); end;
      end;
    finally Win32Check(CloseClipboard); end;
  end;
end;

The problem is that this function returns an empty string, and if I try to read the value of pClipData I olny see few squares. What do I do wrong?
0
Takis
10/31/2009 8:27:57 PM
embarcadero.delphi.non-tech 5933 articles. 1 followers. Follow

3 Replies
882 Views

Similar Articles

[PageSpeed] 16

<Takis Krokodeilakis> wrote in message news:178690@forums.codegear.com...

> The problem is that this function returns an empty string, and if I try
> to read the value of pClipData I olny see few squares. What do I do wrong?

Which version of Delphi are you using?  The HTML clipboard format uses UTF-8 
exclusively.  If you are using Delphi 2009 or higher, PChar is an alias for 
PWideChar, and thus will not be able to interpret the UTF-8 data correctly. 
Change your pClipData variable to PAnsiChar instead.

-- 
Remy Lebeau (TeamB)
0
Remy
11/2/2009 10:16:14 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Takis Krokodeilakis> wrote in message news:178690@forums.codegear.com...
> 
> > The problem is that this function returns an empty string, and if I try
> > to read the value of pClipData I olny see few squares. What do I do wrong?
> 
> Which version of Delphi are you using?  The HTML clipboard format uses UTF-8 
> exclusively.  If you are using Delphi 2009 or higher, PChar is an alias for 
> PWideChar, and thus will not be able to interpret the UTF-8 data correctly. 
> Change your pClipData variable to PAnsiChar instead.
> 
> -- 
> Remy Lebeau (TeamB)

I use Delphi 2009, I changed the variable to PAnsiChar. I can see the content now, but I can only see the characters that are in english. If the text in the website is written in another charset/language, I only see some strange characters. Is there any way to fix it? I also see some warnings (i'm guessing it's related to the charset problem) with the message -- Implicit string cast from 'AnsiString' to 'string' -- for the functions: Pos (I guess changing to ansipos would fix it, but this function works w
ell anyway), Copy and showmessage.
0
Takis
11/10/2009 1:30:19 AM
> I use Delphi 2009, I changed the variable to PAnsiChar. I can see the
> content now, but I can only see the characters that are in english.
> If the text in the website is written in another charset/language, I
> only see some strange characters. 

This will have been the case if you had used the original code in
earlier versions of Delphi too, since it assumes Latin-1 Ansi. That
said, as well as changing the type of pClipData to PAnsiChar, you
should change the result type to UTF8String. Furthermore, you should
avoid the implicit conversions to temporary string variables had by
calling Pos and Copy on a PChar/PAnsiChar/PWideChar - either get the
clipboard data into a UTF8String right away, or use StrPos and
SetString rather than Pos and Copy:

function GetHtmlFormatFromClipboard: UTF8String;
var
  Format: Word;
  Handle: THandle;
  StartPos, EndPos: Integer;
  WholeText: UTF8String;
begin
  Result := '';
  Format := RegisterClipboardFormat('HTML Format');
  if not IsClipboardFormatAvailable(Format) then Exit;
  OpenClipboard(0);
  try
    Handle := GetClipboardData(Format);
    if Handle = 0 then RaiseLastOSError;
    SetString(WholeText, PAnsiChar(GlobalLock(Handle)),
      GlobalSize(Handle));
    GlobalUnlock(Handle);
  finally
    CloseClipboard;
  end;
  StartPos := Pos('<!--StartFragment-->', WholeText);
  if StartPos = 0 then Exit else Inc(StartPos, 20);
  EndPos := Pos('<!--EndFragment-->', WholeText);
  if EndPos = 0 then Exit;
  Result := Copy(WholeText, StartPos, EndPos - StartPos);
end;

The resulting code should be compilable in both Ansi and Unicode
versions of Delphi, and in the latter, should solve the foreign
character issue too.

-- 
Chris Rolliston
http://delphihaven.wordpress.com/
0
Chris
11/10/2009 8:29:11 PM
Reply:

Similar Artilces:

HTML vs. non-HTML Datawindow
What's the difference in what it takes to get the HTML datawindow running? If I try to use the same datawindow that I used for a non-HTML datawindow, and use the same database connection, I get the error message "ERROR: Could not connect to server component" when trying to run the HTML version. What am I missing? How are you running the HTML Datawindow? Through PowerSite/DTC? Dynamo? Can you post your server-side script? Any errors in Jaguar's srv.log? -- pbm_hopethishelps, Roy Kiesler [TeamSybase] Sybase Developers Network (SDN) - http:///www.sybase.com/sdn ...

here's a tech question for non-tech
I have not had occasion to want to do this before, so I'm hoping someone can enlighten me. I have an app built in D7, and it's rather complex in its functionality. Is it possible to rebuild the app to run as a service? Would that require separating the user interface from the app? Thanks, Bill William Meyer wrote on 4/22/2010 : > I have not had occasion to want to do this before, so I'm hoping > someone can enlighten me. I have an app built in D7, and it's rather > complex in its functionality. Is it possible to rebuild the app to run > as a service?...

superreview granted: [Bug 384116] Pasting an image from clipboard uses bad quality JPEG-compression (non-user selectable) : [Attachment 322667] Patch for HTML editor (v3), comments added
David Bienvenu <bienvenu@nventure.com> has granted rsx11m.pub@gmail.com's request for superreview: Bug 384116: Pasting an image from clipboard uses bad quality JPEG-compression (non-user selectable) https://bugzilla.mozilla.org/show_bug.cgi?id=384116 Attachment 322667: Patch for HTML editor (v3), comments added https://bugzilla.mozilla.org/attachment.cgi?id=322667&action=edit ------- Additional Comments from David Bienvenu <bienvenu@nventure.com> this is definitely worth a try. ...

superreview requested: [Bug 384116] Pasting an image from clipboard uses bad quality JPEG-compression (non-user selectable) : [Attachment 322667] Patch for HTML editor (v3), comments added
rsx11m.pub@gmail.com has asked David Bienvenu <bienvenu@nventure.com> for superreview: Bug 384116: Pasting an image from clipboard uses bad quality JPEG-compression (non-user selectable) https://bugzilla.mozilla.org/show_bug.cgi?id=384116 Attachment 322667: Patch for HTML editor (v3), comments added https://bugzilla.mozilla.org/attachment.cgi?id=322667&action=edit ------- Additional Comments from rsx11m.pub@gmail.com Updated patch with comments added to switch() cases as requested. Double-checked that editor/ui/composer.js is used in the TB trunk build. Asking David for...

superreview requested: [Bug 270145] Cannot copy/paste a portion of a XHTML page that is in a CDATA section : [Attachment 167578] Refix the "non-HTML documents are all plaintext and text controls" hac
Boris Zbarsky <bzbarsky@mit.edu> has asked Johnny Stenback <jst@mozilla.jstenback.com> for superreview: Bug 270145: Cannot copy/paste a portion of a XHTML page that is in a CDATA section https://bugzilla.mozilla.org/show_bug.cgi?id=270145 Attachment 167578: Refix the "non-HTML documents are all plaintext and text controls" hacks https://bugzilla.mozilla.org/attachment.cgi?id=167578&action=edit ------- Additional Comments from Boris Zbarsky <bzbarsky@mit.edu> jst, would you review? ...

superreview granted: [Bug 270145] Cannot copy/paste a portion of a XHTML page that is in a CDATA section : [Attachment 167578] Refix the "non-HTML documents are all plaintext and text controls" hacks
Johnny Stenback <jst@mozilla.jstenback.com> has granted Boris Zbarsky <bzbarsky@mit.edu>'s request for superreview: Bug 270145: Cannot copy/paste a portion of a XHTML page that is in a CDATA section https://bugzilla.mozilla.org/show_bug.cgi?id=270145 Attachment 167578: Refix the "non-HTML documents are all plaintext and text controls" hacks https://bugzilla.mozilla.org/attachment.cgi?id=167578&action=edit ------- Additional Comments from Johnny Stenback <jst@mozilla.jstenback.com> r+sr=jst, but I'd split those if-then's up on individual ...

HTML in Delphi
I use Delphi 7 and CodeBase. I need to generate some HTML documents (letters with merges) to use for an Email form letters. Can anyone advise me on how to get started with this project? Perhaps there's a third party component library? But I have no idea how to store an HTML field into a CodeBase (Visual FoxPro data). I would appreciate any help available. Thanks, Jim Sawyer, DataWare Jim Sawyer wrote: > I would appreciate any help available. If you need to generate HTML in Delphi, may I humbly recommend my very own THTMLWriter? https://code.google.com/p/delphihtm...

Going from generic html.Form() to non-generic html.BeginForm()
Hi I have just upgraded to the ASP.NET MVC Beta (from one of the previews) where the Html.Form()-method now has been replaced by the Html.BeginForm() (and EndForm) and at the same time it has gone from generic to non-generic. Does anybody have a suggestion for what the best way will be to rewrite the following code to the new version? Here is a code example where the Authenticate-method return an ActionResult:<% using (Html.Form<AuthenticationController>(c => c.Authenticate())) { %> The layout for the login form<%} %>   Hi The BeginForm<...

Going from generic html.Form() to non-generic html.BeginForm()
Hi I have just upgraded to the ASP.NET MVC Beta (from one of the previews) where the Html.Form()-method now has been replaced by the Html.BeginForm() (and EndForm) and at the same time it has gone from generic to non-generic. Does anybody have a suggestion for what the best way will be to rewrite the following code to the new version? Here is a code example where the Authenticate-method return an ActionResult:<% using (Html.Form<AuthenticationController>(c => c.Authenticate())) { %> The layout for the login form<%} %> See http://forums.asp.net/t...

Delphi to HTML
Anyone know of an open source or freeware simple unit that does not rely on other comps and is threadable that can convert from Delphi source files to HTML? I found a few old units from 2003, but some rely on other compts, even visual ones and another looks like it might work but hasn't been updated for new keywords etc. Chad Hower wrote: > Anyone know of an open source or freeware simple unit that does not > rely on other comps and is threadable that can convert from Delphi > source files to HTML? In the past, Marco Cantu had such code out there. In additi...

DNN 3.0.12 Test/HTML module non-compliant HTML
Between DNN 3.0.11 and DNN 3.0.12, the HTMLModule.ascx changed from this: <%@ Control language="vb" Inherits="DotNetNuke.Modules.Html.HtmlModule" CodeBehind="HtmlModule.ascx.vb" AutoEventWireup="false" Explicit="True" %> <table cellspacing="0" cellpadding="4" width="100%" border="0" summary="Design Table"> <tr valign="top"> <td class="Normal" id="HtmlHolder" runat="server"></td> </tr> ...

ANN: Delphi HTML Reports preview
Very simple to use - buid complex reports in 10 minutes. All you need to know - SQL, HTML, CSS. Use all power of HTML4 and CSS3 in any part of report. Features: Barcodes Multiline headers Groups Cross-tabs (any level) Transformations (cross-tab with range) Pictures (+from database) Master/Detail (any level) Supports all databases and db-libraries (writing adapter for any db-acess library takes only 5 minutes) Standalone reports - no need for delphi forms/datasets. 100% native Delhi code. All Delphi versions - from Delphi 6 to XE6 (VCL). 32/64 bit. Supports Unicode for...

HTML Editor Library trial is now available for Delphi 6
HTML Editor Library trial is now available for Delphi 6 - Delphi XE8 Please use the following link to download the trial: http://delphihtmlcomponents.com/editor.html Hi! Do you have another emailadress than the support@delphihtmlcomponents.com, because I would like to upgrade to the Editor-package, but I can't email you. Gruß aus den Bergen Günter Please use asviridenkov at gmail.com > {quote:title=G|nter Kieninger wrote:}{quote} > Hi! > > Do you have another emailadress than the > support@delphihtmlcomponents.com, because I would like t...

HTML
--____NXPMEMTXWHFOIBVAQWEO____ Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable I'm trying to find out if there are any "tricks" or gotcha's regarding = pasting a "fancy" HTML document into the Groupwise 6.0 client. As a quick test, I used MS Publisher 2002 to create a 1-page newsletter. = I exported it as HTML; opened in Internet Explorer; select all; paste into = GW6 client. It seemed to work, although some of the fonts are not quite = right. I'm wondering if the MS-HTML junk is why the fonts didn'...

Web resources about - HTML Paste - embarcadero.delphi.non-tech

Paste Magazine :: The Best New Music, Movies, TV, Games, and Books :: Music and Movie News, Reviews ...
Paste Magazine is your companion in discovering the best new music, movies, tv, books, games and more. Discover why the Wall Street Journal called ...

Tomato paste - Wikipedia, the free encyclopedia
Tomato paste is a thick paste that is made by cooking tomatoes for several hours to reduce moisture, straining them to remove the seeds and skin, ...

Paste Books (@PasteBooks) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Cut and Paste-your photos on the App Store on iTunes
Get Cut and Paste-your photos on the App Store. See screenshots and ratings, and read customer reviews.

Paste-Ups by Mr. Brainwash - Flickr - Photo Sharing!
On 2nd Avenue Between East Houston Street and 1st Street on September 27, 2008

Transfer Solder Paste Syringe To Syringe Via The Luer Video - YouTube
This video shows how to transfer solder paste from a pre-filled air syringe to a Fishman AirFree® dispensing syringe. http://www.fishmancorp ...

Melbourne couple find maggot in tomato paste bought from Aldi
A Melbourne couple abandoned their dinner plans after opening a jar of Aldi tomato paste to find a maggot inside.

Opportunists choose Photoshop cut-and-paste over honeytraps
The photograph usually arrives as an email attachment or the old-fashioned way, in an envelope with no return address. It is rarely a pretty ...

Review: Paste, a simple, effective and pretty clipboard manager for the Mac
You know how it goes: you copy a link, or a piece of text, intending to paste it – then you get distracted and copy something else before you ...

Give your thumbs a rest and paste what you need with Kemail
... vibrations? Set up custom vibrations or Evernote updates Penultimate handwriting app for iPad Pro and Apple Pencil . Kemail keyboard to paste ...

Resources last updated: 12/24/2015 2:50:03 PM