[英]How to move control in panel by mouse
鼠标在边框控件外部时,我无法移动边框控件
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
x:Class="WpfApplication2.MainWindow"
x:Name="Window"
Title="Window1"
Width="346.5" Height="215" WindowStyle="SingleBorderWindow" >
<Grid Name="stack" >
<Border x:Name="btn" Width="50" Height="20" VerticalAlignment="Top" BorderThickness="1" BorderBrush="Black" Background="#FFF50000"
MouseMove="btn_MouseMove" MouseDown="btn_MouseDown" MouseUp="btn_MouseUp" />
</Grid>
</Window>
后面的代码
bool state = false;
Point prePoint;
private void btn_MouseMove(object sender, MouseEventArgs e)
{
if (state)
{
Point p = e.GetPosition(this);
Point p2 = e.GetPosition(btn);
btn.Margin = new Thickness(0, p.Y - p2.Y + p.Y - prePoint.Y, 0, 0);
prePoint = e.GetPosition(this);
}
}
private void btn_MouseDown(object sender, MouseButtonEventArgs e)
{
if (sender == btn)
{
prePoint = e.GetPosition(this);
state = true;
}
}
private void btn_MouseUp(object sender, MouseButtonEventArgs e)
{
state = false;
}
因为在将鼠标置于边框控件中时会触发鼠标移动事件。 我认为您需要为窗口添加鼠标移动事件而不是为边框控制
只要在边界内捕获鼠标并在任务完成后释放鼠标,否则它将粘住。 或acc。 到您的代码可能是这样->
bool state = false;
Point prePoint;
private void btn_MouseMove(object sender, MouseEventArgs e)
{
if (state)
{
Point p = e.GetPosition(this);
Point p2 = e.GetPosition(btn);
btn.Margin = new Thickness(0, p.Y - p2.Y + p.Y - prePoint.Y, 0, 0);
prePoint = e.GetPosition(this);
// Capture Mouse here ! as far as i think. !!!
Mouse.Capture(this.ColorPlane, CaptureMode.Element);
}
else
{
// Release Mouse here ! as far as i think. !!!
Mouse.Capture(null);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.