在Delphi中使用RichEdit中的表

时间:2014-03-27 13:45:10

标签: delphi richedit

我试图在Delphi XE2 Starter Edition中的TRichEdit控件中使用Tables。 (换句话说,我没有XE2的源代码 - 但我确实有TurboDelphi的源代码)。我知道默认的RichEdit控件不使用支持表的MS RichEdit版本,因此我将其子类化为使用MS RichEdit v4.1,如此处所述1和此处6,并且还建模关闭JEDI TjvRichEdit中的代码。 (为了简洁起见,我没有包含确定我从JEDI借来的除v4.1之外的DLL的RichEdit版本号的代码段。这里显示的是简化版本。)

MSDN博客2声明支持RTF表的Windows消息是MS RichEdit 4.1版的未记录功能,并且自Windows XP SP2起EM_INSERTTABLE消息可用。有关何时可用版本的详细信息,请参阅此处3

2008年9月26日由David Kinder发布的此博客2之后发表的评论指出,他能够使用我在下面显示的相同代码获取EM_INSERTTABLE消息以使用RichEdit v4.1(除了他没有使用Delphi)。

有关EM_INSERTTABLE消息及其支持结构的详细信息,请参阅MSDN文档4(其中说明了它们是在Windows 8中引入的,但显然至少在两个主要操作系统版本之前已经过时) 。另请注意,自2008年Murray撰写博客2以来,结构的定义有所改变。我搜索到了互联网的末端,找不到与RichEdit 4.1一起使用的MS richedit.h版本。并包含当时存在的“无证”结构TABLEROWPARMS和TABLECELLPARMS,因此我仅限于MS8文档,因为它们存在于Win8 4和Murray的博客2,因为它们据称存在于Win XP中和Win7。

这是我的自定义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中的CHARFORMAT2A和CHARFORMAT2W的定义(2005 SDK)在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 correctly 6

2 个答案:

答案 0 :(得分:1)

tl; dr

不要将它用于除广告/记录之外的任何其他内容(即低于W8),即使这样我也会睁大眼睛。这是一团糟。


请注意,您收集记录的博客文章与文档之间存在其他差异,而TABLEROWPARMS的其他三个字段除外。这是TABLECELLPARMS。这里是一个并排比较,左边是博客文章的记录,右边是文档记录。

typedef struct _tableCellParms {           typedef struct _tableCellParms {
    LONG    dxWidth;                         LONG     dxWidth;
    WORD    nVertAlign:2;                    WORD     nVertAlign:2;
    WORD    fMergeTop:1;                     WORD     fMergeTop:1;
    WORD    fMergePrev:1;                    WORD     fMergePrev:1;
    WORD    fVertical:1;                     WORD     fVertical:1;
                                             WORD     fMergeStart:1;
                                             WORD     fMergeCont:1;
    WORD    wShading;                        WORD     wShading;
    SHORT   dxBrdrLeft;                      SHORT    dxBrdrLeft;
    SHORT   dyBrdrTop;                       SHORT    dyBrdrTop;
    SHORT   dxBrdrRight;                     SHORT    dxBrdrRight;
    SHORT   dyBrdrBottom;                    SHORT    dyBrdrBottom;
    COLORREF crBrdrLeft;                     COLORREF crBrdrLeft;
    COLORREF crBrdrTop;                      COLORREF crBrdrTop;
    COLORREF crBrdrRight;                    COLORREF crBrdrRight;
    COLORREF crBrdrBottom;                   COLORREF crBrdrBottom;
    COLORREF crBackPat;                      COLORREF crBackPat;
    COLORREF crForePat;                      COLORREF crForePat;
} TABLECELLPARMS;                          } TABLECELLPARMS;


在这里,您可以直观地在记录中间看到另外两个字段。或者,其中一个是错的,也许两者都是......但是我们从博客文章中知道,有些人能够使其与左边的那个一起工作。也许有针对特定dll的特定版本。

无论如何,考虑到E_INVALIDARG的原因非常有可能是结构的大小,因为即使进行了最小的测试它也无法工作,这导致我使用强力测试来确定正确记录大小。

var
   rows: TABLEROWPARMS;
   cells: TABLECELLPARMS;
begin
   ZeroMemory(@rows,sizeof(rows));
   rows.cbRow := 1;
   rows.cbCell := 1;
   rows.cCell := 1;
   rows.cRow := 1;

   ZeroMemory(@cells,sizeof(cells));

   while SendMessage(RichEdit1.Handle, EM_INSERTTABLE, 
                     WPARAM(@rows), LPARAM(@cells)) <> S_OK do begin
     if rows.cbCell < 120 then  // arbitrary upper limit 
       Inc(rows.cbCell)
     else begin
       Inc(rows.cbRow);
       rows.cbCell := 1;
     end;
     if rows.cbRow = 120 then
      raise Exception.Create('no match');
   end;
end;

在程序结束时设置断点,rows.cbRow出现'28',rows.cbCell出现'40'。它们比两个引用都小。我还测试了一个比第一个命中更大的可能匹配,没有。我的测试是针对'msftedit.dll'版本5.41.21.2510,使用XE2驻留在W7框的'\ syswow64'中。

