【问题标题】:How to display a TextBox over a custom Stroke in an InkCanvas?如何在 InkCanvas 中的自定义 Stroke 上显示 TextBox?
【发布时间】:2019-03-07 19:35:40
【问题描述】:

我正在使用 InkCanvas,在其上显示一些自定义笔划(主要用于表示一些形状,例如 Rectangle 或 RoundedRectangle)。我可以完美地绘制、选择、移动和调整这些形状,但现在我想在这些形状中添加一些文本。

问题是,自定义笔划不能包含子项列表,因此我无法将 TextBox 添加到特定笔划。我尝试在 InkCanvas 子项内的特定位置(相对于笔划的位置)添加一个 TextBox,但结果非常糟糕,因为 TextBox 始终位于我的自定义笔划后面。

有什么办法吗?

谢谢

编辑:这是 xaml 代码

<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}"/>

【问题讨论】:

  • 你能展示你当前的 XAML 代码吗?
  • 我添加了 InkCanvas 的 XAML 代码。我们所做的大部分事情(例如向surfaceDessin.Strokes 添加自定义形状)都是在C# 中完成的。
  • 刚刚发布了一些东西作为答案,因为在这里发布评论很长时间

标签: c# wpf textbox stroke inkcanvas


【解决方案1】:

由于我不知道 C# 端到底发生了什么,+ 我做了一个小实验,看起来你可以在自定义笔画类中使用你的绘图上下文(我知道你有一个)来绘制你喜欢的位置的文字。即使这不是一个完美的解决方案,我仍然认为比用文本框破解它要好。总之,您只需在自定义笔画类中添加一行(drawingcontext.DrawText....)

PS:代码大多来自其他来源,而不是我自己的。

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));



        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多