1
我想在用戶單擊的畫布上的點繪製矩形。我希望我的窗口可縮放,因此我已將Canvas放置在視圖框中,但這不會在指定點上繪製矩形,而是基於視框的縮放而變化。 這裏是我的代碼在wpf中的viewbox上單擊點繪製形狀
<Viewbox MouseLeftButtonDown="Viewbox_MouseLeftButtonDown" Width="300" Height="300">
<Canvas Name="Surface" MouseLeftButtonDown="Surface_MouseRightButtonDown" Background="Transparent" Width="300" Height="300"></Canvas>
</Viewbox>
這裏是視框中鼠標按下事件
private void Viewbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Rectangle rect = new Rectangle { Width = 10, Height = 10, Fill = Brushes.Black };
Surface.Children.Add(rect);
Canvas.SetLeft(rect, e.GetPosition(this).X);
Canvas.SetTop(rect, e.GetPosition(this).Y);
}
結果是,即使我打電話畫布鼠標事件一樣。