How can I detect that my mouse is over a text box from another program?
Who is Participating?
MaquiaveloConnect With a Mentor Commented:
 Use the GetFocus API function, it returns a handle of the window (the textboxes are windows too) that has the focus.

This sample line code shows U how to use the function:

Private Declare Function GetFocus Lib "user32" () As Long

lblFocus = "Focus Window: " & Str$(GetFocus())

  Then with this handle U can find out to which process it is associated, get the app name, etc.
Use the OLEDragOver event of the textbox
it would be difficult... for example in Delphi the class name of an edit box is originally TEdit.. and that's the best way for checking for editboxes... it is difficult..

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

alexbeatyAuthor Commented:
phiro, you said I could use the OLE dragover of the text box... but it only works if it is in my program not in an outside program!
you can see if the mouse pointer is over your app current form. then you can affect a bool flag each time the mouse pointer is not over that form.

Hope it helps
Neal HartmanCommented:
Your going to need a windows hook control like Spyworks.
alexbeatyAuthor Commented:
can you give me a source example?

hehehe... Mouse Over <> Focused Control
now the mouse is over the submit button but this edit window has got the focus because i'm typing in it :))

here is how to get the handle of control underneath the mouse...

type point
  x as long
  y as long
end type
dim h as long
dim pt as point

call GetCursorPos(pt)
h = WindowFromPoint(pt)

now "h" is a handle to the control underneath the mouse.. one way to check if it is an edit is to use this...

dim str as string * MAX_PATH-1

call GetClassName(h, str, MAX_PATH)
if UCase(str) = "EDIT" then MsgBox "The control underneath the mouse is an edit box control"

the classname is very important and most of the VC created programs would have that class name as identifier... but in Delphi for example that names is TEdit so this method would not work.. I've not tried in VB to see what the name is... some people create their own classes and change the name to whatever they want so it would be kind of difficult.. maybe there is some other identifier by which you'll be able to find that and I probably don't know of it, or can't think of it right now, but i doubt that...

btw, GetClassName(), GetCursorPos() and WindowFromPoint() are Win API functions so  you would have to declare them first before using them. Also, you will have to declare MAX_PATH which is a constant value. Use your API Viewer to find out what the declarations for the functions are and the constant value of MAX_PATH.

Good Luck!

 To ViKtOrnet: why using two API functions if U can do it with one? :))
Maquiavelo, and what is the function you're talking about?
Viktornet: it's in the answer. U can use GetFocus (It returns the handle of the window that has the focus at that moment) or GetCapture (the handle of the window that has the capture of the mouse).

Maquiavelo, please read my previous comment dated Tuesday, August 03 1999 - 08:26PM PDT...

the focused control is not the same as the control under the mouse...(in most cases).. if i put the mouse over the focused control then that would be true, otherwise it isn't, so GetFocus() does not work in this case... I already told you how to get the handle of the control under the mouse...


This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.
Thank you,
Moderator @ Experts Exchange
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.

All Courses

From novice to tech pro — start learning today.