Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

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

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
MauricioGaviria
Asked:
MauricioGaviria
  • 8
1 Solution
 
alsantosCommented:
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
 
alsantosCommented:
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
 
alsantosCommented:
I put TfrmRG because my test application here use the window name TfrmRG , You can use TForm or whatever window name you want.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MauricioGaviriaAuthor Commented:
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
 
alsantosCommented:
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
 
alsantosCommented:
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
 
alsantosCommented:
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
 
alsantosCommented:
one more thing... MauricioGaviria , you are a brazilian guy, aren't you? :) brasileiro...  ?

alsantos
0
 
alsantosCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now