WinUI3 メモ マウスイベント - DrumMidiEditor/DrumMidiEditorApp GitHub Wiki

マウスイベント

https://docs.microsoft.com/ja-jp/windows/apps/design/input/mouse-interactions

マウスイベント:
 PointerPressed
 PointerMoved
 PointerReleased

 PointerPressed イベントと PointerReleased イベントは、常にペアで発生するわけではありません。
 アプリでは、ポインターの押下を終了させる可能性のあるすべてのイベント
 (PointerExited、PointerCanceled、PointerCaptureLost など)をリッスンして処理する必要があります。

マウス情報取得:

var p = args.GetCurrentPoint( sender as FrameworkElement );  
p.Properties.IsLeftButtonPressed  
p.Properties.IsRightButtonPressed  
p.Position  
( args.KeyModifiers & VirtualKeyModifiers.Control ) == VirtualKeyModifiers.Control  

マウスイベント外からの情報取得は不明。

ドラッグ&ドロップイベント

下記は、GridViewのアイテムをドラッグして、別のアイテム(TextBlock)へドロップする場合のメモ
(設定が必要な項目のみ記載)

<GridView
	SelectionMode="Single"
	CanDragItems="True"
	DragItemsStarting="ImportMidiMapGridView_DragItemsStarting" />

<TextBlock
	AllowDrop="True"
	Drop="TextBlock_Drop"
	DragEnter="TextBlock_DragEnter"
	DragLeave="TextBlock_DragLeave" />
/// <summary>
/// ドラッグ
/// </summary>
private void ImportMidiMapGridView_DragItemsStarting( object sender, DragItemsStartingEventArgs args )
{
	if ( args.Items[ 0 ] is not ImportMidiMapData data )
        {
		return;
        }

	args.Data.SetData( "midimapdata", data.AfterName );
	args.Data.RequestedOperation = DataPackageOperation.Copy;
}

/// <summary>
/// ドロップ
/// </summary>
private async void TextBlock_Drop( object sender, DragEventArgs args )
{
	switch ( args.AcceptedOperation )
        {
        	case DataPackageOperation.Copy:
                	{
				var def = args.GetDeferral();

				var obj = await args.Data.GetView().GetDataAsync( "midimapdata" );

				var name = ( obj as string ) ?? string.Empty;

				def.Complete();

				if ( sender is not TextBlock textblock )
	                        {
        		                return;
		        	}

				textblock.Text = name;
			}
                    	break;
	}
}

/// <summary>
/// ドラッグ範囲内に入った
/// </summary>
private void TextBlock_DragEnter( object sender, DragEventArgs args )
{
	args.AcceptedOperation = DataPackageOperation.Copy;
}

/// <summary>
/// ドラッグ範囲外に外れた
/// </summary>
private void TextBlock_DragLeave( object sender, DragEventArgs args )
{
	args.AcceptedOperation = DataPackageOperation.None;
}
⚠️ **GitHub.com Fallback** ⚠️