Solved

Find handle of control that has Text Cursor?

Posted on 1998-07-14
8
233 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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