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
Solved

Find handle of control that has Text Cursor?

Posted on 1998-07-14
8
232 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:pbeuger
  • 4
  • 3
8 Comments
 
LVL 1

Expert Comment

by:Greedy
ID: 1357875
can't you just say something like (MyForm.ActiveControl as TWinControl).Handle
0
 

Author Comment

by:pbeuger
ID: 1357876
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.
0
 
LVL 4

Accepted Solution

by:
jeurk earned 100 total points
ID: 1357877
Hi,
I suggest you use The GetCaretPos function to copie the caret's position, in client coordinates, to the specified POINT structure.
After that use the function ControlAtPos(const Pos: TPoint; AllowDisabled: Boolean): TControl; to get the control that holds the caret.
What do you think ?

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:pbeuger
ID: 1357878
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.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 1357879
Hi,
I will look more closely at it and try to get a running solution
that I will submit to you.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 1357880
//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;

0
 

Author Comment

by:pbeuger
ID: 1357881
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.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 1357882
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.

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

791 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