那么我们应该从哪里切割结构?正如我们从上面的参考文献中看到的,可能不是。我没有看到任何声音从这里前进的方法,但我发布了我最好的尝试,以防你有类似的环境,并希望继续(我不建议 - 注意我必须改变字段的顺序到到目前为止)。这肯定是不正确的,因为它无法插入具有多个列的表格。

  _tableRowParms = packed record
     cbRow         : BYTE ;
     cbCell        : BYTE ;
     cCell         : BYTE ;
     cRow          : BYTE ;
     dxCellMarginOrVertAlign  : LONG ;  // when there's more than one cell
     dxIndent      : LONG ;
     dyHeight      : LONG ;
     nAlignment    : DWORD;
     fRTL          : DWORD;
     fKeep         : DWORD;
  end;

  _tableCellParms = packed record
     dxWidth        : LONG    ;
     nVertAlign     : WORD    ;
     fVertical      : WORD    ;
     dxBrdrLeft     : SHORT   ;
     dyBrdrTop      : SHORT   ;
     dxBrdrRight    : SHORT   ;
     dyBrdrBottom   : SHORT   ;
     crBrdrLeft     : COLORREF;
     crBrdrTop      : COLORREF;
     crBrdrRight    : COLORREF;
     crBrdrBottom   : COLORREF;
     crBackPat      : COLORREF;
     crForePat      : COLORREF;
  end;

..

var
   rows: TABLEROWPARMS;
   cells: TABLECELLPARMS;
   rc : LRESULT;
begin
   ZeroMemory(@rows,sizeof(rows));

   rows.cbRow := sizeof(TABLEROWPARMS);
   rows.cbCell := sizeof(TABLECELLPARMS);
   rows.cCell := 1;                // ??
   rows.cRow := 3;
   rows.dxCellMarginOrVertAlign := 120;
   rows.dxIndent := 200;
   rows.dyHeight := 400;
   rows.nAlignment := 1;                    // don't leave at 0
   rows.fRTL := 0;                          // ???
   rows.fKeep := 0;                         // ???

   ZeroMemory(@cells,sizeof(cells));

   cells.dxWidth := 1000;
   cells.nVertAlign := 1;
   cells.fVertical := 1;
   cells.dxBrdrLeft := 50;
   cells.dyBrdrTop := 10;
   cells.dxBrdrRight := 50;
   cells.dyBrdrBottom := 20;
   cells.crBrdrLeft := RGB(255,0,0);
   cells.crBrdrTop := RGB(0, 255, 0);
   cells.crBrdrRight := RGB(0, 0, 255);
   cells.crBrdrBottom := RGB(255, 255, 0);
   cells.crBackPat := RGB(255, 255, 255);
   cells.crForePat := RGB(128, 64, 64);     // ?

   rc := SendMessage(RichEdit1.Handle,EM_INSERTTABLE, WPARAM(@rows),LPARAM(@cells));
end;

答案 1 :(得分:1)

这里是正确的结构......

  _tableRowParms = 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    : LONGINT;     // Cell left/right margin (\trgaph)
     dxIndent        : LONGINT;     // Row left (right if fRTL indent (similar to \trleft)
     dyHeight        : LONGINT;     // Row height (\trrh)
     nParams         : DWORD;       // 0 - 2 bits - Row alignment (like PARAFORMAT::bAlignment, 1/2/3) (\trql, trqr, \trqc)
                                    // 3 bit - Display cells in RTL order (\rtlrow)
                                    // 4 bit - Keep row together (\trkeep}
                                    // 5 bit - Keep row on same page as following row (\trkeepfollow)
                                    // 6 bit - Wrap text to right/left (depending on bAlignment) (see \tdfrmtxtLeftN, \tdfrmtxtRightN)
                                    // 7 bit - lparam points at single struct valid for all cells
     cpStartRow      : LONGINT;     // The character position that indicates where to insert table. A value of –1 indicates the character position of the selection. 
     bTableLevel     : BYTE;        // The table nesting level (EM_GETTABLEPARMS only).
     iCell           : BYTE;        // The index of the cell to insert or delete (EM_SETTABLEPARMS only).
  end;
  TABLEROWPARMS  = _tableRowParms;
  TTableRowParms = TABLEROWPARMS;
  PTableRowParms = ^TTableRowParms;


  _tableCellParms = record
     dxWidth         : LONGINT;     // Cell width (\cellx)
     nParams         : Word;        // 0 - 1 bits - Vertical alignment (0/1/2 = top/center/bottom) (\clvertalt (def), \clvertalc, \clvertalb)
                                    // 2 bit - Top cell for vertical merge (\clvmgf)
                                    // 3 bit - Merge with cell above (\clvmrg)
                                    // 4 bit - Display text top to bottom, right to left (\cltxtbrlv)
                                    // 5 bit - Start set of horizontally merged cells (\clmgf).
                                    // 6 bit - Merge with the previous cell (\clmrg).      
     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;
  PTableCellParms = ^TTableCellParms;