我正在尝试在Delphi XE2 Starter Edition的TRichEdit控件中使用表格。(换句话说,我没有XE2的源代码,但我有TurboDelphi的源代码)。我了解默认的RichEdit控件不使用支持表格的MS RichEdit版本,因此我对其进行了子类化,以使用MS RichEdit v4.1,如此处1和此处6所述,并且还参考了JEDI TjvRichEdit中的代码。(为了简洁起见,我没有包含决定除v4.1之外的DLL的RichEdit版本号的代码段,这是我从JEDI借鉴的。这里显示了一个简化版本。)
一篇 MSDN 博客2指出,支持 RTF 表格的 Windows 消息是 MS RichEdit 4.1 的一个未记录特性,并且自 Windows XP SP2 起,EM_INSERTTABLE 消息已经可用。有关每个版本的可用性的更多信息,请参见此处 3。
在这篇博客2之后的一条评论,由David Kinder于2008年9月26日发布,他表示他能够使用我下面展示的相同代码(除了他没有使用Delphi)来使EM_INSERTTABLE消息在RichEdit v4.1中运行。
有关EM_INSERTTABLE消息及其支持的结构的详细信息,请参阅MSDN文档4(该文档称它们是在Windows 8中引入的,但至少比那之前的两个主要操作系统版本早)。还要注意,自Murray在2008年撰写他的博客2以来,这些结构的定义已经有所改变。我已经搜索了整个互联网,找不到与RichEdit 4.1配套的MS richedit.h版本,其中包括当时存在的“未公开”结构TABLEROWPARMS和TABLECELLPARMS,因此我只能使用MSDN文档作为Win8 4和据称存在于Win XP和Win7的Murray博客2。
这是我的自定义 RichEdit:
unit MyRichEdit;
//Customized RichEdit to use MS RichEdit v4.1
// Some stuff borrowed from Michael Lam's REdit (ca. 1998), found on the Torry page.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
StdCtrls, ComCtrls, Printers, RichEdit;
type
{TableRowParms}
PTableRowParms = ^TTableRowParms;
_tableRowParms = packed record
cbRow : BYTE ; // Count of bytes in this structure
cbCell : BYTE ; // Count of bytes in TABLECELLPARMS
cCell : BYTE ; // Count of cells
cRow : BYTE ; // Count of rows
dxCellMargin : LONG ; // Cell left/right margin (\trgaph)
dxIndent : LONG ; // Row left (right if fRTL indent (similar to \trleft)
dyHeight : LONG ; // Row height (\trrh)
nAlignment{:3}: DWORD; // Row alignment (like PARAFORMAT::bAlignment, \trql, trqr, \trqc)
fRTL{:1} : DWORD; // Display cells in RTL order (\rtlrow)
fKeep{:1} : DWORD; // Keep row together (\trkeep}
fKeepFollow{:1} : DWORD; // Keep row on same page as following row (\trkeepfollow)
fWrap{:1} : DWORD; // Wrap text to right/left (depending on bAlignment) (see \tdfrmtxtLeftN, \tdfrmtxtRightN)
fIdentCells{:1} : DWORD; // lparam points at single struct valid for all cells
//cpStartRow : LONG ; // not in Murray's blog version, so commented here...
//bTableLevel : BYTE; // not in Murray's blog version
//iCell : BYTE; // not in Murray's blog version
end;
TABLEROWPARMS = _tableRowParms;
TTableRowParms = TABLEROWPARMS;
{TableCellParms}
PTableCellParms = ^TTableCellParms;
_tableCellParms = packed record
dxWidth : LONG ; // Cell width (\cellx)
nVertAlign{:2} : WORD ; // Vertical alignment (0/1/2 = top/center/bottom \clvertalt (def), \clvertalc, \clvertalb)
fMergeTop{:1} : WORD ; // Top cell for vertical merge (\clvmgf)
fMergePrev{:1} : WORD ; // Merge with cell above (\clvmrg)
fVertical{:1} : WORD ; // Display text top to bottom, right to left (\cltxtbrlv)
wShading : WORD ; // Shading in .01% (\clshdng) e.g., 10000 flips fore/back
dxBrdrLeft : SHORT ; // Left border width (\clbrdrl\brdrwN) (in twips)
dyBrdrTop : SHORT ; // Top border width (\clbrdrt\brdrwN)
dxBrdrRight : SHORT ; // Right border width (\clbrdrr\brdrwN)
dyBrdrBottom : SHORT ; // Bottom border width (\clbrdrb\brdrwN)
crBrdrLeft : COLORREF; // Left border color (\clbrdrl\brdrcf)
crBrdrTop : COLORREF; // Top border color (\clbrdrt\brdrcf)
crBrdrRight : COLORREF; // Right border color (\clbrdrr\brdrcf)
crBrdrBottom : COLORREF; // Bottom border color (\clbrdrb\brdrcf)
crBackPat : COLORREF; // Background color (\clcbpat)
crForePat : COLORREF; // Foreground color (\clcfpat)
end;
TABLECELLPARMS = _tableCellParms;
TTableCellParms = TABLECELLPARMS;
TMyRichEdit = class(ComCtrls.TRichEdit)
private
function GetRTF: string; // get the RTF string
procedure SetRTF(InRTF: string); // set the RTF string
protected
procedure CreateParams(var Params: TCreateParams); override;
published
property RTFText: string read GetRTF write SetRTF;
end;
//--------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------
var
RichEditVersion : Integer; //Version of the MS Windows RichEdit DLL
const
RichEdit10ModuleName = 'RICHED32.DLL';
RichEdit20ModuleName = 'RICHED20.DLL';
RichEdit41ModuleName = 'MSFTEDIT.DLL';
MSFTEDIT_CLASS = 'RichEdit50W'; //goes with RichEdit 4.1 (beginning with Win XP SP2)
EM_INSERTTABLE = WM_USER + 232;
implementation
function TMyRichEdit.GetRTF: string;
var FStream : TStringStream;
begin
// get the RTF string
FStream := TStringStream.Create; // RTF stream
FStream.Clear;
FStream.Position := 0;
Lines.SaveToStream(FStream);
Result := FStream.DataString;
FStream.Free; // free the RTF stream
end; //ok
procedure TMyRichEdit.SetRTF(InRTF: string);
var FStream : TStringStream;
begin
// set the RTF string
// LoadFromStream uses an EM_STREAMIN windows msg, which by default REPLACES the contents of a RichEdit.
FStream := TStringStream.Create; // RTF stream
FStream.Clear;
FStream.Position := 0;
FStream.WriteString(InRTF);
FStream.Position := 0;
Lines.LoadFromStream(FStream);
Self.Modified := false;
FStream.Free; // free the RTF stream
end; //ok
//===========================================================================
//Defaults: RICHEDIT_CLASS = 'RichEdit20W'; RICHEDIT_CLASS10A = 'RICHEDIT';
//It needs to use RichEdit50W for version 4.1, which I defined in a constant above as MSFTEDIT_CLASS.
procedure TMyRichEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
If RichEditVersion = 1 then CreateSubClass(Params, RICHEDIT_CLASS10A)
else If RichEditVersion = 4 then CreateSubClass(Params, MSFTEDIT_CLASS)
else CreateSubClass(Params, RICHEDIT_CLASS);
end;
//================================================================
{Initialization Stuff}
//================================================================
var
GLibHandle: THandle = 0;
procedure InitRichEditDll;
begin
//Try to load MS RichEdit v 4.1 into memory...
RichEditVersion := 4;
GLibHandle := SafeLoadLibrary(RichEdit41ModuleName);
if (GLibHandle > 0) and (GLibHandle < HINSTANCE_ERROR) then
GLibHandle := 0; //this means it could not find the DLL or it didn't load right.
if GLibHandle = 0 then begin
RichEditVersion := 2;
GLibHandle := SafeLoadLibrary(RichEdit20ModuleName);
if (GLibHandle > 0) and (GLibHandle < HINSTANCE_ERROR) then
GLibHandle := 0;
if GLibHandle = 0 then begin
RichEditVersion := 1;
GLibHandle := SafeLoadLibrary(RichEdit10ModuleName);
if (GLibHandle > 0) and (GLibHandle < HINSTANCE_ERROR) then begin
RichEditVersion := 0;
GLibHandle := 0;
end;
end;
end;
end;
procedure FinalRichEditDll;
begin
if GLibHandle > 0 then
begin
FreeLibrary(GLibHandle);
GLibHandle := 0;
end;
end;
initialization
InitRichEditDll;
finalization
FinalRichEditDll;
End.
使用方法:
Uses … MyRichEdit …
type
TRichEdit = class(TMyRichEdit);
TfrmEdit = class(TForm)
…
memNotes: TRichEdit;
…
end;
procedure TfrmEdit.actTableAddExecute(Sender: TObject);
var
rows: TABLEROWPARMS;
cells: TABLECELLPARMS;
rc : LRESULT;
begin
//Insert a table into the RTF.
ZeroMemory(@rows,sizeof(rows));
rows.cbRow := sizeof(TABLEROWPARMS);
rows.cbCell := sizeof(TABLECELLPARMS);
rows.cCell := 3;
rows.cRow := 2;
rows.dxCellMargin := 5; //50
rows.nAlignment := 1;
rows.dyHeight := 100; //400
rows.fIdentCells := 1;
rows.fRTL := 0;
rows.fKeep := 1;
rows.fKeepFollow := 1;
rows.fWrap := 1;
//rows.cpStartRow := -1;
ZeroMemory(@cells,sizeof(cells));
cells.dxWidth := 600; //1000
cells.dxBrdrLeft := 1;
cells.dyBrdrTop := 1;
cells.dxBrdrRight := 1;
cells.dyBrdrBottom := 1;
cells.crBackPat := RGB(255,255,255);
cells.crForePat := RGB(0,0,0);
cells.nVertAlign := 0;
//cells.fMergeTop := 1;
//cells.fMergePrev := 1;
cells.fVertical := 1;
rc := SendMessage(memNotes.Handle,EM_INSERTTABLE, WPARAM(@rows),LPARAM(@cells));
//rc := memNotes.Perform(EM_INSERTTABLE, WPARAM(@rows),LPARAM(@cells));
end;
在执行时,rc包含-2147024809(
E_INVALIDARG
)。我不太理解为什么会失败,或者消息参数存在什么问题。声明一下,我是新手,正在使用Delphi中的RichEdit,但在求助之前,我尽可能地学习了很多。在我的广泛搜索中,我发现了这个网站5,它可能有助于缩小问题范围。该网站托管了一个名为RTFLabel的实用程序。下载zip文件并查看richedit2.pas,在那里他们解释说,“richedit.h(2005 SDK)中CHARFORMAT2A和CHARFORMAT2W的定义在C部分中存在错误”,并且他们需要插入一个新的虚拟字段以“修复”结构体的字节对齐,以便使其与Delphi正常工作。我感觉TABLEROWPARMS和TABLECELLPARMS结构中可能存在类似的对齐问题,导致此错误。
我希望能得到帮助,找出为什么SendMessage
返回E_INVALIDARG错误,并了解我可以采取哪些措施来解决。非常感谢您的帮助!
-Jeff Aylor
1 [http://fgaillard.com/2010/09/using-richedit-4-1-with-d2010/]1
2 [http://blogs.msdn.com/b/murrays/archive/2008/09/15/richedit-s-nested-table-facility.aspx]2
3 [http://blogs.msdn.com/b/murrays/archive/2006/10/14/richedit-versions.aspx]3
4 [http://msdn.microsoft.com/en-us/library/windows/desktop/hh768373%28v=vs.85%29.aspx]4
5 [http://flocke.vssd.de/prog/code/pascal/rtflabel/]5
6 [Delphi 7 TRichTextEdit Text in a box not displaying correctly6
packed
关键字在这两个记录(结构体)定义中有特定的原因吗?我在 API 文档中没有看到它们应该被打包的任何迹象。 - Ken White