Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get mouse position related to desktop in WPF?

Tags:

Problem

When you search for such question using google you get a lot of hits but all solutions assume you have at least one window.

But my question is just like I phrased it -- not assumptions at all. I can have a window, but I could have zero windows (because I didn't even show one or I just closed the last one). So in short the solution cannot rely on any widget or window -- the only thing is known, is there is a desktop (and app running, but it does not have any windows).

So the question is -- how to get the mouse position?

Background

I would like to show windows centered to mouse position. There is no such mode in WPF (there are only center to owner, or center to screen) so I have to do it manually. The missing piece is mouse position.

Edits

Thank you all, so now I have the first part of the solution -- raw position. Now there is a problem how to convert the data for WPF. I found such topic: WPF Pixels to desktop pixels but again, it assumes having some window.

Then I googled more and I found solution: http://jerryclin.wordpress.com/2007/11/13/creating-non-rectangular-windows-with-interop/

the code includes class for scaling up/down coordinates relying only on info about desktop. So joining those two pieces, I finally get the solution :-). Thanks again.