找回密码
 立即注册

QQ登录

只需一步,快速开始

mindrayguowei

中级会员

122

主题

274

帖子

986

积分

中级会员

积分
986

活字格认证

QQ
mindrayguowei
中级会员   /  发表于:2017-12-7 15:23  /   查看:3043  /  回复:3
我们想要在图片上通过鼠标点击来插入一个Shape,但是现在有点问题, Shape无法插入到鼠标点击的坐标点, 插入的坐标点和鼠标点击的坐标点有偏差. 如何计算这个Shape在图片上的坐标.
以下是我们的代码
  1. private void textControl1_ImageClicked(object sender, TXTextControl.ImageEventArgs e)
  2.         {
  3.             Point location = textControl1.PointToClient(MousePosition);
  4.             int left = location.X *15 + textControl1.ScrollLocation.X;
  5.             int top = location.Y *15 + textControl1.ScrollLocation.Y;
  6.             addShape("Dodecagon", left, top, 500, 500, System.Drawing.Color.Red);
  7.         }
复制代码
  1. private void addShape(string shapeName, int left, int top, int iWidth, int iHeight, System.Drawing.Color gbColor)
  2.         {
  3.             if (string.IsNullOrEmpty(shapeName))
  4.             {
  5.                 return;
  6.             }
  7.             if (textControl1.Drawings.GetActivatedItem() != null)
  8.             {
  9.                 // create new Shape object from selected menu item text string
  10.                 TXTextControl.Drawing.Shape newShape = new TXTextControl.Drawing.Shape((ShapeType)Enum.Parse(typeof(ShapeType), shapeName));

  11.                 // add Shape object to drawing canvas
  12.                 ((TXTextControl.Drawing.TXDrawingControl)textControl1.Drawings.GetActivatedItem().Drawing).Shapes.Add(newShape, ShapeCollection.AddStyle.MouseCreation);
  13.             }
  14.             else
  15.             {
  16.                 TXTextControl.Drawing.TXDrawingControl drawingObject = new TXTextControl.Drawing.TXDrawingControl(iWidth, iHeight);

  17.                 // create a new DrawingFrame object that hosts the TXDrawingControl
  18.                 TXTextControl.DataVisualization.DrawingFrame drawingFrame = new TXTextControl.DataVisualization.DrawingFrame(drawingObject);

  19.                 // add the frame object to the Drawings collection
  20.                 textControl1.Drawings.Add(drawingFrame, new Point(left, top), -1, TXTextControl.FrameInsertionMode.DisplaceText);

  21.                 TXTextControl.Drawing.Shape newShape = new TXTextControl.Drawing.Shape((ShapeType)Enum.Parse(typeof(ShapeType), shapeName));

  22.                 newShape.AutoSize = true;
  23.                 newShape.Sizable = false;
  24.                 newShape.Movable = false;
  25.                 newShape.ShapeFill.Color = gbColor; //底色

  26.                 // add shape directly to TextControl
  27.                 drawingObject.Shapes.Add(newShape, ShapeCollection.AddStyle.Fill);
  28.                 textControl1.Refresh();
  29.             }
  30.         }
复制代码


3 个回复

倒序浏览
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2017-12-11 12:27:21
沙发
经过多次的计算,改成这样子的话,就可以基本上落到正确的位置上,请参考下面的代码,替换你发的第一段


  1.         private void _textControl_ImageClicked(object sender, TXTextControl.ImageEventArgs e)
  2.         {

  3.             Point location = _textControl.PointToClient(MousePosition);
  4.             Point scroll = _textControl.ScrollLocation;
  5.             double pageleft = _textControl.Sections.GetItem().Format.PageMargins.Left;
  6.             double pagetop = _textControl.Sections.GetItem().Format.PageMargins.Top;
  7.             int left = (location.X+ scroll.X/15- (int)pageleft) * 15-250 ;
  8.             int top = (location.Y + (scroll.Y-570)/15- (int)pagetop) * 15-250 ;
  9.             addShape("Dodecagon", left, top, 500, 500, System.Drawing.Color.Red);
  10.         }
复制代码
回复 使用道具 举报
mindrayguowei
中级会员   /  发表于:2017-12-12 09:21:03
板凳
我试试看
回复 使用道具 举报
Richard.Ma讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2017-12-13 09:18:05
地板
好的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部