回复 1楼UpLin的帖子
非常抱歉让您久等了。以下是测试代码,主要是通过计算当前单元格最上部文字位置及最下部文字位置和单元格的上下文字边距来实现的:
- private void addTableToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.textControl1.Tables.Add(10,10,11);
- }
- private void getRowHeightToolStripMenuItem_Click(object sender, EventArgs e)
- {
- TXTextControl.Table tb = this.textControl1.Tables.GetItem(11);
- foreach (TXTextControl.TableCell cell in tb.Cells)
- {
- int height = getTextHeight(cell, this.textControl1);
- }
- }
- /// <summary>
- /// Iterates over all cells within the row and search the highest cell.
- /// </summary>
- /// <param name="cell"></param>
- /// <param name="table">Contains the cell</param>
- /// <param name="textControl">Contains the table and the cell</param>
- /// <returns>Height of the row</returns>
- int getHeight(TXTextControl.TableCell cell, TXTextControl.Table table, TXTextControl.TextControl textControl)
- {
- int absoluteHeight = -1;
- for (int i = 1; i <= table.Columns.Count; i++)
- {
- int relativeCellHeight = getTextHeight(table.Cells.GetItem(cell.Row, i), textControl);
- if (relativeCellHeight > absoluteHeight)
- {
- absoluteHeight = relativeCellHeight;
- }
- }
- return absoluteHeight;
- }
- /// <summary>
- /// Calculates the height with help of the bounds of the first and last character of the TableCell.
- /// The TextDistances are considered.
- /// </summary>
- /// <param name="cell">Contains the text</param>
- /// <param name="textControl">Required to get the TextChar</param>
- /// <returns>The minimum height of the cell.</returns>
- int getTextHeight(TXTextControl.TableCell cell, TXTextControl.TextControl textControl)
- {
- TXTextControl.TextChar firstTextChar = textControl.TextChars[cell.Start];
- TXTextControl.TextChar lastTextChar = textControl.TextChars[cell.Start + cell.Length];
- return lastTextChar.Bounds.Bottom - firstTextChar.Bounds.Top + cell.CellFormat.BottomTextDistance + cell.CellFormat.TopTextDistance;
- }
复制代码 |