Solved

Find handle of control that has Text Cursor?

Posted on 1998-07-14
8
230 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now