none
InkToolBarのペンの初期値 RRS feed

  • 質問

  • InkToolbar とInkCanvasを使って開発を行っています。

    アプリを起動したときに前回開いていた時のペンの設定を持ち越す実装をしたいです。

        // Set initial ink stroke attributes.
        InkDrawingAttributes drawingAttributes = new InkDrawingAttributes();
        drawingAttributes.Color = lastColor;
        inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes);

    のようなコードで設定を行うことを試しましたがinkCanvasとinkToolBarの表示の設定がずれてしまいうまくいきません。

    どうすればいいのか教えてください。


    2020年9月27日 1:58

回答

  • こんな?

    namespace App1
    {
        using System;
        using System.Linq;
        using System.Runtime.InteropServices.WindowsRuntime;
        using Windows.UI;
        using Windows.UI.Input.Inking;
        using Windows.UI.Xaml.Controls;
        using Windows.UI.Xaml.Media;
    
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                inkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse | Windows.UI.Core.CoreInputDeviceTypes.Pen;
    
                InkStatus status = new InkStatus();
                status.Attributes = new InkDrawingAttributes();
                status.Attributes.Color = Colors.Red;
                status.Attributes.IgnorePressure = false;
                status.Attributes.FitToCurve = true;
    
                status.BallpointColor = Colors.Red;
                status.BallpointStrokeWidth = 1;
    
                status.PencilColor = Colors.Green;
                status.PencilStrokeWidth = 5;
    
                status.HighlightColor = Color.FromArgb(0xFF, 0x66,0x00,0xCC);
                status.HighlightStrokeWidth = 10;
    
                status.CurrentButtonType = InkToolbarTool.Highlighter;
    
                status.LoadTo(this.inkCanvas, this.inkToolbar);
            }
        }
    
    
        class InkStatus
        {
            public InkStatus()
            {
            }
    
            public InkStatus
                (InkDrawingAttributes attributes
                , InkToolbarTool currentButtonType
                , Color ballpointColor, double ballpointWidth
                , Color pencilColor, double pencilWidth
                , Color highlightColor, double highlightWidth)
            {
                this.Attributes = attributes;
                this.CurrentButtonType = currentButtonType;
                this.BallpointColor = ballpointColor;
                this.BallpointStrokeWidth = ballpointWidth;
                this.PencilColor = pencilColor;
                this.PencilStrokeWidth = pencilWidth;
                this.HighlightColor = highlightColor;
                this.HighlightStrokeWidth = highlightWidth;
            }
    
            public InkDrawingAttributes Attributes { get; set; }
    
            public InkToolbarTool CurrentButtonType { get; set; }
    
            public Color BallpointColor { get; set; }
            public double BallpointStrokeWidth { get; set; }
    
            public Color PencilColor { get; set; }
            public double PencilStrokeWidth { get; set; }
    
            public Color HighlightColor { get; set; }
            public double HighlightStrokeWidth { get; set; }
    
            public void LoadTo(InkCanvas inkCanvas, InkToolbar inkToolbar)
            {
                if (!inkToolbar.IsLoaded)
                {
                    inkToolbar.Loaded += (s, e) =>
                    {
                        LoadTo(inkCanvas, inkToolbar);
                    };
                    return;
                }
    
                var ballpointBrush = new SolidColorBrush(this.BallpointColor);
                var pencilBrush = new SolidColorBrush(this.PencilColor);
                var highlightBrush = new SolidColorBrush(this.HighlightColor);
    
                inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(this.Attributes);
    
                var ballpointButton = inkToolbar.GetToolButton(InkToolbarTool.BallpointPen) as InkToolbarBallpointPenButton;
                var pencilButton = inkToolbar.GetToolButton(InkToolbarTool.Pencil) as InkToolbarPencilButton;
                var highlightButton = inkToolbar.GetToolButton(InkToolbarTool.Highlighter) as InkToolbarHighlighterButton;
                //var eraserButton = toolbar.GetToolButton(InkToolbarTool.Eraser) as InkToolbarPencilButton;
                //var custonButton = toolbar.GetToolButton(InkToolbarTool.CustomPen) as InkToolbarCustomPenButton;
                //var customToolButton = toolbar.GetToolButton(InkToolbarTool.CustomTool) as InkToolbarCustomToolButton;
    
                SetCurrentBrush(ballpointButton, ballpointBrush, this.BallpointStrokeWidth);
                SetCurrentBrush(pencilButton, pencilBrush, this.PencilStrokeWidth);
                SetCurrentBrush(highlightButton, highlightBrush, this.HighlightStrokeWidth);
    
                inkToolbar.ActiveTool = inkToolbar.GetToolButton(this.CurrentButtonType);
            }
    
            private static void SetCurrentBrush(InkToolbarPenButton button, Brush brush, double strokeWidth)
            {
                if (button != null && brush != null)
                {
                    if (brush is SolidColorBrush solid)
                    {
                        var mtt = solid.Transform as MatrixTransform;
                        var mtr = solid.RelativeTransform as MatrixTransform;
    
                        var colros = button.Palette.OfType<SolidColorBrush>().Select(_ => _.Color).ToArray();
                        foreach (SolidColorBrush palette in button.Palette.OfType<SolidColorBrush>().Where(_ => _.Color == solid.Color))
                        {
                            if (palette.Transform is MatrixTransform mt1 && palette.RelativeTransform is MatrixTransform mt2)
                            {
                                if (mt1.Matrix == mtt.Matrix && mt2.Matrix == mtr.Matrix)
                                {
                                    brush = palette;
                                    break;
                                }
                            }
                        }
                    }
    
                    int index = button.Palette.IndexOf(brush);
                    if (index < 0)
                    {
                        button.Palette.Add(brush);
                        index = button.Palette.Count - 1;
                    }
                    button.SelectedStrokeWidth = strokeWidth;
                    button.SelectedBrushIndex = index;
    
                }
            }
    
            public void GetFrom(InkCanvas inkCanvas, InkToolbar inkToolbar)
            {
                if (!inkToolbar.IsLoaded)
                {
                    throw new System.InvalidOperationException();
                }
    
                this.Attributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
    
                var ballpointButton = inkToolbar.GetToolButton(InkToolbarTool.BallpointPen) as InkToolbarBallpointPenButton;
                var pencilButton = inkToolbar.GetToolButton(InkToolbarTool.Pencil) as InkToolbarPencilButton;
                var highlightButton = inkToolbar.GetToolButton(InkToolbarTool.Highlighter) as InkToolbarHighlighterButton;
    
                this.BallpointColor = (ballpointButton.SelectedBrush as SolidColorBrush)?.Color ?? Colors.Transparent;
                this.BallpointStrokeWidth = ballpointButton.SelectedStrokeWidth;
    
                this.PencilColor = (pencilButton.SelectedBrush as SolidColorBrush)?.Color ?? Colors.Transparent;
                this.PencilStrokeWidth = pencilButton.SelectedStrokeWidth;
    
                this.HighlightColor = (highlightButton.SelectedBrush as SolidColorBrush)?.Color ?? Colors.Transparent;
                this.HighlightStrokeWidth = highlightButton.SelectedStrokeWidth;
            }
        }
    }


    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    • 回答としてマーク shimonOP 2020年9月29日 1:33
    2020年9月28日 9:34

