mindrayguowei 发表于 2017-12-7 15:23:59

在图片上添加Shape的问题

我们想要在图片上通过鼠标点击来插入一个Shape,但是现在有点问题, Shape无法插入到鼠标点击的坐标点, 插入的坐标点和鼠标点击的坐标点有偏差. 如何计算这个Shape在图片上的坐标.
以下是我们的代码
private void textControl1_ImageClicked(object sender, TXTextControl.ImageEventArgs e)
      {
            Point location = textControl1.PointToClient(MousePosition);
            int left = location.X *15 + textControl1.ScrollLocation.X;
            int top = location.Y *15 + textControl1.ScrollLocation.Y;
            addShape("Dodecagon", left, top, 500, 500, System.Drawing.Color.Red);
      }private void addShape(string shapeName, int left, int top, int iWidth, int iHeight, System.Drawing.Color gbColor)
      {
            if (string.IsNullOrEmpty(shapeName))
            {
                return;
            }
            if (textControl1.Drawings.GetActivatedItem() != null)
            {
                // create new Shape object from selected menu item text string
                TXTextControl.Drawing.Shape newShape = new TXTextControl.Drawing.Shape((ShapeType)Enum.Parse(typeof(ShapeType), shapeName));

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

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

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

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

                newShape.AutoSize = true;
                newShape.Sizable = false;
                newShape.Movable = false;
                newShape.ShapeFill.Color = gbColor; //底色

                // add shape directly to TextControl
                drawingObject.Shapes.Add(newShape, ShapeCollection.AddStyle.Fill);
                textControl1.Refresh();
            }
      }

Richard.Ma 发表于 2017-12-11 12:27:21

经过多次的计算,改成这样子的话,就可以基本上落到正确的位置上,请参考下面的代码,替换你发的第一段


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

            Point location = _textControl.PointToClient(MousePosition);
            Point scroll = _textControl.ScrollLocation;
            double pageleft = _textControl.Sections.GetItem().Format.PageMargins.Left;
            double pagetop = _textControl.Sections.GetItem().Format.PageMargins.Top;
            int left = (location.X+ scroll.X/15- (int)pageleft) * 15-250 ;
            int top = (location.Y + (scroll.Y-570)/15- (int)pagetop) * 15-250 ;
            addShape("Dodecagon", left, top, 500, 500, System.Drawing.Color.Red);
      }

mindrayguowei 发表于 2017-12-12 09:21:03

我试试看

Richard.Ma 发表于 2017-12-13 09:18:05

好的
页: [1]
查看完整版本: 在图片上添加Shape的问题