2010-11-26 33 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分鐘內放棄。

相關問題