• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

Finding the Mouse location, and chaning cursor

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
ShadowHawk071998
Asked:
ShadowHawk071998
  • 8
  • 5
  • 3
  • +2
1 Solution
 
trestanCommented:
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
 
nietodCommented:
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
 
nietodCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
nietodCommented:
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
 
ShadowHawk071998Author Commented:
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
 
ShadowHawk071998Author Commented:
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
 
Andy_KeysCommented:
Once you have the x,y can't you use windowFromPoint to get you the HWND at that position!
0
 
nietodCommented:
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
 
ChefInnocentCommented:
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
 
nietodCommented:
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
 
ShadowHawk071998Author Commented:
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
 
nietodCommented:
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
 
ShadowHawk071998Author Commented:
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
 
nietodCommented:
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
 
trestanCommented:
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
 
nietodCommented:
>> 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
 
trestanCommented:
But he knew how to reject an answer.
0
 
ShadowHawk071998Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 8
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now