如何在程序中读出裁剪板内HTML格式的内容(100分)

  • 主题发起人 主题发起人 whf
  • 开始时间 开始时间
W

whf

Unregistered / Unconfirmed
GUEST, unregistred user!
请问:
我想在程序中读出裁剪板内HTML格式的内容,如何做到?万分谢谢!!!
 
那不过是带HTML标记的纯文本呀?
至于察看剪贴板内容嘛,这里有段程序,也是超来的,不过我试过挺好用

unit ClipboardViewer;

interface

uses
Windows, Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FNextViewerHandle : THandle;
procedure WMDrawClipboard (var message : TMessage);message WM_DRAWCLIPBOARD;
procedure WMChangeCBCHain (var message : TMessage); message WM_CHANGECBCHAIN;
end;

var Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
// Hook the clipboard viewer chain
// Should also check for a possible null return value, which indicates
// that the function failed.
FNextViewerHandle := SetClipboardViewer(Handle);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
// Remove ourselves from the clipboard viewer chain.
ChangeClipboardChain(Handle, FNextViewerHandle);
end;

procedure TForm1.WMDrawClipboard (var message : TMessage);
begin
// Called whenever contents of the clipboard changes
message.Result := SendMessage(WM_DRAWCLIPBOARD, FNextViewerHandle, 0, 0);
end;

procedure TForm1.WMChangeCBCHain (var message : TMessage);
begin
// Called when there is a change in the Clipboard viewer chain.
if message.wParam = FNextViewerHandle then
begin
// the next viewer in the chain is being removed. Update our internal var.
FNextViewerHandle := message.lParam;
// Return 0 to indicate message was processed
message.Result := 0;
end
else
begin // Pass message on to next window in chain.
message.Result := SendMessage(FNextViewerHandle, WM_CHANGECBCHAIN,message.wParam, message.lParam);
end;
end;
end.
 
yes, HTML实际是文本格式
 
方兄好象没有对获取的Clipboard 信息做处理嘛?
我改了一下:
procedure TForm1.WMDrawClipboard (var message : TMessage);
begin
// Called whenever contents of the clipboard changes
message.Result := SendMessage(WM_DRAWCLIPBOARD, FNextViewerHandle, 0, 0);
Clipboardchanged; //你自己的处理程序!!!
end;

procedure TForm1.ClipboardChanged;
var
i :integer;
s :string;
begin
if Clipboard.HasFormat(CF_TEXT) then
begin
s := clipboard.astext; //在这儿取得裁剪板内文本内容。
//你可以根据s里是否含有HTML关键字来判断是否HTML文本
memo1.lines.add(s);
end;
end;
 
各位:
我想得到的是HTML格式的信息,不是文本信息,就像FP98做到的那样.
 
HTML就是文本。FP98也是得到剪贴板文本再去解释为HTML
 
HTML就是文本。FP98也是得到剪贴板文本再去解释为HTML
王大虾的话是对的!

我的做法是这样:
1、在Delphi中安装ActiveX控件--Microsoft Internet Controls
2、在窗口中放一个TWebBrowser控件,
3、从剪贴板读出文本;
4、判断文本是否是HTML格式的,如果是则将它存入一个临时文件,
5、如果程序中发现剪贴板中是HTML文本,则用
Webrowser1.navigate(临时文件名,...) //具体用法你可以查看已答问题,
这样你就可以看到你想要的格式了!
 
将Clipboard的文本转存为Html后缀的新文件名,而后用Netscape/IE观察即可。
 
whf:
这个问题已经很长时间没有人参加讨论,为保持版面
整洁,节约网友时间,请提问者选择继续讨论或结束
问题。关于本版管理细则,请参见<a href="http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=111927">这个</a>问题。如两天
内提问者没有响应,我将采取强制措施。请提问者尽
可能自己处理自己的问题。这是您的权利,也是您的
义务。

如有管理建议,请到<a href="http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=111927">这里</a>提出。谢谢!
 
