Solved

Send  Focus to anywhere control in other application by "label" in control

Posted on 2003-10-24
9
760 Views
Last Modified: 2007-12-19
Hello Experts,

I'm making an application that send mouse events and text to edit controls etc., to other applications but my problem it's that before send focus to anywhere control in the other application I need know the position of this control then the resolution is a problem before the position of the controls change.

In this days I'm using "Dragon NaturallySpeaking 5.0" , then when I run Dislplay Properties by default the initial Tab is "BackGround" then I say "Configuration" and "Dragon NaturallySpeaking" send the focus to the tab with label "Configuration" then I say "Colors" then send focus to control with label "Colors".

My cuestion is: how to send focus to anywhere control with label "anywherelabel" in other application? , please help me.

Thanks in advance.
0
Comment
Question by:MauricioGaviria
[X]
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
  • 8
9 Comments
 
LVL 2

Expert Comment

by:alsantos
ID: 9618180
You can use this code:

var
  T: HWND;
begin
  T := FindWindow('TForm1', nil); // TForm1 is the name of the window, you can use WinSight32 to detect it ( Winsight32 come with Delphi )
  T:=FindWindowex(T,0,'TEdit',nil); // TEdit is the name of the Edit that you want to set focus, you TLabel , of any name.
  if T<>0 then
    SendMessage(T, WM_SETFOCUS, 0, 0); // this code to send focus to it.
end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9618209
sorry, it don't bring to front the application with window name TForm1, it only set the focus to the edit . Put this code below to set focus to Edit and put the appplication to front.

procedure TForm1.Button1Click(Sender: TObject);
var
  T: HWND;
begin
  T := FindWindow('TfrmRG', nil); // TForm1 is the name of the window, you can use WinSight32 to detect it ( Winsight32 come with Delphi )
  SetForegroundWindow(T); // will put the application to the first ( front )
  T:=FindWindowex(T,0,'TEdit',nil); // TEdit is the name of the Edit that you want to set focus, you TLabel , of any name.
  if T<>0 then
    SendMessage(T, WM_SETFOCUS, 0, 0); // this code to send focus to it.
end;

alsantos
0
 
LVL 2

Accepted Solution

by:
alsantos earned 500 total points
ID: 9618216
I put TfrmRG because my test application here use the window name TfrmRG , You can use TForm or whatever window name you want.
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:MauricioGaviria
ID: 9628448
Hello alsantos,

I use your code , but in function "FindWindow" the parameters are FindWindow(nil,'TfrmRG')  else the result = 0 , in the function "FindWindowex(T,0,'TEdit',nil)"
 I don't understand if  'TEdit' is the "Text" in the control or I need know the name of the control, I test this with one app  that contain two controls  Button1 with caption "The Button1" and Button2 "default focus" with Caption "Second Button "  then I run this, immediately run other app with your code, then I replace the name of the Window
        T := FindWindow(nil,'TfrmRG');
        T > 0 (Windows found),
and replace the 'TEdit' with 'Button1' but T=0, then replace 'Button1' by ''The Button1'' (Caption) but the result = 0 .

Please, tellme what's wrong.

Thanks in advance.
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629031
You don't have to use TfrmRG, use TForm1 or whatever is the name of your form. For example, if your form has the name MauricioGaviria, you use FindWindow('TMauricioGaviria', nil); is TNameOfYourForm that you set on the property editor, by default, the name is Form1, so you use FindWindow('TForm1', nil);

So, you found the HWND of the Main Form. Now, you have to find the HWND of the Button, so use T:=FindWindowEx(T, 0, 'TButton', nil); and It will return the HWND of the Button. Now you can setfocus to button. If it isn't the button that you want, you want the next button, you have to use T:=GetWindow(T, GW_HWNDNEXT); and it will return the HWND of the next button. So, I put below, an example:


procedure TForm1.Button1Click(Sender: TObject);
var
  T: HWND;
begin
  T := FindWindow('TForm1', nil); // TForm1 , Form1 is the default name of form, if you change the name of your form, you put here TNameofYourForm.
  SetForegroundWindow(T);
  T:=FindWindowex(T,0,'TButton',nil); // get the HWND of the first button on your Form1
  T:=GetWindow(T, GW_HWNDNEXT); // get the HWND of the next button.
  if T<>0 then
    SendMessage(T, WM_SETFOCUS, 0, 0);
end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629086
You tell me that you use FindWindow(nil,'TfrmRG') , if you use FindWindow(1TForm1', nil); you are looking for the class name. If you use FindWindow(nil, 'TfrmRG'); you are looking for a window with title TfrmRG. You choose the way you want.
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629098
the same to FindWindowEx, you can use FindWindowEx(T, 0, 'TButton', nil); you will look for the First object of class TButton or you can look for the object with name Button1 using FindWindowEx(T, 0, nil, 'Button1');

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629154
one more thing... MauricioGaviria , you are a brazilian guy, aren't you? :) brasileiro...  ?

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629487
and look.... name and caption is 2 things different. When you rename the name of Form from Form1 to SomeThing, you rename the class name of this form too. It will show on global var of the form like this:

var
  SomeThing: TSomeThing;

so... If you would like to find the window by the classname, you have to look for TSomeThing , FindWindow('TSomeThing', nil) . or use the caption to find the HWND, FindWindow(nil, 'CaptionNameOfTheForm');

but, when you rename the Name of a button, for example, you change only the name of this button, but the classname is the same: TButton , the same thing to Edit, Label....

I Think that you already understood that.

alsantos
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

628 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