本文介绍了如何防止用鼠标移动网格走出屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< Grid x:Name =" grid"背景= QUOT;蓝色"
Width =" 100"高度= QUOT; 100"
MouseDown =" Grid_MouseDown"的MouseMove = QUOT; Grid_MouseMove"的MouseUp = QUOT; Grid_MouseUp">
< Grid.RenderTransform>
< TranslateTransform x:Name =" tt" />
< /Grid.RenderTransform>
< / Grid>
< Window x:Class =" Test.window" ...>
<网格x:名称="网格" ...
< /窗口>
Point m_start;
Vector m_startOffset;
private void Grid_MouseDown(object sender,MouseButtonEventArgs e)
{
m_start = e.GetPosition(window);
m_startOffset = new Vector(tt.X,tt.Y);
grid.CaptureMouse();
}
private void Grid_MouseMove(object sender,MouseEventArgs e)
{
if(grid.IsMouseCaptured)
{
Vector offset = Point.Subtract(e.GetPosition(window),m_start);
tt.X = m_startOffset.X + offset.X;
tt.Y = m_startOffset.Y + offset.Y;
}
}
private void Grid_MouseUp(object sender,MouseButtonEventArgs e)
{
grid.ReleaseMouseCapture();
}
如何防止网格走出屏幕边界?
$
解决方案
<Grid x:Name="grid" Background="Blue"
Width="100" Height="100"
MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp">
<Grid.RenderTransform>
<TranslateTransform x:Name="tt"/>
</Grid.RenderTransform>
</Grid>
<Window x:Class="Test.window"...> <Grid x:Name="grid"... </Window>
Point m_start;
Vector m_startOffset;
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
m_start = e.GetPosition(window);
m_startOffset = new Vector(tt.X, tt.Y);
grid.CaptureMouse();
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
if (grid.IsMouseCaptured)
{
Vector offset = Point.Subtract(e.GetPosition(window), m_start);
tt.X = m_startOffset.X + offset.X;
tt.Y = m_startOffset.Y + offset.Y;
}
}
private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
grid.ReleaseMouseCapture();
}
How prevent the grid go out of the screen boundaries?
解决方案
这篇关于如何防止用鼠标移动网格走出屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!