这是从M$主页上摘下来的:(不知道html标记是否会被屏蔽掉...)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>HTML Clipboard Format</TITLE>
<META NAME="description" CONTENT="HTML Clipboard Format" />
<META NAME="keywords" CONTENT="Internet Tools & Technologies" />
<META NAME="robots" CONTENT="all" />
<META NAME="MS.LOCALE" CONTENT="EN-US" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1" />
<META NAME="Author" CONTENT="InetSDK" />
<SCRIPT LANGUAGE="JavaScript"><!--

// -----------------------------------------------------------
// Client-side BrowserData constructor
// Populated using data from server-side oBD object to avoid redundancy
// -----------------------------------------------------------

function BrowserData()
{
this.userAgent = "Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)";
this.browser = "MSIE";
this.majorVer = 4;
this.minorVer = "01";
this.betaVer = "0";
this.platform = "98";
this.getsNavBar = true;
this.doesActiveX = true;

}
var oBD = new BrowserData();

//--></SCRIPT>


<SCRIPT LANGUAGE="JavaScript"><!--

// if workshop and not vroot, assign "/workshop/" to sBucketRoot
// rename sVRoot

// var sBucketRoot = "/workshop/networking/";

//--></SCRIPT>
<LINK REL="stylesheet" TYPE="text/css" HREF="/msdn-online/shared/css/ie4.css" />

<SCRIPT LANGUAGE="JScript"><!--

function CommonLoad()
{
if ("function" == typeof(InitNavLinks)) InitNavLinks();
if ("function" == typeof(CheckToTop)) CheckToTop();
if ("function" == typeof(RemoveLastPipe)) RemoveLastPipe();
}

//--></SCRIPT>



<META NAME="MS-HAID" CONTENT="_inet_HTML_Clipboard_Format">
</HEAD>
<BODY TOPMARGIN="0" LEFTMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0" BGCOLOR="#FFFFFF">
<A NAME="bk_clipboard"></A>

<!-- HEADER_START -->


<DIV CLASS="clsNavLinksDiv">
<TABLE CELLPADDING="1" CELLSPACING="0" BORDER="0">
<TR><TD COLSPAN="3"> </TD></TR>
<TR>
<TD ROWSPAN="2"><A HREF="/workshop/networking/default.asp"><IMG HEIGHT="32" WIDTH="31" VSPACE="5" BORDER="0" ALT="Click to return to the Networking, Protocols & Data Formats home page" SRC="/workshop/graphics/icons/networking.gif" /></A></TD><TD ROWSPAN="2" WIDTH="18">  </TD>
<TD VALIGN="top">

<TABLE ID="tblNavLinks" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR>
<TD><A ID="lnkShowImg" HREF="/workshop/networking/toc.htm" TARGET="_top"><IMG ID="imgShow" CLASS="clsLeftMenu" HEIGHT="15" WIDTH="18" BORDER="0" SRC="/msdn-online/shared/graphics/icons/nl-show-0.gif" ALT="Show/Hide TOC in left frame" /></A> <BR></TD>
<TD NOWRAP><A ID="lnkShowText" CLASS="clsLeftMenu" HREF="/workshop/networking/toc.htm" TARGET="_top" TITLE="Show/Hide TOC in left frame">show toc</A>       </TD>

<TD><A ID="lnkSyncImg" HREF="/workshop/c-frame.htm?934204577186#/workshop/networking/clipboard/htmlclipboard.asp" TARGET="_top"><IMG ID="imgSync" CLASS="clsLeftMenu" HEIGHT="15" WIDTH="18" BORDER="0" SRC="/msdn-online/shared/graphics/icons/nl-sync-0.gif" ALT="Synchronize TOC and Address Bar with this page" /></A> <BR></TD>
<TD NOWRAP><A ID="lnkSyncText" CLASS="clsLeftMenu" HREF="/workshop/c-frame.htm?934204577186#/workshop/networking/clipboard/htmlclipboard.asp" TARGET="_top" TITLE="Synchronize TOC and Address Bar with this page">sync toc</A>       </TD>

