前提・実現したいこと
筆圧を測定するUWPアプリを作成しています.
画面に表示されている図形をなぞり,なぞっているカーソルの現在の座標値を表示するようにしたい.
発生している問題・エラーメッセージ
表示されている円の中にカーソルを持ってきたときだけしかPointerPressed,PointerMoved,PointerReleased関数が動作していない.円の中以外のアプリの画面にカーソルがあるときも動作してほしい.
プログラミング初心者なため,丁寧に教えていただけると助かります.
該当のソースコード
BlankPage1xaml
1<Page 2 x:Class="UWPApp1.BlankPage1" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:UWPApp1" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 mc:Ignorable="d" 9 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 10 11 <Grid> 12 <Grid PointerPressed="Canvas_PointerPressed"> 13 <Grid PointerMoved="Canvas_PointerMoved"> 14 <Grid PointerReleased="Canvas_PointerReleased"> 15 <Grid PointerMoved="Grid_PointerMoved"> 16 <Grid Background="White"> 17 <Grid.RowDefinitions> 18 <RowDefinition Height="Auto"/> 19 <RowDefinition Height="*"/> 20 </Grid.RowDefinitions> 21 <StackPanel x:Name="HeaderPanel" 22 Orientation="Horizontal" 23 Grid.Row="0"> 24 <TextBlock x:Name="Header" 25 Style="{ThemeResource HeaderTextBlockStyle}" 26 Margin="10,0,0,0" SelectionChanged="Header_SelectionChanged" > 27 <Run Text="描画してください"/><LineBreak/><Run/></TextBlock> 28 <TextBlock x:Name="text_position" TextWrapping="Wrap" Text="TextBlock" FontSize="24"/> 29 <TextBlock x:Name="text_isPointerOn" TextWrapping="Wrap" Text="TextBlock" FontSize="24"/> 30 31 </StackPanel> 32 33 34 35 36 37 <Grid x:Name="drawingCanvas" Grid.Row="1" FocusVisualPrimaryBrush="Black"> 38 <Grid.ColumnDefinitions> 39 <ColumnDefinition Width="0*"/> 40 <ColumnDefinition/> 41 </Grid.ColumnDefinitions> 42 <Grid.FocusVisualSecondaryBrush> 43 <AcrylicBrush/> 44 </Grid.FocusVisualSecondaryBrush> 45 46 <!-- The canvas where we render the replacement text and shapes. --> 47 <!-- The canvas for ink input. --> 48 <InkCanvas x:Name="inkCanvas" Grid.ColumnSpan="2" Margin="6,0,-6,0" FocusVisualPrimaryBrush="{x:Null}" FocusVisualSecondaryBrush="{x:Null}" Opacity="0.5" /> 49 50 <!-- The canvas where we render the replacement text and shapes. --> 51 <Canvas x:Name="recognitionCanvas" Grid.ColumnSpan="2" /> 52 <!-- The canvas for ink input. --> 53 54 </Grid> 55 56 </Grid> 57 58 <Canvas HorizontalAlignment="Left" VerticalAlignment="Top"> 59 60 61 62 <Ellipse 63 Stroke="#FF044B0A" FocusVisualPrimaryBrush="{x:Null}" FocusVisualSecondaryBrush="{x:Null}" Margin="132,189,-132,-189" 64 Height="187" Width="189" RenderTransformOrigin="0.5,0.5" Visibility="Visible" RequestedTheme="Light" Canvas.Left="99" Canvas.Top="162" UseLayoutRounding="False" d:LayoutRounding="Auto" 65 > 66 <Ellipse.RenderTransform> 67 <CompositeTransform Rotation="-0.481"/> 68 </Ellipse.RenderTransform> 69 </Ellipse> 70 71 72 </Canvas> 73 <Button Content="Button" Margin="118,63,0,0" VerticalAlignment="Top" RenderTransformOrigin="-3.172,0.297" Click="Button_Click"/> 74 <TextBlock HorizontalAlignment="Left" Margin="121,91,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="24"><Run Text="戻るボタン"/><LineBreak/><Run/></TextBlock> 75 76 </Grid> 77 </Grid> 78 </Grid> 79 </Grid> 80 </Grid> 81 82</Page> 83 84 85
BlankPage1cs
1using System; 2using System.Collections.Generic; 3using System.Diagnostics; 4using System.IO; 5using System.Linq; 6using System.Runtime.InteropServices.WindowsRuntime; 7using Windows.Devices.Input; 8using Windows.Foundation; 9using Windows.Foundation.Collections; 10using Windows.UI.Core; 11using Windows.UI.Input; 12using Windows.UI.Input.Inking; 13using Windows.UI.Xaml; 14using Windows.UI.Xaml.Controls; 15using Windows.UI.Xaml.Controls.Primitives; 16using Windows.UI.Xaml.Data; 17using Windows.UI.Xaml.Input; 18using Windows.UI.Xaml.Media; 19using Windows.UI.Xaml.Navigation; 20 21 22// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=234238 を参照してください 23 24namespace UWPApp1 25{ 26 /// <summary> 27 /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。 28 /// </summary> 29 public sealed partial class BlankPage1 : Page 30 { 31 public BlankPage1() 32 { 33 this.InitializeComponent(); 34 // 描画属性を作成する 35 InkDrawingAttributes attributes = new InkDrawingAttributes(); 36 attributes.Color = Windows.UI.Colors.Red; // ペンの色 37 attributes.Size = new Size(5, 2); // ペンのサイズ 38 attributes.IgnorePressure = true; // ペンの圧力を使用するかどうか 39 attributes.FitToCurve = false; 40 41 // インクキャンバスに属性を設定する 42 inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(attributes); 43 // マウスとペンによる描画を許可する 44 inkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse | Windows.UI.Core.CoreInputDeviceTypes.Pen; 45 } 46 protected override void OnNavigatedTo(NavigationEventArgs e) 47 { 48 SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = Frame.CanGoBack ? 49 AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed; 50 SystemNavigationManager.GetForCurrentView().BackRequested += hogehoge; 51 base.OnNavigatedTo(e); 52 } 53 private void hogehoge(object obj, Windows.UI.Core.BackRequestedEventArgs e) 54 { 55 if (Frame.CanGoBack) 56 { 57 Frame.GoBack(); 58 if (e != null) 59 e.Handled = true; 60 } 61 } 62 private void Button_Click(object sender, RoutedEventArgs e) 63 { 64 hogehoge(sender, null); 65 } 66 private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e) 67 { 68 // 0~1の間で筆圧が取れる 69 var pressure = e.GetCurrentPoint(null).Properties.Pressure; 70 Debug.WriteLine(pressure); 71 var p = e.GetCurrentPoint(drawingCanvas); 72 Debug.WriteLine($"({p.Position.X},{p.Position.Y})"); 73 74 } 75 76 private void Canvas_PointerPressed(object sender, PointerRoutedEventArgs e) 77 { 78 PointerPoint point = e.GetCurrentPoint(drawingCanvas); 79 80 switch (point.PointerDevice.PointerDeviceType) 81 { 82 case PointerDeviceType.Pen: 83 84 break; 85 case PointerDeviceType.Mouse: 86 text_isPointerOn.Text = string.Format("IsPointerOn={0}", true); 87 break; 88 case PointerDeviceType.Touch: 89 90 break; 91 } 92 93 } 94 95 private void Canvas_PointerMoved(object sender, PointerRoutedEventArgs e) 96 { 97 PointerPoint point = e.GetCurrentPoint(drawingCanvas); 98 99 switch (point.PointerDevice.PointerDeviceType) 100 { 101 case PointerDeviceType.Pen: 102 103 break; 104 case PointerDeviceType.Mouse: 105 text_position.Text = string.Format("Position={0},{1}", point.Position.X, point.Position.Y); 106 break; 107 case PointerDeviceType.Touch: 108 109 break; 110 } 111 } 112 113 private void Canvas_PointerReleased(object sender, PointerRoutedEventArgs e) 114 { 115 PointerPoint point = e.GetCurrentPoint(drawingCanvas); 116 117 switch (point.PointerDevice.PointerDeviceType) 118 { 119 case PointerDeviceType.Pen: 120 121 break; 122 case PointerDeviceType.Mouse: 123 text_isPointerOn.Text = string.Format("IsPointerOn={0}", false); 124 break; 125 case PointerDeviceType.Touch: 126 127 break; 128 } 129 } 130 131 private void Header_SelectionChanged(object sender, RoutedEventArgs e) 132 { 133 134 } 135 136 } 137} 138
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/04 05:57
2019/09/04 06:59
2019/09/24 02:48