delphi - 在 Delphi 中使用 RichEdit 中的表格

标签 delphi richedit

我正在尝试在 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 版的一个未记录的功能,并且 EM_INSERTTABLE 消息自 Windows XP SP2 起就可用。有关哪些版本可用的详细信息,请参阅此处 3 .

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

有关 EM_INSERTTABLE 消息及其支持结构的详细信息,请参阅 MSDN 文档 4 (其中声明它们是随 Windows 8 一起引入的,但显然早于至少两个主要操作系统版本)。另请注意,自从 Murray 撰写博客 2 以来,结构的定义已经发生了一些变化。 2008年。我搜索了互联网的两端,但找不到与RichEdit 4.1一起使用的MS richedit.h版本,并且包括当时存在的“未记录”结构TABLEROWPARMS和TABLECELLPARMS,因此我仅限于MSDN Win8 的文档 4和默里的博客 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 (2005 SDK) 中 CHARFORMAT2A 和 CHARFORMAT2W 的定义在 C 部分有错误”,并且他们需要插入一个新的虚拟值字段来“修复”结构的字节对齐,以使其在 Delphi 中正常工作。我感觉 TABLEROWPARMS 和 TABLECELLPARMS 结构之一或两者可能存在类似的对齐问题,从而导致此错误。

我需要一些帮助来弄清楚为什么 SendMessage 返回 E_INVALIDARG,以及我可以采取哪些措施来修复它。任何帮助将不胜感激!
-杰夫·艾勒

引用网站:
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

最佳答案

这是正确的结构...

  _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;

关于delphi - 在 Delphi 中使用 RichEdit 中的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22689517/

相关文章:

Delphi - 当单元名称与属性名称相同时如何解决冲突?

delphi - 如何使用 dcc32/dcc64 将代码编译为 ASM 文件?

c++ - 如何插入不带下划线的超链接到 Win32 RichEdit?

.net - 是否有适用于 VB6 的好的 TDD 工具或资源?

delphi - 将 Richedit 行中的前 4 个字符设为粗体

delphi - 如何像delphi中的矩阵一样迭代DBGrid

arrays - Delphi 中两个应用程序之间共享数据数组

delphi - 如何检测 vcl 样式何时更改?

c# - 在执行语法突出显示时暂时禁用 C# Rich Edit 撤消缓冲区

c++ - 如何消除 RICHEDIT 控件的 MessageBeep?