找回密码
 立即注册

QQ登录

只需一步,快速开始

mindrayguowei

中级会员

122

主题

274

帖子

986

积分

中级会员

积分
986

活字格认证

QQ
mindrayguowei
中级会员   /  发表于:2017-11-17 14:34  /   查看:3314  /  回复:5
我需要在Table里面添加CheckBox等科编辑的控件, 现在的做法是通过TableCell.Start取坐标, 但是这个坐标好像不不准,加进去的控件位置不对.

  1. Table table = textControl1.Tables.GetItem(100);
  2.             if (table == null) return;

  3.             int dpiX = Helper.Instance.GetDpiX(textControl1);

  4.             for (int i = 1; i <= table.Rows.Count; i++)
  5.             {
  6.                 int rowIndex = i + 1;
  7.                 TableCell cell = table.Cells.GetItem(rowIndex, columnIndex);

  8.                 textControl1.InputPosition = new InputPosition(cell.Start - 1);

  9.                 int x = textControl1.InputPosition.Location.X;
  10.                 int y = textControl1.InputPosition.Location.Y;

  11.                 //Line start = textControl1.Lines.GetItem(cell.Start - 1);
  12.                 //Line end = textControl1.Lines.GetItem(cell.Start + cell.Length - 1);

  13.                 //int x = start.TextBounds.Location.X;
  14.                 //int y = start.TextBounds.Location.Y;

  15.                 int left = (x - textControl1.ScrollLocation.X) / dpiX;
  16.                 int top = (y - textControl1.ScrollLocation.Y) / dpiX;
  17.                 //int left = x;
  18.                 //int top = y;

  19.                 Point p = new Point(left, top);

  20.                 CheckBox control = new CheckBox();
  21.                 //control.Location = new Point(100, 100);
  22.                 control.Location = p;
  23.                 //control.Text = "text";
  24.                 control.AutoSize = true;

  25.                 control.BringToFront();

  26.                 textControl1.Controls.Add(control);
  27.             }
复制代码
出来的效果是这样的, 怎么才能精确的把控件添加到Cell里面呢?

S7H%OBI9IM@65ML5)QZ_%%C.png

5 个回复

倒序浏览
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2017-11-20 15:32:58
沙发
开发包本身目前确实没有别的的方法直接获取到位置,
回复 使用道具 举报
mindrayguowei
中级会员   /  发表于:2017-11-20 17:26:15
板凳
那这个没有解决方法吗?
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2017-11-21 12:20:37
地板
我这边也没有其他的办法了,只能是调整来达到界面上的一致,我调整你的代码以后是可以的,但是缩放文档肯定就会错位
image.png185473631.png

  1.         private void testToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             _textControl.Tables.Add(3, 3);
  4.             Table table = _textControl.Tables.GetItem(0);
  5.             if (table == null) return;

  6.             int dpiX = 15;

  7.             for (int i = 1; i <= table.Rows.Count; i++)
  8.             {
  9.                 for (int j = 1; j <= table.Columns.Count; j++)
  10.                 {
  11.                     TableCell cell = table.Cells.GetItem(i, j);

  12.                     _textControl.InputPosition = new InputPosition(cell.Start - 1);

  13.                     int x = _textControl.InputPosition.Location.X;
  14.                     int y = _textControl.InputPosition.Location.Y;

  15.                     //Line start = textControl1.Lines.GetItem(cell.Start - 1);
  16.                     //Line end = textControl1.Lines.GetItem(cell.Start + cell.Length - 1);

  17.                     //int x = start.TextBounds.Location.X;
  18.                     //int y = start.TextBounds.Location.Y;

  19.                     int left = (x - _textControl.ScrollLocation.X) / dpiX;
  20.                     int top = (y - _textControl.ScrollLocation.Y) / dpiX;
  21.                     //int left = x;
  22.                     //int top = y;

  23.                     Point p = new Point(left, top);

  24.                     CheckBox control = new CheckBox();
  25.                     //control.Location = new Point(100, 100);
  26.                     control.Location = p;
  27.                     //control.Text = "text";
  28.                     control.AutoSize = true;

  29.                     control.BringToFront();

  30.                     _textControl.Controls.Add(control);
  31.                 }

  32.             }
复制代码


回复 使用道具 举报
mindrayguowei
中级会员   /  发表于:2017-11-22 16:18:52
5#
看起来可以解决问题, 缩放文档我再研究下. 谢谢
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2017-11-22 17:50:53
6#
我这边帮你写了一个,可以解决问题。这个方法放在Zoomed事件里面即可

  1.         private void _textControl_Zoomed(object sender, EventArgs e)
  2.         {
  3.             int zoom = _textControl.ZoomFactor;
  4.             foreach (CheckBox cb in lc) {
  5.                 string[] cdf = cb.Tag.ToString().Split(new char[] { ';'});
  6.                 TableCell cell = _textControl.Tables.GetItem(0).Cells.GetItem(Int16.Parse(cb.Tag.ToString().Split(new char[] { ';' })[0]), Int16.Parse(cb.Tag.ToString().Split(new char[] { ';' })[1]));
  7.                 _textControl.InputPosition = new InputPosition(cell.Start - 1);
  8.                 int x = _textControl.InputPosition.Location.X;
  9.                 int y = _textControl.InputPosition.Location.Y;

  10.                 //Line start = textControl1.Lines.GetItem(cell.Start - 1);
  11.                 //Line end = textControl1.Lines.GetItem(cell.Start + cell.Length - 1);

  12.                 //int x = start.TextBounds.Location.X;
  13.                 //int y = start.TextBounds.Location.Y;

  14.                 int left = (x - _textControl.ScrollLocation.X) / 15*zoom/100;
  15.                 int top = (y - _textControl.ScrollLocation.Y) / 15 * zoom / 100;
  16.                 //int left = x;
  17.                 //int top = y;

  18.                 Point p = new Point(left, top);
  19.                 cb.Location = p;
  20.             }
  21.         }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部