C#(WPF)にて、タブレット端末で動作する画面を製作しています。
画面を閉じるボタン(×)を自前で作成する必要があるのですが
閉じるボタン(×)に対して「タッチダウン→指を下に移動してタッチリーブ」をすると
稀にクリックイベントが発生してしまい、画面を閉じる際のメッセージBOXが表示されてしまいます。
「タッチダウン→指を下に移動してタッチリーブ」の操作ではメッセージBOXを表示したくありません。
原因や対応策について、思い当たる方がいらっしゃれば、ご教示お願いします。
開発環境:.NetFramework 4.0
動作環境:Windows11
XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontSize="24"
FontFamily="MS Gothic"
WindowState="Maximized"
WindowStyle="None"
UseLayoutRounding="True">
<Grid>
<DockPanel>
<Button Content="×" Name="Exit" Margin="0" Padding="3" Width="30" Height="25"
Click="Exit_Click" TouchDown="Exit_TouchDown" TouchUp="Exit_TouchUp" TouchLeave="Exit_TouchLeave"
Visibility="Visible" FontSize="12" Foreground="Black" HorizontalAlignment="Center"
VerticalAlignment="Top" DockPanel.Dock="Right" ClickMode="Press" Background="Transparent"/>
<StackPanel Background="Transparent">
<TextBlock Background="Transparent" Text="test"/>
</StackPanel>
</DockPanel>
</Grid>
</Window>
コードビハインド
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// クリックイベント処理
private void Exit_Click(object sender, RoutedEventArgs e)
{ // アプリケーション終了ボタン クリックイベント処理
MessageBoxResult ret;
ret = System.Windows.MessageBox.Show( "オペキーボードを終了します。よろしいですか?",
"終了確認",
MessageBoxButton.YesNo,
MessageBoxImage.Question); // 終了確認ダイアログ表示
if (ret == MessageBoxResult.Yes)
{ // ダイアログ展開成功か
System.Windows.Application.Current.Shutdown(); // オペキー画面終了
}
}
// タッチダウンイベント処理
private void Exit_TouchDown(object sender, TouchEventArgs e)
{
System.Windows.Controls.Button btnObj = (System.Windows.Controls.Button)sender; // ボタンオブジェクト生成
btnObj.ClickMode = ClickMode.Release;
}
// タッチアップイベント処理
private void Exit_TouchUp(object sender, TouchEventArgs e)
{
Exit_Click(sender,e);
}
// タッチリーブイベント処理
private void Exit_TouchLeave(object sender, TouchEventArgs e)
{
System.Windows.Controls.Button btnObj = (System.Windows.Controls.Button)sender; // ボタンオブジェクト生成
btnObj.ClickMode = ClickMode.Press;
}
}
}