如何防止用鼠标移动网格走出屏幕? [英] How prevent grid move with a mouse go out of the screen?

查看:94
本文介绍了如何防止用鼠标移动网格走出屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < 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();
}


 

如何防止网格走出屏幕边界?






$

解决方案

您好Groboto,


我无法理解您的问题,但我认为您希望鼠标不会移出MainWindow或Grid。


如果是这样,那么您可以使用 


参考System.Windows.Forms  (右键单击引用>添加引用> .NET选项卡> Systems.Windows.Forms>
ok)

使用System.Windows.Forms; //引用它

创建一个像这样的点

 Point position = System.Windows.Forms.Cursor.Position; 
// x = position.X
// y = position .Y

然后创建一个if语句来检查光标是否正在接触MainWindow或Grid。


你可以使用 

获取MainWindow的x和y

 int x = Application.Current.MainWindow.Left; // for x 
int y = Application.Current.MainWindow.Top; // for y

如果if条件为true,则可以使用

 //应添加x和y值
Cursor.Position = new Point(x,y);

希望,这有帮助。


谢谢..



<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?

解决方案

Hi Groboto,

I can't understand your question, But I think that You want the mouse not move out of the MainWindow or the Grid.

If that so, Then you can check the coordinates of the mouse using 

Reference System.Windows.Forms (right click on references > add reference > .NET tab > Systems.Windows.Forms > ok)

using System.Windows.Forms; //Reference it

The create a Point like this

Point position = System.Windows.Forms.Cursor.Position;
//x = position.X
//y = position.Y

Then create an if statement to check whether the cursor is touching the MainWindow or the Grid.

You can get the x and y of MainWindow using 

int x = Application.Current.MainWindow.Left; // for x
int y = Application.Current.MainWindow.Top; // for y

If the if condition is true then you can change the mouse position inside the MainWindow using

// x and y values should be added
Cursor.Position = new Point(x, y);

Hope, That helps.

Thanks..


这篇关于如何防止用鼠标移动网格走出屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