<TD><A ID="lnkIndexImg" HREF="/workshop/index/default.asp"><IMG ID="imgIndex" CLASS="clsLeftMenu" HEIGHT="15" WIDTH="18" BORDER="0" SRC="/msdn-online/shared/graphics/icons/nl-index-0.gif" ALT="Go to an alphabetical keyword list" /></A> <BR></TD>
<TD NOWRAP><A ID="lnkIndexText" CLASS="clsLeftMenu" HREF="/workshop/index/default.asp" TITLE="Go to an alphabetical keyword list">index</A>       </TD>

</TR></TABLE>

</TD></TR>
<TR><TD>

<TABLE ID="tblPrevNext" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR> <TD WIDTH="17"><A HREF="/workshop/networking/default.asp"><IMG ID="imgUp" CLASS="clsLeftMenu" HEIGHT="13" WIDTH="17" BORDER="0" TITLE="Networking, Protocols & Data Formats" SRC="/msdn-online/shared/graphics/icons/nl-up-0.gif" /></A></TD>
<TD NOWRAP><A ID="lnkUpText" CLASS="clsLeftMenu" HREF="/workshop/networking/default.asp" TITLE="Networking, Protocols & Data Formats">Networking, Protocols & D...</A>     </TD>
</TR></TABLE>

</TD></TR>
</TABLE>
</DIV>

<DIV CLASS="clsBucketBranch"><A HREF="/workshop/default.asp" TARGET="_top">Web Workshop</A>  |  <A HREF="/workshop/c-frame.htm?934204577171#/workshop/networking/default.asp" TARGET="_top">Networking, Protocols & Data Formats</A></DIV>
<!-- HEADER_END -->


<DIV CLASS="clsDocBody">
<!-- CONTENTS_START -->
<H1><A NAME="ch_htmlclipboard">HTML Clipboard Format</A></H1>
<HR SIZE="1">

<P>This article discusses cutting and pasting fragments of an HTML document. The CF_HTML clipboard format allows a fragment of raw HTML text and its context to be stored on the clipboard as ASCII. This allows the context of the HTML fragment, which consists of all preceding and surrounding tags, to be examined by an application so that the surrounding tags can be noted with their attributes. Although it is up to an application to interpret such fragments, some basic guidelines are included here based on MSHTML implementations.</P>

<P>
This article covers the following topics:</P>
<ul><li><A CLASS=tctop HREF="/workshop/networking/clipboard/htmlclipboard.asp#Overview of CF_HTML">Overview of CF_HTML</A>
<li><P><A CLASS=tctop HREF="/workshop/networking/clipboard/htmlclipboard.asp#Scenarios">Scenarios</A></ul>

<H2><A NAME="Overview of CF_HTML">Overview of CF_HTML</A></H2>
<P>CF_HTML is entirely text format and uses the transformation format UTF-8. It includes a description, a context, and within the context, the fragment. The format name of a clipboard containing CF_HTML data
is HTML Format. Therefore, if you use functions such as <A HREF="/workshop/external_Win32.asp#_win32_RegisterClipboardFormat">RegisterClipboardFormat</A>, pass "HTML Format" in the format name parameter. This section gives a code example of a clipboard using CF_HTML and discusses the three components of the CF_HTML clipboard format.</P>

<P>The following is an example of a clipboard:</P>

<PRE CLASS="clsCode">
Version:0.9
StartHTML:71
EndHTML:170
StartFragment:140
EndFragment:160
<B>StartSelection:140</B>
<B>EndSelection:160</B>
<!DOCTYPE>
<HTML>
<HEAD>
<TITLE>The HTML Clipboard</TITLE>
<BASE HREF="http://sample/specs">
</HEAD>
<BODY>
<!--StartFragment -->
<B><P>The Fragment</P></B>
<!--EndFragment -->
</BODY>
</HTML>
</PRE>
<H3>Description</H3>
<P>The description includes the clipboard version number and offsets, indicating where the context and the fragment start and end. The description is a list of ASCII text keywords followed by a string and separated by a colon (:).</P>

