之前我们在 TX Text Control 中表格操作之获取选中单元格坐标 文章中介绍了如何获取选中单元格的坐标和范围,本文将介绍如何获取焦点单元格的大小,包括宽度和高度。TX中的表格宽度不会根据内容自动变宽,而高度可以根据内容自动增高,所以,TableCell只提供的宽度Width属性,而没有提供高度Height属性。下面是获取单元格高度的实现代码。
基本思路,首先获取单元格中第一行的 TextBounds 属性,然后获取单元格最后一行的TextBounds,从而计算出单元格的高度,代码如下:
private void heightToolStripMenuItem_Click(object sender, EventArgs e)
{
TXTextControl.Table table = textControl1.Tables.GetItem(1000);
TXTextControl.TableCell cell = table.Cells.GetItem();
TXTextControl.Line lstart = textControl1.Lines.GetItem(cell.Start);
TXTextControl.Line lend = textControl1.Lines.GetItem(cell.Start + cell.Length - 1);
int width = cell.Width - cell.CellFormat.LeftTextDistance - cell.CellFormat.RightTextDistance;
int height = lend.TextBounds.Top - lstart.TextBounds.Top + lend.TextBounds.Height;
MessageBox.Show(string.Format("当前单元格坐标为:({0},{1}),宽度:{2},高度:{3}", cell.Row, cell.Column, width, height));
}
运行截图:
源码下载:VS2010 + TX X9 for WinForms