zoukankan      html  css  js  c++  java
  • delphi使用RichView控件 表格大小

    TRichView表格大小

    介绍

    表格布局算法

    表格没有明确指定的列宽和行高。

    宽度的计算基于:

    • 在单元格内显示文档所需的最小宽度(如果 rvtoIgnoreContentWidth 不在 table.Options 中)

    • table.BestWidth

    • cells.BestWidth

    高度的计算基于:

    • 单元格内容的高度(如果 rvtoIgnoreContentHeight 不在 table.Options 中)

    • cells.BestHeight

    计算表格宽度的规则:

    1. 单元格的宽度总是足够大以包含它们最宽的项目(如果 rvtoIgnoreContentWidth 不在 table.Options 中)

    2. 如果 table.BestWidth<>0table.BestWidth定义表格的宽度(但表格可以更宽,因为规则1)

    3. 如果 table.BestWidth=0 那么

      如果所有列都具有在 TRVStyleUnits 中定义的宽度(至少有一个具有在 TRVStyleUnits 中定义的单元格的 BestWidth (单位是像素或缇),并且没有以百分比定义的单元格的 BestWidth)(即以像素或缇为单位的 cell.BestWidth,并且没有具有以 % 为单位的 cell.BestWidth),表格的宽度是根据单元格 BestWidths 和显示单元格内容所需的最小宽度计算而不会重叠(在这种情况下 鼠标调整大小与 MS Word 最相似)

      否则,表格的宽度与 BestWidth=-100 (100%) 相同(RichView 无法根据单元格的最佳宽度计算表格的宽度)

    计算单元格宽度的规则非常复杂。

    最重要的规则(当单元格的宽度冲突时):

    • 左侧单元格的宽度比右侧单元格的宽度具有更高的优先级。

    • 以 百分比(%) 为单位的单元格宽度比以像素/缇为单位的宽度具有更高的优先级。

    用鼠标调整大小

    只能在编辑器中使用鼠标调整大小。

    如果在 table.Options 中包含 rvtoRowSizingrvtoColSizing,用户将无法调整它的大小。

    当用户用鼠标调整表格大小时,实际上改变了单元格的 BestWidthBestHeight 属性的值。

    当用鼠标调整列大小时,左右列单元格的BestWidth没有以百分比(%) 为单位,RichViewEdit 调整左右列单元格的BestWidth,试图保持它们的总宽度不变。

    按下 Shift 键调整大小时,RichView 仅设置左侧列的 BestWidths

    表格宽度的计算

    cell.BestWidth(以像素为单位)不包括单元格填充和边框宽度。
    因此,当单元格宽度由 BestWidth 设置时,其全宽为:

    Full-Cell-Width = (CellBorderWidth + CellHPadding) * 2 + BestWidth

    完整的表格宽度为(为简单起见,让所有单元格具有相同的宽度,Full-Cell-Width:

    (BorderWidth + BorderHSpacing) * 2 + (Full-Cell-Width) * ColCount + CellHSpacing * (ColCount - 1)

    table.BestWidth 包括所有内容、所有单元格、间距和边框。

    属性

    表格首选宽度TRVTableItemInfo.BestWidth

    property BestWidth: TRVHTMLLength;
    

    该值以链接的 TRVStyle 组件的单位(像素pixels或缇twips)或百分比测量。

    如果需要不重叠地显示其内容,则表格可以比该值更宽。

    如果 BestWidth 未设置(BestWidth=0),RichView 会尝试根据其单元格计算表格宽度。RichView 仅在所有列都具有在 TRVStyleUnits 中定义的宽度(至少有一个具有在 TRVStyleUnits 中定义的单元格的BestWidth,并且没有以百分比定义的单元格的BestWidth)时才能执行此操作。 否则 BestWidth=0 就像 BestWidth=-100 (100%) 一样。

    此属性对应于 HTML的table width属性。

    默认值 0

    单元格首选宽度TRVTableCellData.BestWidth

    property BestWidth: TRVHTMLLength;
    

    该值以链接的 TRVStyle 组件的单位(像素pixels或缇twips)或百分比测量。

    此属性对应于 HTML的td width属性。

    用户无法撤消/重做对此属性的直接分配,请使用 table.SetCellBestWidth

    如果需要不重叠地显示其内容,则单元格实际宽度可以比此值更宽。 单元格总是有足够的宽度来显示,单元格的宽度 = 最宽的项目 + CellHPadding*2 + 段落缩进(FirstIndentLeftIndentRightIndent)(如果 rvtoIgnoreContentWidth 未包含在 table.Options 中)。

    如果table.BestWidth小于所有单元格宽度 cell.BestWidth的和,单元格的实际宽度cell.Width将小于cell.BestWidth

    默认值 0

    单元格首选高度TRVTableCellData.BestHeight

    property BestHeight: TRVStyleLength;
    

    单元格的最小高度。

    该值以屏幕像素pixels或缇twips为单位测量(打印时,根据打印机分辨率重新计算),具体取决于链接的 TRVStyle 组件的单位。

    此属性对应于 HTML的td height,但有区别。 BestHeight 不包括 CellVPadding,而 td height 包括垂直单元格填充。

    用户无法撤消/重做对此属性的直接分配,请使用 table.SetCellBestHeight

    如果需要显示其所有内容(如果 rvtoIgnoreContentHeight 未包含在 table.Options 中),则单元格可以更高。

    默认值 0

    方法

    设置单元格首选宽度TRVTableItemInfo.SetCellBestWidth

    procedure SetCellBestWidth(Value: Integer; Row,Col: Integer);
    

    Cells[Row, Col].BestWidth 赋值,作为编辑操作。

    与直接分配给 Cells[Row, Col].BestWidth 属性不同,如果为在 TRichViewEdit 中插入的表(插入后)调用此方法,则可以撤消/重做此方法。

    设置单元格首选高度TRVTableItemInfo.SetCellBestHeight

    procedure SetCellBestHeight(Value: TRVStyleLength; Row,Col: Integer);
    

    Cells[Row, Col].BestHeight 赋值,作为编辑操作。

    与直接分配给 Cells[Row, Col].BestHeight 属性不同,如果为在 TRichViewEdit 中插入的表(插入后)调用此方法,则可以撤消/重做此方法。

    获取表格宽度TRVTableItemInfo.GetWidth

    function GetWidth(ARVStyle: TRVStyle): Integer;
    

    参数

    ARVStyle 计算宽度的单位,将值从单位转换为像素

    表格格式化后取得的数据才准确

    获取表格高度TRVTableItemInfo.GetHeight

    function GetHeight(ARVStyle: TRVStyle): Integer;
    

    参数

    ARVStyle 计算宽度的单位,将值从单位转换为像素

    表格格式化后取得的数据才准确

    获取行高度TRVTableRow.GetHeight

    function GetHeight: Integer;
    

    以屏幕像素为单位返回行高。
    不能用于计算打印尺寸

    表格格式化后取得的数据才准确

    获取单元格宽度TRVTableCellData.GetWidth

    function GetWidth: Integer; 
    

    不包含单元格水平内边距table.CellHPadding

    表格格式化后取得的数据才准确

    获取单元格高度TRVTableCellData.GetHeight

    function GetHeight: Integer; 
    

    不包含单元格垂直内边距table.CellVPadding

    表格格式化后取得的数据才准确

    例子

    计算单元格宽度

    uses RVTable, RVItem;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Table: TRVTableItemInfo;
      Row: Integer;
    begin
      //创建4行3列的表格
      Table := TRVTableItemInfo.CreateEx(4, 3, RichViewEdit1.RVData);
      //设置表格和单元格边框样式为平面
      Table.BorderStyle := rvtbColor;
      Table.CellBorderStyle := rvtbColor;
      //设置表格和单元格边框、间距大小
      Table.BorderWidth := 1;
      Table.BorderVSpacing := 2;
      Table.BorderHSpacing := 2;
      Table.CellBorderWidth := 2;
      Table.CellVSpacing := 1;
      Table.CellHSpacing := 1;
    
      //设置表格的宽度(如果设置了表格宽度,再设置单元格的宽度就不起作用了,只能设置单元格的百分比)
      Table.BestWidth := 600;
      //设置单元格的占比(第一列20% 第二列30% 第三列50%)
      for Row := 0 to Table.RowCount - 1 do
        Table.Cells[Row, 0].BestWidth := -20;
      for Row := 0 to Table.RowCount - 1 do
        Table.Cells[Row, 1].BestWidth := -30;
      for Row := 0 to Table.RowCount - 1 do
        Table.Cells[Row, 2].BestWidth := -50;
    
      //将表格添加到文档中
      RichViewEdit1.InsertItem('', Table);
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      Item: TCustomRVItemInfo;
      Table: TRVTableItemInfo;
      Rve: TCustomRichViewEdit;
      Row, Col: Integer;
    begin
      //获取当前选择的表格
      if not RichViewEdit1.CanChange or
        not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, Rve, Item) then
        Exit;
      Table := TRVTableItemInfo(Item);
      //显示所选表格单元格的宽度
      for Row := 0 to Table.RowCount - 1 do
        for Col := 0 to Table.ColCount - 1 do
        begin
          Table.Cells[Row, Col].Clear;
          Table.Cells[Row, Col].AddFmt('BestWidth=%d', [Table.Cells[Row, Col].BestWidth], 0, 0);
          Table.Cells[Row, Col].AddFmt('CellHPadding=%d', [Table.CellHPadding], 0, 0);
          Table.Cells[Row, Col].AddFmt('CellBorderWidth=%d', [Table.CellBorderWidth], 0, 0);
          Table.Cells[Row, Col].AddFmt('Width=%d', [Table.Cells[Row, Col].Width], 0, 0);
        end;
      //添加说明
      Rve.AddNL('单元格宽度Cells.Width = Cells.BestWidth + Table.CellHPadding * 2', 0, 0);
      Rve.AddNL('单元格实际宽度 = Cells.BestWidth + (Table.CellBorderWidth + Table.CellHPadding) * 2', 0, 0);
      Rve.AddNL('表格宽度Table.BestWidth = 所有单元格实际宽度 + ' +
        '(Table.BorderWidth + Table.BorderHSpacing) * 2 + CellHSpacing * (ColCount - 1)', 0, 0);
      //格式化文档
      Rve.Format;
    end;
    

    计算表格高度

    uses RVTable, RVItem;
    
    procedure TForm1.Button3Click(Sender: TObject);
    var
      Table: TRVTableItemInfo;
      Col: Integer;
    begin
      //创建3行4列的表格
      Table := TRVTableItemInfo.CreateEx(3, 4, RichViewEdit1.RVData);
      //设置表格和单元格边框样式为平面
      Table.BorderStyle := rvtbColor;
      Table.CellBorderStyle := rvtbColor;
      //设置表格和单元格边框、间距大小
      Table.BorderWidth := 1;
      Table.BorderVSpacing := 2;
      Table.BorderHSpacing := 2;
      Table.CellBorderWidth := 2;
    
      //设置表格的宽度(如果设置了表格宽度,不设置单元格的宽度,会平均分在每个列上)
      Table.BestWidth := 600;
    
      //设置单元格的高度(要超过后期填入内容的高度,否则会影响高度计算)
      for Col := 0 to Table.ColCount - 1 do
        Table.Cells[0, Col].BestHeight := 40;
      for Col := 0 to Table.RowCount - 1 do
        Table.Cells[1, Col].BestHeight := 50;
      for Col := 0 to Table.RowCount - 1 do
        Table.Cells[2, Col].BestHeight := 60;
    
      //将表格添加到文档中
      RichViewEdit1.InsertItem('', Table);
    end;
    
    procedure TForm1.Button4Click(Sender: TObject);
    var
      Item: TCustomRVItemInfo;
      Table: TRVTableItemInfo;
      Rve: TCustomRichViewEdit;
      Row, Col: Integer;
    begin
      //获取当前选择的表格
      if not RichViewEdit1.CanChange or
        not RichViewEdit1.GetCurrentItemEx(TRVTableItemInfo, Rve, Item) then
        Exit;
      Table := TRVTableItemInfo(Item);
      //显示所选表格单元格的宽度
      for Row := 0 to Table.RowCount - 1 do
        for Col := 0 to Table.ColCount - 1 do
        begin
          Table.Cells[Row, Col].Clear;
          Table.Cells[Row, Col].AddFmt('BestHeight=%d', [Table.Cells[Row, Col].BestHeight], 0, 0);
          Table.Cells[Row, Col].AddFmt('Height=%d', [Table.Cells[Row, Col].Height], 0, 0);
        end;
      //添加说明
      Rve.AddNL('单元格高度Cells.Height = Cells.BestHeight + Table.CellVPadding', 0, 0);
      Rve.AddNL('单元格实际高度 = Cells.BestHeight + (Table.CellBorderWidth + Table.CellBorderWidth) * 2', 0, 0);
      Rve.AddNL('表格高度 = 所有单元格实际高度 + ' +
        '(Table.BorderWidth + Table.BorderVSpacing) * 2 + CellVSpacing * (RowCount - 1)', 0, 0);
      //格式化文档
      Rve.Format;
    end;
    
  • 相关阅读:
    ASCII码
    cron表达式学习
    mysql学习二、SQL常用数据类型
    mysql学习一 常用语句
    python学习
    搬砖
    新接触Linux 命令
    搬砖
    python encode decode
    201521123071 《JAVA程序设计》第十二周学习总结
  • 原文地址:https://www.cnblogs.com/txgh/p/15414676.html
Copyright © 2011-2022 走看看