2010-11-26 135 views
0

我想在屏幕上的任意位置跟踪屏幕坐标中鼠标光标的位置。所以即使鼠标光标移动到窗口边界之外,是否有办法获得鼠标光标的位置?如何将GetMousePosition放置在屏幕上的任何地方,在窗口的边界之外(或任何可视)

我正在做的是试图让弹出窗口跟随鼠标光标,即使它离开主窗口。

这里是什么,我已经尝试了代码片段(和没有工作):

 private void OnLoaded(object sender, RoutedEventArgs e) 
    {   
     bool gotcapture = this.CaptureMouse(); 
     Mouse.AddLostMouseCaptureHandler(this, this.OnMouseLostCapture); 
    } 
      Point mouse_position_relative = Mouse.GetPosition(this); 
     Point mouse_screen_position = popup.PointToScreen(mouse_position_relative); 
     private void OnMouseLostCapture(object sender, MouseEventArgs e) 
    { 
     bool gotcapture = this.CaptureMouse(); 
     this.textblock.Text = "lost capture."; 
    } 

回答

0

没关系,我意识到有没有办法相对于屏幕弹出的位置上,只是相对的到包含它的Visual。

3

究竟是什么问题?
等等!有一种方式来定位弹出相对于屏幕。看到PlacementMode.AbsolutePoint
这表明小笑脸到处飞:

private Popup _popup; 

public Window1() 
{ 
    InitializeComponent(); 

    this.Loaded += OnLoaded; 
} 

private void OnLoaded(object sender, RoutedEventArgs e) 
{ 
    _popup = new Popup 
       { 
        Child = new TextBlock {Text = "=))", Background = Brushes.White}, 
        Placement = PlacementMode.AbsolutePoint, 
        StaysOpen = true, 
        IsOpen = true 
       }; 
    MouseMove += MouseMoveMethod; 
    CaptureMouse(); 
} 

private void MouseMoveMethod(object sender, MouseEventArgs e) 
{ 
    var relativePosition = e.GetPosition(this); 
    var point= PointToScreen(relativePosition); 
    _popup.HorizontalOffset = point.X; 
    _popup.VerticalOffset = point.Y; 
} 
0

有许多的方式来获得一个WPF Window外的鼠标位置的屏幕坐标。不幸的是,你需要添加引用来使用它们中的任何一个,但它可能是。你可以在@ FredrikHedblad对How do I get the current mouse screen coordinates in WPF?问题的回答中找到他们的例子。巧合的是,这个问题在你提出这个问题之前几天才回答,并在21分钟内放弃。

相关问题