C# 如何用鼠标移动面板中的控件
当鼠标位于边界控制线之外时,我无法移动边界控制线C# 如何用鼠标移动面板中的控件,c#,wpf,controls,C#,Wpf,Controls,当鼠标位于边界控制线之外时,我无法移动边界控制线 <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"
<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);
// 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);
}
}
只需在鼠标位于边界内时捕获鼠标,并在任务完成后释放鼠标,否则它将粘住。或者根据您的代码,可能是这样->
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);
}
}
在Windows窗体应用程序中,尽管按下鼠标按钮时鼠标处于不受控制的状态,但仍会激发鼠标事件。在Windows窗体应用程序中,尽管按下鼠标按钮时鼠标处于不受控制的状态,但仍会激发鼠标事件。