<TABLE CLASS="clsStd">
<tr><td>Version <i>vv</i></td><td>Version number of the clipboard. Starting version is 0.9. </td></tr>
<tr><td>StartHTML</td><td>Byte count from the beginning of the clipboard to the start of the context, or -1 if no context.</td></tr>
<tr><td>EndHTML</td><td>Byte count from the beginning of the clipboard to the end of the context, or -1 if no context.</td></tr>
<tr><td>StartFragment</td><td>Byte count from the beginning of the clipboard to the start of the fragment.</td></tr>
<tr><td>EndFragment</td><td>Byte count from the beginning of the clipboard to the end of the fragment.</td></tr>
<tr><td>StartSelection</td><td>Byte count from the beginning of the clipboard to the start of the selection. </td></tr>
<tr><td>EndSelection</td><td>Byte count from the beginning of the clipboard to the end of the selection.</td></tr> </table>

<P>The StartSelection and EndSelection keywords are optional
because sufficient information for basic pasting is included in the fragment description. However, the selection information indicates the exact HTML area the user has selected. This adds more information to the fragment description. </P>


<P>Other information may be added in the description section. For example, multiple pairs of StartFragment/EndFragment could be added to support noncontiguous selection of fragments.</P>

<P>The only character set supported by the clipboard is Unicode in its UTF-8 encoding. Because the first characters of UTF-8 and ASCII match, the description is always ASCII, but the bytes of the context (starting at StartHTML) may use any other characters coded in UTF-8. Ends of lines may be represented in a clipboard format header as Carriage Return (CR), Carriage Return/Line Feed (CR/LF), or Line Feed (LF).</P>

<H3>Context</H3>
<P>When working with a complete document, the context is the entire HTML document. Because this discussion is limited to copying and pasting a fragment of a document, the context is the selected fragment and all preceding and surrounding start and end tags. These tags represent all the parent nodes of the fragment, up to the HTML node. The context also contains the complete <A HREF="/workshop/author/dhtml/reference/objects/HEAD.asp">HEAD</a> element and allows the <A HREF="/workshop/author/dhtml/reference/objects/BASE.asp">BASE</a> and <A HREF="/workshop/author/dhtml/reference/objects/TITLE.asp">TITLE</a> elements to be included. Sufficient information is included in the fragment for a basic pasting operation. However, if your application requires information concerning the tags surrounding the fragment, you must store the context on the clipboard.
</P>

<P>An application copying a fragment of HTML to the clipboard may choose to create a <b>BASE</b> element to include in the context so that partial URLs in the fragment can be resolved.</P>

<H3>Fragment</H3>
<P>The fragment contains valid HTML representing the area the user has selected. This includes the information required for basic pasting of an HTML fragment, as follows:</P>

<UL>
<LI>Selected text.
<LI>Opening tags and attributes of any element that has an end tag within the selected text.
<LI>End tags that match the included opening tags.
</UL>

<P>The fragment should be preceded and followed by the HTML comments <!--StartFragment--> and <!--EndFragment--> (no space allowed between the !-- and the text) to indicate where the fragment starts and ends. So the start and end of the fragment are indicated by these comments as well as by the StartFragment and EndFragment byte counts. Though redundant, this makes it easier to find the start of the fragment (from the byte count) and mark the position of the fragment directly in the HTML tree.</P>


<H2><A NAME="Scenarios">Scenarios</A></H2>
<P>The following scenarios describe how the MSHTML HTML editor handles HTML cutting and pasting; other applications may or may not follow these scenarios. The clipboard format described here is intended to allow flexibility for the way an application functions. These scenarios show only well-formed HTML.</P>

