本文介绍了PreviewMouseMove无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好专家,
我正在使用WPF和C#.在此,我正在使用图像上的面板数.并尝试使用e.LeftButton == MouseButtonState.Pressed在图像上移动面板.它可以正确移动,但是当一个面板在另一面板上覆盖时,PreviewMouseMove会保留较旧的面板,然后拾取新的面板.
使用此代码:
Hello Experts,
I am working with WPF and C#. In this I am using number of panels on image. and trying to move panels over an image using e.LeftButton == MouseButtonState.Pressed.Its moving properly but when one panel override on another panel PreviewMouseMove leaves older one and pick up new one.
Using this code:
void panel_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point x = e.GetPosition(ImageHolder);
StackPanel panel = (StackPanel)sender;
if (e.LeftButton == MouseButtonState.Pressed && !dragging)
{
dragging = true;
//restrict dropped camera image to be in map image area
if ((Canvas.GetLeft(panel) + (x.X - p.X)) <= (Img.Width - panel.Width) && (Canvas.GetLeft(panel) + (x.X - p.X)) >= 0)
{
Canvas.SetLeft(panel, Canvas.GetLeft(panel) + (x.X - p.X));
}
if ((Canvas.GetTop(panel) + (x.Y - p.Y)) <= (Img.Height - panel.Height) && (Canvas.GetTop(panel) + (x.Y - p.Y)) >= 0)
{
Canvas.SetTop(panel, Canvas.GetTop(panel) + (x.Y - p.Y));
}
}
else if (e.LeftButton == MouseButtonState.Released)
{
dragging = false;
}
p = x;
}
向我建议一些修改或其他选择.
谢谢
Suggest me some modifications or else any alternative.
Thanks
推荐答案
StackPanel panel;
void panel_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point x = e.GetPosition(ImageHolder);
if (e.LeftButton == MouseButtonState.Pressed)
{
if (panel == null)
{
panel = (StackPanel)sender;
}
//restrict dropped camera image to be in map image area
if ((Canvas.GetLeft(panel) + (x.X - p.X)) <= (Img.Width - panel.Width) && (Canvas.GetLeft(panel) + (x.X - p.X)) >= 0)
{
Canvas.SetLeft(panel, Canvas.GetLeft(panel) + (x.X - p.X));
}
if ((Canvas.GetTop(panel) + (x.Y - p.Y)) <= (Img.Height - panel.Height) && (Canvas.GetTop(panel) + (x.Y - p.Y)) >= 0)
{
Canvas.SetTop(panel, Canvas.GetTop(panel) + (x.Y - p.Y));
}
}
else if (e.LeftButton == MouseButtonState.Released)
{
panel = null;
}
p = x;
}
这篇关于PreviewMouseMove无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!