I am porting a WinForms application to WPF. As part of the existing functionality the user can click on any running windows application (including outside of my running WPF app) that will capture the window handle and mouse coordinates. Is this possible?
Asked
Active
Viewed 596 times
0
-
2You already told us it's possible in your question :P – hoodaticus Jun 27 '17 at 21:07
-
Unfortunately the globalhook method doesn't seem to play nice with WPF :( – Mark Ruse Jun 27 '17 at 21:08
-
I'm not sure WPF has anything to do with it. https://stackoverflow.com/questions/11180773/setwindowshookex-for-wh-mouse Maybe you should run the hook in its own thread though. – hoodaticus Jun 27 '17 at 21:08
-
1I have checked out that article but it is implemented in C++ using windows hooks. I was hoping to find a native C# WPF way of achieving this as most of the returning structures are troublesome when using in C# as I found when trying to port the initial hook code from the working Winforms app – Mark Ruse Jun 28 '17 at 07:28
-
Sorry for the trouble you've had, Mark. Maybe try to get it started though? We SO guys are pretty good at p/invoke stuff. – hoodaticus Jun 28 '17 at 12:40
1 Answers
-1
yes you can do it with WinApi global Hoocker for mouse and keyboard
private readonly MouseHookListener m_MouseHookManager;
public Form1()
{
InitializeComponent();
m_MouseHookManager = new MouseHookListener(new GlobalHooker());
m_MouseHookManager.Enabled = true;
m_MouseHookManager.MouseClick += m_mouseHookListener_Coordinates;
}
private void m_mouseHookListener_Coordinates(object sender, MouseEventArgs e)
{
MessageBox.Show(this,"Coordinate X:"+e.X +" Coordinate Y:"+e.Y,"Coordinates",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
And is working and was tested by me
Sorry this is for Windows Form App
-
It seems the `MouseHookListener` is from an external library (https://globalmousekeyhook.codeplex.com/documentation) – JiBéDoublevé Nov 24 '17 at 12:46