欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

鼠标移动时,光标相对于对象的位置

发布时间:2023/12/13 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 鼠标移动时,光标相对于对象的位置 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

 

鼠标在一个物体上移动时,能够计算出光标相对于任何其它对象的相对位置。 Code
 <Grid x:Name="LayoutRoot" Background="White">
        
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid1" Background="Red" Width="100" Height="100" MouseEnter="Grid_MouseEnter"
               MouseMove
="Grid_MouseMove"
              MouseLeave
="Grid_MouseLeave"
              
>
            
        
</Grid>

        
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid2" Margin="150,0,0,0" Background="Green" Width="100" Height="100" MouseMove="Grid_MouseMove_1">
              
        
</Grid>

        
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Name="grid3" Background="Blue" Margin="300,0,0,0" Width="100" Height="100" MouseMove="Grid_MouseMove_2">

        
</Grid>
        
<TextBlock Name="label1" Margin="20,200,0,0">
            
            
<Run    Foreground="Red"  >
               mouse 离grid1左上角距离:
                 
            
</Run>
           
            
<LineBreak></LineBreak>
            
<Run  Foreground="Green">
               mouse 离grid2左上角距离:
            
</Run>
            
<LineBreak/>
            
<Run  Foreground="Blue">
               mouse 离grid3左上角距离:
            
</Run>
            
            
            
        
</TextBlock>

    
</Grid>
Code
  public partial class Page : UserControl
    {
        
public Page()
        {
            InitializeComponent();
        }

        
private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {

        }

        
private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
          
this.label1.Text="Mouse 离grid1距离为:"+e.GetPosition(this.grid1)+"\r\n";
          
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
          
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3); 
            
        }

        
private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {

        }

        
private void Grid_MouseMove_1(object sender, MouseEventArgs e)
        {

            
//this.label1.d = "<Run    Foreground=\"Red\"  > mouse 离grid1中心距离:</Run>" + e.GetPosition(this.grid1) + "<LineBreak></LineBreak>";
            this.label1.Text = "Mouse 离grid1距离为:" + e.GetPosition(this.grid1) + "\r\n";
            
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
            
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3); 
        }

        
private void Grid_MouseMove_2(object sender, MouseEventArgs e)
        {
            
this.label1.Text = "Mouse 离grid1距离为:" + e.GetPosition(this.grid1) + "\r\n";
            
this.label1.Text += "Mouse 离grid2距离为:" + e.GetPosition(this.grid2) + "\r\n";
            
this.label1.Text += "Mouse 离grid3距离为:" + e.GetPosition(this.grid3); 
        }
    }

转载于:https://www.cnblogs.com/netfuns/archive/2009/09/12/1565195.html

创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的鼠标移动时,光标相对于对象的位置的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。