简体   繁体   中英

How to display a TextBox over a custom Stroke in an InkCanvas?

I'm using an InkCanvas on which I display some custom strokes (mainly to represent some shapes, such as a Rectangle or a RoundedRectangle). I can perfectly draw, select, move and resize those shapes, but now I want to add some text inside those shapes.

The thing is, the custom strokes can't hold a list of children, so I cannot add a TextBox to a specific stroke. I tried adding a TextBox at a specific position (relative to the stroke's position) inside the InkCanvas children, but the result is pretty bad because the TextBox is always behind my custom stroke.

Is there any way to do this?

Thanks

Edit: This is the xaml code

<InkCanvas ClipToBounds="True" Grid.Column="0" Grid.Row="0" Name="surfaceDessin" 
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"                           
    MouseLeave="surfaceDessin_MouseLeave" MouseMove="surfaceDessin_MouseMove" PreviewMouseMove="InkCanvas_LeftMouseMove" PreviewMouseUp="InkCanvas_LeftMouseUp" PreviewMouseDown="InkCanvas_LeftMouseDown"                                   
    Strokes="{Binding Path=Traits, Mode=OneTime}" EditingMode="{Binding Path=OutilSelectionne, Converter={StaticResource convertisseurModeEdition}, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
    DefaultDrawingAttributes="{Binding Path=AttributsDessin, Mode=OneTime}"/>

As I don't know exactly what is going on in the C# side,+ I did a little experiment, it seems like you can use your drawing context in your custom stroke class (I understand that you have one) to draw the text at the position you like. Even if this is not a perfect solution, I still think is better than hacking it with a textbox. In summary, you need to add one line only to your custom stroke class (drawingcontext.DrawText....)

PS: the code mostly is form other sources and not my own completely.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        public void DrawTextInRectangle(object sender, RoutedEventArgs e)
        {
            StylusPointCollection pts = new StylusPointCollection();

            pts.Add(new StylusPoint(100, 100));

            pts.Add(new StylusPoint(100, 300));

            pts.Add(new StylusPoint(300, 300));

            pts.Add(new StylusPoint(300, 100));

            pts.Add(new StylusPoint(100, 100));

            CustomStroke st = new CustomStroke(pts);

            st.DrawingAttributes.Color = Colors.Red;
            inkCanvas1.Strokes.Add(st);



        }




    }

}

    // A class for rendering custom strokes
    class CustomStroke : Stroke
    {
        Brush brush;
        Pen pen;

        public CustomStroke(StylusPointCollection stylusPoints)
            : base(stylusPoints)
        {
            // Create the Brush and Pen used for drawing.
            brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20d);
            pen = new Pen(brush, 2d);
        }

        protected override void DrawCore(DrawingContext drawingContext,
                                         DrawingAttributes drawingAttributes)
        {
            // Allocate memory to store the previous point to draw from.
            Point prevPoint = new Point(double.NegativeInfinity,
                                        double.NegativeInfinity);

            // Draw linear gradient ellipses between 
            // all the StylusPoints in the Stroke.
            for (int i = 0; i < this.StylusPoints.Count; i++)
            {
                Point pt = (Point)this.StylusPoints[i];
                Vector v = Point.Subtract(prevPoint, pt);

                // Only draw if we are at least 4 units away 
                // from the end of the last ellipse. Otherwise, 
                // we're just redrawing and wasting cycles.
                if (v.Length > 4)
                {
                    // Set the thickness of the stroke 
                    // based on how hard the user pressed.
                    double radius = this.StylusPoints[i].PressureFactor * 10d;
                    drawingContext.DrawEllipse(brush, pen, pt, radius, radius);

                    prevPoint = pt;
                }
            }
            Typeface tf = new Typeface("Arial");
            FormattedText ft = new FormattedText("test test", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, tf, 15, Brushes.Red);
            drawingContext.DrawText(ft, new Point(1.5 * prevPoint.X, 1.5 * prevPoint.Y));



        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM