2017-06-20 32 views
1

私は、アプリケーションと同じ高さで50の幅を持つグリッドを持っています。幅も50です。私はこのボタンをマウスでドラッグして縦の左の軸に沿って移動したいと思います。しかし、それは普通にクリックすることができます。私はマイクロソフトのドラッグアンドドロップサンプルでこれをやろうとしましたが、実装したい手順はドラッグ&ドロップではありません。ユニバーサルなWindowsアプリケーションで、XAMLとC++ - cxをコードの背後に使って、これをどのように実装できますか?グリッド/ ButtonのUWP/C++ - cx - ドラッグして縦軸のボタンを移動する

私のXAML-コード:あなたの条件については

<Grid x:Name="Grid1" Width="50" > 
<Button x:Name="btnMove" Content="Move me!" Background="PaleGreen" Click="btnMove_Click" VerticalAlignment="Top" HorizontalAlignment="Left" Width="50" Height="50"></Button> 
</Grid> 

答えて

4

、あなたはManipulationDeltaクラスを使用することにより、縦軸上のボタンを移動することができます。そして次のコードでそれを達成することができます。 詳細については、Handle pointer inputを参照してください。ここにはofficial codeサンプルがあります。

MainPage::MainPage() 
{ 
    InitializeComponent(); 
    InitManipulationTransforms(); 
    btnMove->ManipulationDelta += ref new ManipulationDeltaEventHandler(this, &MainPage::btnMove_ManipulationDelta); 
    btnMove->ManipulationMode = ManipulationModes::TranslateX; 
} 

void App14::MainPage::InitManipulationTransforms() 
{ 
    transforms = ref new TransformGroup(); 
    previousTransform = ref new MatrixTransform(); 
    previousTransform->Matrix = Matrix::Identity; 

    deltaTransform = ref new CompositeTransform(); 

    transforms->Children->Append(previousTransform); 
    transforms->Children->Append(deltaTransform); 

    // Set the render transform on the rect 
    btnMove->RenderTransform = transforms; 
} 

void App14::MainPage::btnMove_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
{ 

} 
void MainPage::btnMove_ManipulationDelta(Platform::Object^ sender, ManipulationDeltaRoutedEventArgs^ e) 
{ 


    previousTransform->Matrix = transforms->Value; 

    // Get center point for rotation 
    Point center = previousTransform->TransformPoint(Point(e->Position.X, e->Position.Y)); 
    deltaTransform->CenterX = center.X; 
    deltaTransform->CenterY = center.Y; 

    // Look at the Delta property of the ManipulationDeltaRoutedEventArgs to retrieve 
    // the rotation, scale, X, and Y changes 
    deltaTransform->Rotation = e->Delta.Rotation; 
    deltaTransform->TranslateX = e->Delta.Translation.X; 
    deltaTransform->TranslateY = e->Delta.Translation.Y; 
} 

あなたはボタンのManipulationModeを変更することで、ボタンのスクロール方向を変更することができます。

btnMove->ManipulationMode = ManipulationModes::TranslateY; 

enter image description here

+0

どうもありがとう、それが正常に働いています。しかし、私はまだ1つの質問があります。このコードでは、ボタンはappbordersの外に出ることができますし、彼を戻すことはできません、彼は国境の後ろに立ち往生します。これをどうやって解決するのですか?国境を設定するにはどうすればいいですか? – David

+0

さんの倍精度高さ=(dynamic_cast (Window :: Current-> Content)) - > ActualHeight; ' Frameの現在の高さを宣言しました。ここでは、前に投稿したif構造をコード内に正確に配置していますか? – David

関連する問題