none
How to get mouse position in screen coordinates?

    Question

  • Is there any way how to get mouse position relative to desktop (in screen coordinates)?

     

    I know about static method Mouse.GetPosition() but it requires parameter specifying IInputElement.

     

    Is there something like “screen” or “desktop” or something else I can use as parameter to GetPosition?

     

     

    As I need to get this screen mouse position inside method in my class derived from Grid I could use this:

    PointToScreen(Mouse.GetPosition(this))

     

    But still I think there could be some “desktop” object, or not?
    Thursday, July 20, 2006 4:02 AM

Answers

  • I cheated and added a reference to System.Windows.Forms and used the static property System.Windows.Control.MousePosition when I needed to compare it with screen co-ordinates from System.Windows.Forms.Screen.... This worked for me since both use the same units.

    //Tomi B.
    Thursday, July 20, 2006 8:46 PM

All replies

  • That is actually the best way to do it in WPF. If you'd like I can provide some Win32 code that does what you want but a better question is why you need the screen coordinates.
    Thursday, July 20, 2006 4:30 AM
    Moderator
  • You can find a root visual, which is normally your window and get its position. Didn't check it though.

     

    Thursday, July 20, 2006 8:29 AM
  • I cheated and added a reference to System.Windows.Forms and used the static property System.Windows.Control.MousePosition when I needed to compare it with screen co-ordinates from System.Windows.Forms.Screen.... This worked for me since both use the same units.

    //Tomi B.
    Thursday, July 20, 2006 8:46 PM
  • hi Tomi B.
     I cant't compile successfully  after your code "System.Windows.Control.MousePosition"

    And I can't find the namespace "System.Windows.Control"....

     hope your response.
     thanks
    Friday, November 3, 2006 9:36 AM
  • i think you might wanna look for system.windows.forms as the namespace and there you can use the static method control.mouseposition which is in it.

     

    hope this helps.

    Monday, December 4, 2006 5:42 PM
  • Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace StringPerf

    {

    public partial class PopupPanel : Form

    {

    private Boolean doDrag = false;

    private Int32 offsetX = 0;

    private Int32 offsetY = 0;

    public PopupPanel()

    {

    InitializeComponent();

    }

    void Parent_MouseMove(object sender, MouseEventArgs e)

    {

    }

    private void PopupPanel_MouseDown(object sender, MouseEventArgs e)

    {

    doDrag = true;

    offsetX = e.X;

    offsetY = e.Y;

    }

    private void PopupPanel_MouseUp(object sender, MouseEventArgs e)

    {

    doDrag = false;

    }

    private void PopupPanel_MouseMove(object sender, MouseEventArgs e)

    {

    if (doDrag)

    {

    this.Location = new Point(MousePosition.X - offsetX, MousePosition.Y- offsetY);

    }

    }

    }

    }

     

    Tuesday, May 15, 2007 1:06 AM
  • you may get the location with out going to Win forms

     

    this.PointToScreen(Mouse.GetPosition(this));

     

    this means object of Window.

    -Joy

    • Proposed as answer by Andreas_000 Wednesday, March 30, 2011 1:22 PM
    Thursday, June 21, 2007 9:18 AM
  • if you dont mind using the WindowsSDK
    GetCursorPos(POINT p);


    Friday, July 13, 2007 10:33 PM