【发布时间】:2017-05-12 17:53:36
【问题描述】:
我有一个 UWP C++ 应用程序,用户可以在其中向画布添加形状。我希望他们能够使用 Surface Pen 上的橡皮擦将其删除。
像这样成功地将项目添加到画布...
if (e->Pointer->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Pen) {
auto Translate = ref new TranslateTransform();
auto ellipse = ref new Ellipse();
PointerPoint^ p = e->GetCurrentPoint(imgCanvas);
Translate->X = p->Position.X - Radius/2;
Translate->Y = p->Position.Y - Radius/2;
ellipse->Fill = ref new SolidColorBrush(ColorHelper::FromArgb(127, 255, 255, 0));
ellipse->Width = Radius;
ellipse->Height = Radius;
ellipse->Stroke = ref new SolidColorBrush(Colors::Black);
ellipse->StrokeThickness = 1;
ellipse->RenderTransform = Translate;
ellipse->PointerPressed += ref new Windows::UI::Xaml::Input::PointerEventHandler(this, &MainPage::onEllipseTouched);
Canvas->Children->Append(ellipse);
}
我有下面的函数在被触摸时被成功调用
MainPage::onEllipseTouched(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
//check for eraser
PointerPoint^ p = e->GetCurrentPoint(Canvas);
if (p->Properties->IsEraser) {
//NEED TO REMOVE ITSELF FROM
//Canvas->Children->RemoveAt(i);
//but obviously I don't know I...
}
}
如何让椭圆从子列表中删除自己?
另一种方法是遍历画布中的所有孩子并计算最接近的。这听起来不像使用 EventHandler 那样干净。
编辑
我找到了这个示例Adding an ellipse to mouse position?,它还包含一个完全按照我尝试的方式删除的示例,但它在 C# 中的函数
GridCanvas.Children.Remove(ellipse);
存在。但是我在 C++ 中找不到和等价物。
【问题讨论】: