Solved

Find handle of control that has Text Cursor?

Posted on 1998-07-14
8
228 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

13 Experts available now in Live!

Get 1:1 Help Now