我们想要在图片上通过鼠标点击来插入一个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();
- }
- }
复制代码
|
|