?
Solved

Finding the Mouse location, and chaning cursor

Posted on 1998-07-23
18
Medium Priority
?
322 Views
Last Modified: 2010-04-10
Hello. I need to find the Cursor location, but not on my window, on anywhere on the screen. I have
to decide if it is over a certain window, and if it is on it, then whether it is on a certain area on it, and then
change the cursor to a certain cursor of mine.
To Re-Phrase the question : Let's say I have a window, an application with a button on it. (Or any component,
or just some square I decide upon). Whenever the cursor goes on that point, I need it to change to my cursor.
But, of course, I'm doing this from ANOTHER application...
Can I hook somehow to the windows messages and get the mouse position whenever it is changed? How do
I do it efficiently?

please help,
                     Ron (Who is, BTW, using Visual C++ for that thing...)
0
Comment
Question by:ShadowHawk071998
[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
  • 8
  • 5
  • 3
  • +2
18 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1168455
You need to trap WM_MOUSEMOVE message:
WM_MOUSEMOVE fwKeys = wParam;        // key flags
xPos = LOWORD(lParam);  // horizontal position of cursor
yPos = HIWORD(lParam);  // vertical position of cursor
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168456
But you don't get the WM_MOUSEMOVE message when the cursor is over another application's window.  There are two ways around that however.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168457
One way is you can use a systemhook (SetWindowsHookEx())   You can use the hook to hook only mousemessages using the WH_MOUSE hook type.  Or you can use the hook to hook all messages and then look for mouse messages and ignore the others using the WH_MSGFILTER.  I don't really recomend this way.  The hook procedure has to be placed in a DLL for of all.  Most importatnly, If the hook code crashes it will usually take the rest of the system with it.
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 22

Expert Comment

by:nietod
ID: 1168458
The way I use (and this may or may not due for your needs) is to create a windows system timer (SetTimer()) at a high frequency, like 10 times a second.  Each time I get a timer notification I use GetCursorPos() to get the current position of the mouse.

Now this does nto work (well) for finding mouse presses that occur outside of your windows.  If you are interested in mouse presses that start on another app's window, you probably have to use a hook.  If the mouse will be up, or will be pressed in one of your windows and dragged to another app's window,  Then the timer technique seems to work well and is much easier and safer to implement.
0
 

Author Comment

by:ShadowHawk071998
ID: 1168459
Okay.  Now I know where the mouse is (X and Y). I will try that. But the problem won't end there.
I have a handle to the window of the application I want to check (Remember, I need to check if the mouse is over some specific application window, in a certain square in it!). How Do I do that?
If I have the mouse position, I now only need to find the position of that application window, and then I could calculate if I'm on the right place. Ideas?

thanks!
0
 

Author Comment

by:ShadowHawk071998
ID: 1168460
BTW - I DON'T need to find mouse presses. Like I said, the purpose is to change the
cursor when the mouse is over some area of that specific window.

0
 
LVL 1

Expert Comment

by:Andy_Keys
ID: 1168461
Once you have the x,y can't you use windowFromPoint to get you the HWND at that position!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168462
WindowFromPoint() will return the overlapped window that is under the mouse (or at any other specified point).  If you want to find a child window, you will need to use ChildWindowFromPoint() to get the overlapped window's child that is under the mouse.

Note if the window is dissabled, it will not be "found" by WindowFromPoint.  If that is a problem, let me know.  There are ways around this.
0
 

Accepted Solution

by:
ChefInnocent earned 120 total points
ID: 1168463
On method that I used is to convert all coordinates to Screen Coordinates in the mouse move.  Or course mouse move works best if you SetCapture() to your CWnd object.

void CCLass::OnMouseMove(UINT nFlags, CPoint point)
{
      CRect box;
      GetClientRect(box);
      CPaintDC dc(this);
      CPoint somepoint = point;
      ClientToScreen(&somepoint);



// Next I created a linked list of valid objects influenced by the originating CWnd object.  Since each CWnd object knows its own coords I cycled through the list until I found an object that the mouse was over.  Then I took the necessary action.



ListBoxList *target_ptr;
target_ptr = m_Targets;
CRect TargetRect;
while(target_ptr && target_ptr->Target)
{
  target_ptr->Target->GetWindowRect(&TargetRect);
  if (TargetRect.PtInRect(somepoint))
  {
    SetCursor(h_Copy);
    target_ptr->Target->Action();
    break;
  }
  else
    target_ptr = target_ptr->Next;
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1168464
If you read the question history you would see that Trestan already suggested that.  The problem is that WM_MOUSEMOVE doesn't occur when the mouse is over another application's window.  
0
 

Author Comment

by:ShadowHawk071998
ID: 1168465
Wouldn't all these options put rather a strain on the computer? I mean, cycling through lists,
timers and all. Consider that this piece of code must be on at all times. (And changing cursors, at that). Isn't there a simpler way?

Anyway, I think I have some pretty good ideas by now. I'll have to (of course) sit down and try it all :)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168466
Do you realize you just accepted an answer from chefinnocent that largely had be proposed by trestan?

>> Wouldn't all these options put rather a strain on the computer? I mean, cycling through lists,  timers and all.
Yes, that is why today's GUI applications perform at about the same speed (in terms of user interaction) as the non-graphical programs we tended to use 15 years ago.  And those programs ran on computers that performed about 1/1000th as quickly.

So the stuff is slow, but it is necessary and is happening all the time. when you move the mouse even a single pixel, the OS must search for the top window that is under the mouse.  That involves traversing a list of overlapped windows and comparing their coordinates with the mouse's.  Then it needs to traverse the list of child windows to find any child window that is under the mouse.  Then it has to see if the child has children under the mouse etc.  eventually it finds the window under the mouse and sends that application with that window a mouse move message.  it also sends it a NCHITTEST message and a SETCURSOR message.  this goes on every time the mouse moves.
0
 

Author Comment

by:ShadowHawk071998
ID: 1168467
Okay. I see the speed issue is not that horrible. I'll just have to see if my program effects
the overall speed :)

Actually, I know I was wrong giving that guy a credit. It's my first question here at the EE, so I guess I can be forgiven, I've yet to figure it out...


0
 
LVL 22

Expert Comment

by:nietod
ID: 1168468
forgiveness?  We're experts, not priests : - )

If someone answers with a answer you don't like you can reject the anwser and that allows another expert to answer.  If another expert has already provided an answer that you like, but within a comment, you can invite that expert to submit a "dummy" answer and grade that answer.  Remeber that:

Only one expert can have a pending answer at a time.  
If you grade a question you are giving points to the expert that has the pending answer.
If you reject an answer the question is open for other experts to answer.

0
 
LVL 8

Expert Comment

by:trestan
ID: 1168469
Such things happened a lot in the EE. Some user just reject an answer without having a detailed look at it while accept others without any consideration. I think it is really unfare because so many experts are trying their hard to provide better solutions. But some guy just step in and take away the points. I wonder if he did not know who will get the points (being unfamiliar with the procedure of EE), why he did not give me the points by chance at the first time when he saw the answer.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168470
>> I wonder if he did not know who will get the points
It seems almost everyone has problems with that the first time or two.  I don't know why.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1168471
But he knew how to reject an answer.
0
 

Author Comment

by:ShadowHawk071998
ID: 1168472
No. I knew how to do it, I got confused the second time. But it won't happen again (Okay, I hope it won't.).

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 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