<H3>Simple Fragment of HTML</H3>
<P>HTML text:</P>
<PRE CLASS="clsCode">
<BODY> This is normal. <B>This is bold.</B> <I><B>This is bold italic.</B></I>
<I>This is italic.</I> </BODY>
</PRE>
<P>This appears as:</P>
<DIV Style='text-indent:"1cm"'>
<P>This is normal. <B>This is bold.</B> <i><B>This is bold italic.</B></i>
<I>This is italic.</I></P>
</DIV>
<P>The text between the ** is selected and copied to the
clipboard: </P>
<DIV Style='text-indent:"1cm"'>
<P>This is normal. <B>This is </B>**<B>bold.
</B> <I><B>This is bold italic.</B></I><I>
This</I>**<I> is italic.</I> </P>
</DIV>
<P>In this scenario only the <A HREF="/workshop/author/dhtml/reference/objects/BODY.asp">BODY</A> element and the <A HREF="/workshop/author/dhtml/reference/objects/HTML.asp">HTML</A> element appear in the context as it precedes the selected fragment. Note that start tags and end tags are included in the context. The selection, delimited by StartSelection and EndSelection, is shown in bold. This is what will be on the clipboard (note this is MSHTML's
interpretation).</P>

<PRE CLASS="clsCode">
Version:0.9
StartHTML:71
EndHTML:160
StartFragment:130
EndFragment:150
<B>StartSelection:130</B>
<B>EndSelection:150</B>
<!DOCTYPE ...>
<HTML>
<BODY>
<!--StartFragment-->
<B><B>bold.</B> <I><B>This is bold italic.</B></I> <I>This</I> </B>
<!--EndFragment-->
</BODY>
</HTML>
</PRE>


<H3>Fragment of a Table in HTML</H3>

<P>HTML text:</P>
<PRE CLASS="clsCode">
<BODY><TABLE BORDER><TR><TH
ROWSPAN=2>Head1</TH><TD>Item 1</TD>
<TD>Item 2</TD> <TD>Item 3</TD>
<TD>Item 4</TD></TR><TR><TD>Item
5</TD> <TD>Item 6</TD> <TD>Item
7</TD> <TD>Item
8</TD></TR><TR><TH>Head2</TH><TD>Item
9</TD> <TD>Item 10</TD> <TD>Item
11</TD> <TD>Item
12</TD></TR></TABLE></BODY>
</PRE>
<P>This appears as:</P>
<DIV Style='text-indent:"1cm"'>
<TABLE CLASS="clsStd">
<TR><TH ROWSPAN=2>Head1</TH><TD>Item 1</TD> <TD>Item 2</TD> <TD>Item 3</TD> <TD>Item 4</TD></TR>
<TR><TD>Item 5</TD> <TD>Item 6</TD> <TD>Item 7</TD> <TD>Item 8</TD></TR>
<TR><TH>Head2</TH><TD>Item 9</TD> <TD>Item 10</TD> <TD>Item 11</TD> <TD>Item 12</TD></TR>
</TABLE>
</DIV>
<P>The Item 6, Item 7, Item 10, and Item 11 elements of the table are selected as a block and copied to the clipboard. The following is an MSHTML interpretation of what will be on the clipboard. The selection is shown in bold.</P>
<PRE CLASS="clsCode">
<!DOCTYPE ...>
<HTML><BODY><TABLE BORDER>
<!--StartFragment-->
<B><TR><TD>Item 6</TD>
<TD>Item 7</TD></TR><TR><TD>Item
10</TD> <TD>Item 11</TD></TR></B>
<!--EndFragment-->
</TABLE>
</BODY></HTML>
</PRE>

<H3>Pasting a Fragment of an Ordered List into Plain Text</H3>

<P>HTML text:</P>
<PRE CLASS="clsCode">
<BODY><OL TYPE = a><LI>Item 1<LI>Item
2<LI>Item 3<LI>Item 4<LI>Item 5<LI>Item
6</OL></BODY>
</PRE>
<P>This appears as:</P>
<OL>
<LI Style='text-indent:"1cm"'>Item 1
<LI Style='text-indent:"1cm"'>Item 2
<LI Style='text-indent:"1cm"'>Item 3
<LI Style='text-indent:"1cm"'>Item 4
<LI Style='text-indent:"1cm"'>Item 5
<LI Style='text-indent:"1cm"'>Item 6
</OL>
<P>The user selects and copies items 3 through 5 to the clipboard.
The following HTML is in the clipboard. The user's selection is shown in bold.</P>
<PRE CLASS="clsCode">
<DOCTYPE...><HTML><BODY><OL TYPE = a>
<!-- StartFragment-->
<B><LI>Item 3<LI>Item 4<LI>Item 5</B>
<!-- EndFragment-->
</OL></BODY></HTML>
</PRE>