すべての返信

  • こんな?

    namespace App1
    {
        using System;
        using System.Linq;
        using System.Runtime.InteropServices.WindowsRuntime;
        using Windows.UI;
        using Windows.UI.Input.Inking;
        using Windows.UI.Xaml.Controls;
        using Windows.UI.Xaml.Media;
    
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                inkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse | Windows.UI.Core.CoreInputDeviceTypes.Pen;
    
                InkStatus status = new InkStatus();
                status.Attributes = new InkDrawingAttributes();
                status.Attributes.Color = Colors.Red;
                status.Attributes.IgnorePressure = false;
                status.Attributes.FitToCurve = true;
    
                status.BallpointColor = Colors.Red;
                status.BallpointStrokeWidth = 1;
    
                status.PencilColor = Colors.Green;
                status.PencilStrokeWidth = 5;
    
                status.HighlightColor = Color.FromArgb(0xFF, 0x66,0x00,0xCC);
                status.HighlightStrokeWidth = 10;
    
                status.CurrentButtonType = InkToolbarTool.Highlighter;
    
                status.LoadTo(this.inkCanvas, this.inkToolbar);
            }
        }
    
    
        class InkStatus
        {
            public InkStatus()
            {
            }
    
            public InkStatus
                (InkDrawingAttributes attributes
                , InkToolbarTool currentButtonType
                , Color ballpointColor, double ballpointWidth
                , Color pencilColor, double pencilWidth
                , Color highlightColor, double highlightWidth)
            {
                this.Attributes = attributes;
                this.CurrentButtonType = currentButtonType;
                this.BallpointColor = ballpointColor;
                this.BallpointStrokeWidth = ballpointWidth;
                this.PencilColor = pencilColor;
                this.PencilStrokeWidth = pencilWidth;
                this.HighlightColor = highlightColor;
                this.HighlightStrokeWidth = highlightWidth;
            }
    
            public InkDrawingAttributes Attributes { get; set; }
    
            public InkToolbarTool CurrentButtonType { get; set; }
    
            public Color BallpointColor { get; set; }
            public double BallpointStrokeWidth { get; set; }
    
            public Color PencilColor { get; set; }
            public double PencilStrokeWidth { get; set; }
    
            public Color HighlightColor { get; set; }
            public double HighlightStrokeWidth { get; set; }
    
            public void LoadTo(InkCanvas inkCanvas, InkToolbar inkToolbar)
            {
                if (!inkToolbar.IsLoaded)
                {
                    inkToolbar.Loaded += (s, e) =>
                    {
                        LoadTo(inkCanvas, inkToolbar);
                    };
                    return;
                }
    
                var ballpointBrush = new SolidColorBrush(this.BallpointColor);
                var pencilBrush = new SolidColorBrush(this.PencilColor);
                var highlightBrush = new SolidColorBrush(this.HighlightColor);
    
                inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(this.Attributes);
    
                var ballpointButton = inkToolbar.GetToolButton(InkToolbarTool.BallpointPen) as InkToolbarBallpointPenButton;
                var pencilButton = inkToolbar.GetToolButton(InkToolbarTool.Pencil) as InkToolbarPencilButton;
                var highlightButton = inkToolbar.GetToolButton(InkToolbarTool.Highlighter) as InkToolbarHighlighterButton;
                //var eraserButton = toolbar.GetToolButton(InkToolbarTool.Eraser) as InkToolbarPencilButton;
                //var custonButton = toolbar.GetToolButton(InkToolbarTool.CustomPen) as InkToolbarCustomPenButton;
                //var customToolButton = toolbar.GetToolButton(InkToolbarTool.CustomTool) as InkToolbarCustomToolButton;
    
                SetCurrentBrush(ballpointButton, ballpointBrush, this.BallpointStrokeWidth);
                SetCurrentBrush(pencilButton, pencilBrush, this.PencilStrokeWidth);
                SetCurrentBrush(highlightButton, highlightBrush, this.HighlightStrokeWidth);
    
                inkToolbar.ActiveTool = inkToolbar.GetToolButton(this.CurrentButtonType);
            }
    
            private static void SetCurrentBrush(InkToolbarPenButton button, Brush brush, double strokeWidth)
            {
                if (button != null && brush != null)
                {
                    if (brush is SolidColorBrush solid)
                    {
                        var mtt = solid.Transform as MatrixTransform;
                        var mtr = solid.RelativeTransform as MatrixTransform;
    
                        var colros = button.Palette.OfType<SolidColorBrush>().Select(_ => _.Color).ToArray();
                        foreach (SolidColorBrush palette in button.Palette.OfType<SolidColorBrush>().Where(_ => _.Color == solid.Color))
                        {
                            if (palette.Transform is MatrixTransform mt1 && palette.RelativeTransform is MatrixTransform mt2)
                            {
                                if (mt1.Matrix == mtt.Matrix && mt2.Matrix == mtr.Matrix)
                                {
                                    brush = palette;
                                    break;
                                }
                            }
                        }
                    }
    
                    int index = button.Palette.IndexOf(brush);
                    if (index < 0)
                    {
                        button.Palette.Add(brush);
                        index = button.Palette.Count - 1;
                    }
                    button.SelectedStrokeWidth = strokeWidth;
                    button.SelectedBrushIndex = index;
    
                }
            }
    
            public void GetFrom(InkCanvas inkCanvas, InkToolbar inkToolbar)
            {
                if (!inkToolbar.IsLoaded)
                {
                    throw new System.InvalidOperationException();
                }
    
                this.Attributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
    
                var ballpointButton = inkToolbar.GetToolButton(InkToolbarTool.BallpointPen) as InkToolbarBallpointPenButton;
                var pencilButton = inkToolbar.GetToolButton(InkToolbarTool.Pencil) as InkToolbarPencilButton;
                var highlightButton = inkToolbar.GetToolButton(InkToolbarTool.Highlighter) as InkToolbarHighlighterButton;
    
                this.BallpointColor = (ballpointButton.SelectedBrush as SolidColorBrush)?.Color ?? Colors.Transparent;
                this.BallpointStrokeWidth = ballpointButton.SelectedStrokeWidth;
    
                this.PencilColor = (pencilButton.SelectedBrush as SolidColorBrush)?.Color ?? Colors.Transparent;
                this.PencilStrokeWidth = pencilButton.SelectedStrokeWidth;
    
                this.HighlightColor = (highlightButton.SelectedBrush as SolidColorBrush)?.Color ?? Colors.Transparent;
                this.HighlightStrokeWidth = highlightButton.SelectedStrokeWidth;
            }
        }
    }


    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    • 回答としてマーク shimonOP 2020年9月29日 1:33
    2020年9月28日 9:34
  • まさにこれです!

    ありがとうございます!解決しました。

    2020年9月29日 1:32