Link to home
Start Free TrialLog in
Avatar of pbeuger
pbeuger

asked on

Find handle of control that has Text Cursor?

I'm looking for a way to find out what the Handle is of the control that has the text cursor (Caret) in it. The GetForeFroundWindow API returns the application handle that has the foxus, but I would like to get the handle of the actual control in that application that has the caret in it.
I've used SetWindowsHookEx with the WH_CALLWNDPROC option to hook into the SendMessage stream to look at GetFocus messages but I'm having trouble making that work correctly. (I've hooked into the KeyBoard and Mouse without any problems)
Any suggestions on how to make this work would be greatly appriciated! :)

Thanks, Paul.
Avatar of Greedy
Greedy

can't you just say something like (MyForm.ActiveControl as TWinControl).Handle
Avatar of pbeuger

ASKER

I guess I should have explained it better... I want to know what application/control has the text cursor, not what control in MY application has it.
ASKER CERTIFIED SOLUTION
Avatar of jeurk
jeurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pbeuger

ASKER

ControlAtPos only works for the Delphi program itself. It needs to work for all programs. For example, if the cursor is in the Netscape textbox I am now typing in, I would need the handle of that text box.
Hi,
I will look more closely at it and try to get a running solution
that I will submit to you.
//here it is, I'm not commenting it to well again,
//delphi crashed before I saved, so I'm too lazy for now
//anyway it is working
//it took me two hour, so if it's what you wanted, please give
//me a good mark, thaks.
//if not let me know what's still wrong
//try it you will see, it's kinda magic
//jeurk.
//i assume that is a global oui : boolean
PROCEDURE TForm1.Button2Click(Sender: TObject);
VAR
     p: tpoint;
     hp: hWnd; //parent
     hc: hwnd; //child
     hOtherWin, OtherThreadID, hFocusWin: integer;
BEGIN
     //we get a handle on the window that has the focus
     hOtherWin := GetForegroundWindow;
     OtherThreadID := GetWindowThreadProcessID(hOtherWin, NIL);
     //we attach the current thread to the focus of the other thread
     //otherwise the getcaretPos will not work
     IF AttachThreadInput(GetCurrentThreadID, OtherThreadID, true) THEN
     BEGIN
          //I use getfocus that gets me the handle of the window that has the current key focus
          hFocusWin := GetFocus;
          IF hFocusWin <> 0 THEN
          BEGIN
               //as an example I hide the control that has the focus
               IF oui THEN
               BEGIN
                    ShowWindow(hFocusWin, 0);
                    oui := NOT oui;
                    beep;
               END
               ELSE
               BEGIN
                    //it won't reappear, cause it won't have the focus again
                    ShowWindow(hFocusWin, 1);
                    oui := NOT oui;
                    beep;
               END;
          END;

          TRY
          //I tyed that first but getcaretpos get only the coords in client coords
//               //we get the pos of the caret
//               GetCaretPos(p);
//
//               hp := WindowFromPoint(p);
//               hc := ChildWindowFromPoint(hp, p);
//               IF oui THEN
//               BEGIN
//                    ShowWindow(hc, 0);
//                    oui := NOT oui;
//               END
//               ELSE
//               BEGIN
//                    ShowWindow(hc, 1);
//                    oui := NOT oui;
//               END;
//
//               showmessage('hello');
//               {
//               Remarks
//               Windows maintains an internal list, containing the handles
//               of the child windows associated with a parent window.
//               The order of the handles in the list depends on the Z order
//               of the child windows. If more than one child window contains
//               the given point, Windows returns the handle of the first window
//               in the list that contains the point.
//               }
          FINALLY
                 //don't forget to detach
               AttachThreadInput(GetCurrentThreadID, OtherThreadID, False);
          END;
     END;
     {
      Please note that key state, which can be ascertained by calls to the
      GetKeyState or GetKeyboardState function, is reset after a call to AttachThreadInput.
     }
END;

Avatar of pbeuger

ASKER

Looks very promising! I'll try this out as soon as I have a chance...work is not cooperating :( I just don't want you to think I have forgotten about this :)

Paul.
Cool to let me know that.
It's ok anyway.
When you try it out, and it's not working like you want let me know.