<P>If this fragment is now pasted into an empty document, the
following HTML will be created:</P>
<PRE CLASS="clsCode">
<BODY><OL TYPE = a><LI>Item 3<LI>Item4<LI>Item 5</OL></BODY>
</PRE>
<P>This appears as:</P>
<UL>
<OL>
<LI Style='text-indent:"1cm"'>Item 3
<LI Style='text-indent:"1cm"'>Item 4
<LI Style='text-indent:"1cm"'>Item 5
</OL>
</UL>
<H3>Pasting a Partially Selected Region</H3>

<P>HTML text:</P>
<PRE CLASS="clsCode">
<P> MSHTML is a WYSIWYG Editor that supports:</P>
<UL><LI>Cut<LI>Copy<LI>Paste</UL> <P>This is a great tool!</P>
</PRE>
<P>This appears as:</P>
<DIV Style='text-indent:"1cm"'>
<P>MSHTML is a WYSIWYG Editor that supports:</P>
<UL>
<LI Style='text-indent:"1cm"'>Cut
<LI Style='text-indent:"1cm"'>Copy
<LI Style='text-indent:"1cm"'>Paste
</UL>
</DIV>
<DIV Style='text-indent:"1cm"'>
<P>This is a great tool!</P>
</DIV>
<P>The user selects from "WYSIWYG" to "Cop". The following HTML is in the clipboard. The user's selection is bold.</P>
<PRE CLASS="clsCode">
<DOCTYPE...><HTML><BODY>
<!-- StartFragment-->
<B><P>WYSIWYG Editor that supports:</P>
<UL><LI>Cut<LI>Cop</UL></B>
<!-- EndFragment-->
</BODY></HTML>
</PRE>

<P>The user selects from "opy" to "great". The following HTML is in the clipboard. The user's selection is bold.</P>
<PRE CLASS="clsCode">
<DOCTYPE...><HTML><BODY>
<!-- StartFragment-->
<UL><LI>
<B>opy<LI>Paste</UL><P> This is a great</B>
</P>
<!-- EndFragment-->
</BODY></HTML>
</PRE>
<!-- CONTENTS_END -->
</DIV>
<!-- START_PAGE_FOOTER -->
<SCRIPT LANGUAGE="JavaScript1.2" DEFER SRC="/workshop/code/common.js"></SCRIPT>
<DIV CLASS="clsFooter">
<HR SIZE="1" /><BR />
<P ID="pBackTop"><A HREF="#top" CLASS="clsBackTop"><IMG SRC="/msdn-online/shared/graphics/top.gif" WIDTH="18" HEIGHT="8" BORDER="0" ALT="Back to top">Back to top</A></P>
<P><I>Did you find this topic useful? Suggestions for other topics? <A HREF="/siteguide/write-us.asp?author=InetSDK&area=Networking%2C+Protocols+%26amp%3B+Data+Formats" TARGET="_top">Write us!</A></I></P>
<P>© <A CLASS="clsIncCpyRt" HREF="/isapi/gomscom.asp?TARGET=/misc/cpyright.htm" TARGET="_top">1999 Microsoft Corporation. All rights reserved. Terms of use</A>.</P>
<P> </P>
</DIV>

<SCRIPT LANGUAGE="JavaScript"><!--

function CheckToTop()
{
var eBody = document.body;
if (eBody.scrollHeight > eBody.offsetHeight) pBackTop.style.display = 'block';
}

//--></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="/msdn-online/shared/js/navlinks.js"></SCRIPT>
<!-- END_PAGE_FOOTER -->

</BODY>
</HTML>
 
多人接受答案了。
 
其实各位说的都不对,这样者能得到纯文本内容,丢到了HTML的格式,最后,我自己解决了
 
能否告诉我如何粘贴网页中的表格?我现在只能处理纯文本,表格标记都没有了,
显示内容一团糟。
 
后退
顶部