Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mouse capture even out of my form

Posted on 1999-06-28
10
Medium Priority
?
251 Views
Last Modified: 2010-04-06
How to capure the mouse position even if it
is out of my form (and OnmouseMove does not
work) without calling GetCursor all the time?
I've tried to work with WindowHooks and WH_MOUSe
but still only called if it's on my form.
How to do?
0
Comment
Question by:cydo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1385007
Use SetCapture and ReleaseCapture. Setcapture directs all mouse input to the specified window, eg:

SetCapture(MyForm.Handle);

Your form will get OnMouseMove evetns even when the mouse is not over your form.

Cheers,

Raymond.

0
 

Author Comment

by:cydo
ID: 1385008
Hmm does not work, i've put that SetCapture(self.Handle);
in the forms oncreate event, but i seems, that it
will lose the capture after some points out of the
form, because now i can see e.g. x=-13 but thats
all. I move the mouse outside the form but it does not create
any onMouseMove events.

0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1385009
Oh yes, there is one little proviso, you will get the mouse move events only if the mouse button is held down while the mouse is over a window of another thread.

Does that help?

If not you might need to look at hooking the mouse - but thats getting nasty...

Cheers,

Raymond.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 300 total points
ID: 1385010
Hi guys.

Um.  Another way of doing this is create a thread which has the following code in it:


Function CursorPosition: TPoint;
var Mouse: TMouse;

begin
   CursorPosition := Mouse.CusorPos;
end;

This worked perfectly for me.  If you dont want to do it in a thread, just use a TTimer component which has a quick interval set (less than 50ms).  Works just as well, its just not as nice :)

Cheers,

Stuart
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1385011
OH! The above code will get the current position all the time, if you only want to get it every now and then, just call that function manually.

Cheers,


Stuart
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1385012
Stuart: Cute!

Cheers,

Raymond.

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1385013
Oh shucks, thanks Ray :)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1385014
Hi all,

the usual way to capture the mouse all the time is to create a hidden tool window (AllocateHWND) and set the mouse capture to this window. The associated WindowProc will then get all mouse messages and can forward them to the main window (or whereever they are needed). An example of doing this is TSpeedButton with a drop down menu. When you click the arrow area on the button then the popup window appears and gets all the mouse messages (redirected from the hidden tool window).

One point to consider here. Windows automatically releases the mouse capture of a window when the user has clicked the mouse (actually when the mouse button is released). To keep the capturing alive you have therefore to listen to the WM_LBUTTONUP/WM_RBUTTONUP message and "refresh" the capturing by again calling SetCapture(FCaptureWindow).

Ciao, Mike
0
 

Author Comment

by:cydo
ID: 1385015
thats a nice idea doing in a thread.
i've extended that with checking if the mouse
hast changed it's position then i will
create a OnMouseMove Event.

Thanks
 cydo
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1385016
Do you really consider this as good solution? I'm quite perplex. Polling the mouse yourself bombs your application nearly back to DOS times. Have you ever heard that Windows is event-driven? Windows already uses an own thread/process to handle mouse events and you will waste resources for such a simple task as mouse capturing.

On the other hand, if you do not intend to make a professional product (perhaps with further threads in it) then it doesn't really matter whether you are polling states which are reported automatically to your application by Windows.

Ciao, Mike
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question