现在我是一个应用程序,让用户单击按钮浏览图片以用作画布背景 . 我想这样做,如果用户点击画布上的某个位置,那么就会放置一个节点 . 我假设我需要获得鼠标坐标 . 是否有一种简单的方法可以调用将节点放在鼠标单击位置,或者我是否必须在此链接中使用该路径:WPF - Drawing on canvas with mouse events?提前致谢 .
编辑:添加了我试图制作椭圆的代码 . 它不会工作,我不知道如何使用鼠标点击椭圆的坐标 . 我知道一行,它只是.x1 = currentPoint.x等 .
画布的XAML代码:
制作帆布背景的代码背后:
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFileName = dlg.FileName;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(selectedFileName));
canvas1.Background = brush;
BitmapImage bitmap = new BitmapImage();
}
}
//This is the method for adding the ellipse.
private void addNode_MouseDown(object sender, MouseButtonEventArgs e)
{
Point currentPoint = new Point();
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
Ellipse ellipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
ellipse.Fill = mySolidColorBrush;
ellipse.Width = 10;
ellipse.Height = 10;
canvas1.Children.Add(ellipse);
}