Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
How to disable mouse down outside WPF Window without Platform Invoke?
I want no response when pressing mouse down outside WPF Window.
Is it possible?

> Why?
I have a small window, user presses mouse down to create a circle and then he moves mouse up getting out the window to increase a size of the circle, at the end when he likes the size, he presses mouse down again to save the size, but it is already outside the window, i keep it by Mouse Capture and i don't wanna get another response other than my app's.

You said i can't disable it, i get it as i can't implement what i described above.

But i think i can create a very small not transparent window (because transparent window allows mouse work though itself), make its Style None and keep the window Top Most and Activated moving it under mouse by Mouse Position, it might work, but i never checked and it is not a professional way.
Posted
Updated 29-Apr-15 21:34pm
v7
Comments
Sergey Alexandrovich Kryukov 29-Apr-15 14:10pm    
Why? why?!
—SA
Ziya1995 30-Apr-15 3:38am    
I updated the question.

The result:
1. I can't stop mouse working outside my WPF Window.
2. I can't change Mouse Position in WPF.

Ok.
Sergey Alexandrovich Kryukov 30-Apr-15 9:42am    
As I say, both things are very good. At least something is stopping your from doing bad things.
—SA

1 solution

The question makes no sense: "mouse down" is something the user does with the physical mouse device, it cannot be disabled. And if you could disable moving the mouse pointer to any area of the screen, it would greatly damage the usability of the whole system. It would be hard to invent more irritating system abuse.

However, if you really want, you can "capture" the mouse by some UI element, to dispatch all software mouse events to this element. Please see:
https://msdn.microsoft.com/en-us/library/system.windows.uielement.capturemouse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.input.mouse.capture%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.input.capturemode%28v=vs.110%29.aspx[^].

But such things should only be done on temporary basis, for relatively short time. Here is one typical application: you want to create button-like behavior of some element. Say, you fist handle MouseDown on this element and change its state, and, say, it's view. Then you may want to handle MouseUp to get to the main state of the element, even if by that time the mouse pointer moved away from the element. Then you capture mouse in your MouseDown and un-capture after you handle MouseUp. You can use this idea instead of that hypothetical pointless "disable" action.

—SA
 
Share this answer
 
v3
Comments
Thomas Daniels 29-Apr-15 14:23pm    
5ed.
Sergey Alexandrovich Kryukov 29-Apr-15 14:26pm    
Thank you.
—SA
Ziya1995 29-Apr-15 14:56pm    
I know about Mouse Capture, it doesn't disable mouse down outside WPF Window.
I can't neither set mouse position nor disable it in WPF.
It is bad news.

Ok.
Sergey Alexandrovich Kryukov 29-Apr-15 15:03pm    
No, you can set mouse position. How can you try to do so?
And you cannot "disable" it. Both are good news. :-)
—SA
Ziya1995 29-Apr-15 15:22pm    
> No, you can set mouse position.
It works without Pinvoke in Windows Forms and with Pinvoke in WPF.
It doesn't work without Pinvoke in WPF.
WPF has no API to change mouse position.

If you know, let me know)

By "disable" i mean to keep mouse to work in my WPF app and not outside.
I can create a window and move it by mouse position, top most and activated - it can disable mouse, but it is not a professional way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900